From 0a52a33f7e9da94502ce1ff706f6014aff601639 Mon Sep 17 00:00:00 2001 From: Wouter Wijsman Date: Thu, 21 Nov 2024 13:38:55 +0100 Subject: [PATCH] Use SDL to get language to use --- src/TranslationManager.cpp | 46 +++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/TranslationManager.cpp b/src/TranslationManager.cpp index 8efa684..2148fed 100644 --- a/src/TranslationManager.cpp +++ b/src/TranslationManager.cpp @@ -16,8 +16,6 @@ #ifdef __PSP__ #include -#else - #include #endif @@ -35,6 +33,7 @@ void TranslationManager::loadTranslations() { for(std::string system_language : getSystemLanguageList()) { + SDL_Log("Checking language %s", system_language.c_str()); tinygettext::Language language = tinygettext::Language::from_name(system_language); if (language) { for (tinygettext::Language l: dictionary_manager.get_languages()) { @@ -53,9 +52,9 @@ void TranslationManager::loadTranslations() { std::vector TranslationManager::getSystemLanguageList() { std::vector locales; - std::string locale = "C"; #ifdef __PSP__ - int current_locale_int; + std::string locale = ""; + int current_locale_int = 0; sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, ¤t_locale_int); switch(current_locale_int) { case PSP_SYSTEMPARAM_LANGUAGE_JAPANESE: @@ -95,24 +94,35 @@ std::vector TranslationManager::getSystemLanguageList() { locale = "zh_TW.UTF-8"; break; } + if (!locale.empty()) { + if (locale.find(".") != std::string::npos) { + locales.push_back(locale.substr(0, locale.find("."))); + } + if (locale.find("_") != std::string::npos) { + locales.push_back(locale.substr(0, locale.find("_"))); + } + } #else - char * locale_c_str = setlocale(LC_ALL, ""); - if (locale_c_str){ - locale = std::string(locale_c_str); - if (strlen(locale_c_str) > 1) { - free(locale_c_str); + SDL_Locale * preferred_locales = SDL_GetPreferredLocales(); + if(preferred_locales) { + for (SDL_Locale * l = preferred_locales; l->language; l++) { + std::string language = std::string(l->language); + std::string country = ""; + if (l->country) { + country = std::string(l->country); + } + std::string char_set = "UTF-8"; + std::string current_locale; + if (country.empty()) { + locales.push_back(language + "." + char_set); + } else { + locales.push_back(language + "_" + country + "." + char_set); + locales.push_back(language + "_" + country); + } + locales.push_back(language); } } #endif - - locales.push_back(locale); - - if (locale.find(".") != std::string::npos) { - locales.push_back(locale.substr(0, locale.find("."))); - } - if (locale.find("_") != std::string::npos) { - locales.push_back(locale.substr(0, locale.find("_"))); - } return locales; }