Skip to content

Commit

Permalink
Use the translation map for unix system locale
Browse files Browse the repository at this point in the history
  • Loading branch information
CupnPlateGames committed Mar 25, 2024
1 parent c71a682 commit 38b625c
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions lib/framework/i18n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,57 @@ const char *getLanguageName()
return language;
}

#if !defined(__EMSCRIPTEN__) && !defined(WZ_OS_WIN)
const char *unixSystemLANGUAGE = nullptr;
const char *unixSystemLANG = nullptr;
static unsigned int getMatchingLanguage(const char *locale)
{
if (locale == nullptr)
{
return -1;
}
static char localeName[6] = { '\0' };
sstrcpy(localeName, locale);
char *delim = NULL;
unsigned int i;
// cut anything after a '.' to get rid of the encoding part
delim = strchr(localeName, '.');
if (delim)
{
*delim = '\0';
}
for (i = 0; i < ARRAY_SIZE(map); i++)
{
if (strcmp(localeName, map[i].language) == 0 || strcmp(localeName, map[i].localeFallback) == 0)
{
return i;
}
}
// if language is xx_XX, cut the _XX part for short language
delim = strchr(localeName, '_');
if (delim)
{
*delim = '\0';
for (i = 0; i < ARRAY_SIZE(map); i++)
{
if (strcmp(localeName, map[i].language) == 0 || strcmp(localeName, map[i].localeFallback) == 0)
{
return i;
}
}
}
return -1;
}
static unsigned int getUnixSystemLanguage()
{
unsigned int ret = getMatchingLanguage(unixSystemLANGUAGE);
if (ret != -1)
{
return ret;
}
return getMatchingLanguage(unixSystemLANG);
}
#endif

#if defined(ENABLE_NLS)
# if defined(WZ_OS_WIN)
Expand Down Expand Up @@ -440,6 +491,14 @@ bool setLanguage(const char *language)
# elif defined(__EMSCRIPTEN__)
return setLocaleEmscripten(map[i].localeFilename);
# else
if (i == 0)
{
unsigned int detectedI = getUnixSystemLanguage();
if (detectedI != -1)
{
return setLocaleUnix(map[detectedI].locale) || setLocaleUnix(map[detectedI].localeFallback) || setLocaleUnix_LANGUAGEFallback(map[detectedI].localeFilename);
}
}
return setLocaleUnix(map[i].locale) || setLocaleUnix(map[i].localeFallback) || setLocaleUnix_LANGUAGEFallback(map[i].localeFilename);
# endif
}
Expand Down Expand Up @@ -680,6 +739,11 @@ void initI18n()
// Should come *after* bindTextDomain
canUseLANGUAGEEnvVar = checkSupportsLANGUAGEenvVarOverride();

#if !defined(__EMSCRIPTEN__) && !defined(WZ_OS_WIN)
unixSystemLANGUAGE = getenv("LANGUAGE");
unixSystemLANG = getenv("LANG");
#endif

if (!setLanguage(defaultLanguage.c_str())) // set to system default
{
// no system default?
Expand Down

0 comments on commit 38b625c

Please sign in to comment.