[sword-devel] swlocale question

Matthew Talbert ransom1982 at gmail.com
Thu Sep 3 20:42:45 MST 2009

> There is some interesting code in setDefaultLocaleName.  If you do a
> blame on this, you might find who can help you with the logic.  Don't
> have time right now to check but the comments are beyond my logic of
> system locales, so it's not likely me (unless I reformatted it).
> Anyway, as Jonathan Marsden mentioned earlier, if you are parsing system
> locale strings and trying to set and appropriate SWORD locale from such,
> we could sure use that logic in the engine for everyone to take
> advantage of.

OK, here's the code we came up with (Karl wrote it). This is based off
the environment variable LANG which is typically in the form of, say,
ar_EG for Arabic/Egyptian or fa_IR for Persian/Iran. So basically what
we do is get the list of available locales, then look for an exact
match. If we find one, we're done. If not, then we look for a match of
the first 5 characters. This should be the right thing nearly all of
the time, for those locales which actually have 5 characters. If it
doesn't find a match with 5 characters, it looks for a match of 2
characters. While this might be incorrect some of the time, it will
probably be correct most of the time. If we don't find anything after
all of this, we just give up.

This code could, and probably should, be incorporated into the engine.
It deals nicely with locales presented in the typical *nix way. For
Windows, you would have to convert the locale into a *nix style
format. We're using a glib-provided function for that already. I
wouldn't know how to go about parsing straight Windows locales. If
everyone thinks this is the right approach, then I can try to get it
into a suitable form for inclusion.


int ncmp[2] = { 5, 2 };	// fixed data

	if (sys_locale) {
		SWBuf locale;
		StringList localelist =
		StringList::iterator it;

		// whole match
		for (it = localelist.begin(); it != localelist.end(); ++it) {
			locale = *it;
			if (!strcmp(sys_locale, locale.c_str())) {
				return g_strdup(locale.c_str());

		// length-limited match
		for (int i = 0; i < 2; ++i) {
			for (it = localelist.begin(); it != localelist.end(); ++it) {
				locale = *it;
				if (!strncmp(sys_locale, locale.c_str(), ncmp[i])) {
					return g_strdup(locale.c_str());


More information about the sword-devel mailing list