diff --git a/demo/src/main.cpp b/demo/src/main.cpp index c5b1e0694..1febf882f 100644 --- a/demo/src/main.cpp +++ b/demo/src/main.cpp @@ -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()) { diff --git a/library/lib/platforms/desktop/desktop_darwin.mm b/library/lib/platforms/desktop/desktop_darwin.mm index ce3e4c759..27e00e5e5 100644 --- a/library/lib/platforms/desktop/desktop_darwin.mm +++ b/library/lib/platforms/desktop/desktop_darwin.mm @@ -46,4 +46,13 @@ bool darwin_runloop(const std::function& runLoopImpl) { } } +std::string darwin_locale() { + @autoreleasepool { + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; + NSString *current = [languages objectAtIndex:0]; + return [current UTF8String]; + } +} + } \ No newline at end of file diff --git a/library/lib/platforms/desktop/desktop_platform.cpp b/library/lib/platforms/desktop/desktop_platform.cpp index d5eebaa12..e192b8a18 100644 --- a/library/lib/platforms/desktop/desktop_platform.cpp +++ b/library/lib/platforms/desktop/desktop_platform.cpp @@ -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 { diff --git a/library/lib/platforms/glfw/glfw_platform.cpp b/library/lib/platforms/glfw/glfw_platform.cpp index cfd0d439b..d16b441aa 100644 --- a/library/lib/platforms/glfw/glfw_platform.cpp +++ b/library/lib/platforms/glfw/glfw_platform.cpp @@ -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) @@ -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 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(); }