Skip to content

Commit

Permalink
Auto detect locale on desktop (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonflylee authored Nov 28, 2024
1 parent 2acd185 commit 4a275c4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ int main(int argc, char* argv[])
}
}

brls::Platform::APP_LOCALE_DEFAULT = brls::LOCALE_AUTO;

// Init the app and i18n
if (!brls::Application::init())
{
Expand Down
9 changes: 9 additions & 0 deletions library/lib/platforms/desktop/desktop_darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,13 @@ bool darwin_runloop(const std::function<bool()>& runLoopImpl) {
}
}

std::string darwin_locale() {
@autoreleasepool {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
NSString *current = [languages objectAtIndex:0];
return [current UTF8String];
}
}

}
1 change: 0 additions & 1 deletion library/lib/platforms/desktop/desktop_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ DesktopPlatform::DesktopPlatform()
{
char* langEnv = getenv("BOREALIS_LANG");
this->locale = langEnv ? std::string(langEnv) : LOCALE_DEFAULT;
brls::Logger::info("Auto set app locale: {}", this->locale);
}
else
{
Expand Down
52 changes: 52 additions & 0 deletions library/lib/platforms/glfw/glfw_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ extern "C"
namespace brls
{

#if defined(__APPLE__)
std::string darwin_locale();
#endif

static void glfwErrorCallback(int errorCode, const char* description)
{
switch (errorCode)
Expand Down Expand Up @@ -67,6 +71,54 @@ GLFWPlatform::GLFWPlatform()
// Misc
glfwSetTime(0.0);

// override local
if (Platform::APP_LOCALE_DEFAULT == LOCALE_AUTO)
{
#if defined(_WIN32)
LANGID lang = GetUserDefaultLangID();
switch (PRIMARYLANGID(lang)) {
case LANG_CHINESE: this->locale = (SUBLANGID(lang) == SUBLANG_CHINESE_SIMPLIFIED) ? LOCALE_ZH_HANS : LOCALE_ZH_HANT; break;
case LANG_ENGLISH: this->locale = (SUBLANGID(lang) == SUBLANG_ENGLISH_UK) ? LOCALE_EN_GB : LOCALE_EN_US; break;
case LANG_FRENCH: this->locale = (SUBLANGID(lang) == SUBLANG_FRENCH_CANADIAN) ? LOCALE_FR_CA : LOCALE_FR; break;
case LANG_PORTUGUESE: this->locale = (SUBLANGID(lang) == SUBLANG_PORTUGUESE_BRAZILIAN) ? LOCALE_PT_BR : LOCALE_PT; break;
case LANG_JAPANESE: this->locale = LOCALE_JA; break;
case LANG_GERMAN: this->locale = LOCALE_DE; break;
case LANG_ITALIAN: this->locale = LOCALE_IT; break;
case LANG_SPANISH: this->locale = LOCALE_ES; break;
case LANG_KOREAN: this->locale = LOCALE_Ko; break;
case LANG_DUTCH: this->locale = LOCALE_NL; break;
case LANG_RUSSIAN: this->locale = LOCALE_RU; break;
default:;
}
#elif defined(__APPLE__)
std::string lang = darwin_locale();
auto pos = lang.find_last_of('-');
this->locale = lang.substr(0, pos);
#elif defined(__linux__)
char* langEnv = getenv("LANG");
/* fallback languages */
if (!langEnv) langEnv = getenv("LANGUAGE");
if (langEnv) {
std::unordered_map<std::string, std::string> lang2brls = {
{ "zh_CN", LOCALE_ZH_HANS },
{ "zh_TW", LOCALE_ZH_HANT },
{ "ja_JP", LOCALE_JA },
{ "ko_KR", LOCALE_Ko },
{ "it_IT", LOCALE_IT }
};
char *ptr = strchr(langEnv, '.');
if (ptr) *ptr = '\0';

ptr = strchr(langEnv, '@');
if (ptr) *ptr = '\0';

std::string lang = langEnv;
this->locale = lang2brls.count(lang) ? lang2brls[lang] : LOCALE_EN_US;
}
#endif
brls::Logger::info("Auto detect app locale: {}", this->locale);
}

// Platform impls
this->audioPlayer = new NullAudioPlayer();
}
Expand Down

0 comments on commit 4a275c4

Please sign in to comment.