flare-engine
flare-engine copied to clipboard
Detect language automatically at first startup
So when starting the engine the first time, it will default to English language. We could try to detect the language of the operating system and see if we can get a match of the languages provided by the mods.
However we'd need to make sure that the right mods are loaded at startup. Finding out the default language of the operating system would highly depend on the operating system used unfortunately.
On posix systems it would check the LANG environment variable, which may be set to en_US.UTF-8 or de_DE.UTF-8 or similar. At those systems it would be sufficient to check if the first 2 letters are found in the list of engine/languages.txt
However we're still undecided on https://github.com/clintbellanger/flare-game/issues/254 where we discussed using another implementation of gettext (probably gnu). That implementation may have already an idea about the systems default language.
On both Unix and Windows platforms current locale is usually set and detected by setlocale
. GNU Gettext also uses locale set by this function. In multilingual C programs the first line in main
is usually
setlocale(LC_ALL, "");
which sets the current program locale to system's. This function also returns current locale as a string. The problem of detecting language is that different systems use different naming schemes.
#include <locale.h>
#include <stdio.h>
int main(void)
{
printf("%s\n", setlocale(LC_ALL, ""));
}
On my Ubuntu returns ru_RU.UTF-8
.
On Windows returns Russian_Russia.1251
.
Well I guess we're fine if we'd just take the first 2 characters off the returned string of `setlocale``and put it to lower case and see if we have that language available.
I did a proposal at the pull request #606 which is doing that exactly.
Well I guess we're fine if we'd just take the first 2 characters off the returned string of
setlocale
and put it to lower case and see if we have that language available.
This approximation will definitely fail for some popular languages.
German <-> de
Spanish <-> es
List that GNU Gettext uses to match language names: link
It would be better to take full language name from setlocale(LC_ALL, ""); cut it to underscore and then search it inside getLanguagesFullList()
@igorko
In most of the unixlike systems setlocale returns of the form language[_territory][.codeset][@modifier]
, where language is an ISO 639 language code, i.e. only 2 characters. So your proposal would work on Windows, but not on the other systems :/
Personally I am trying to get gnu gettext running, which would have this functionality included, so let's put this PR on hold until we have a decision whether we're going to use gettext (may be gnu or other implementations)