diff --git a/android/app/src/main/java/com/cleverraven/cataclysmdda/CataclysmDDA.java b/android/app/src/main/java/com/cleverraven/cataclysmdda/CataclysmDDA.java index ecd31b35194da..7be201bf01560 100755 --- a/android/app/src/main/java/com/cleverraven/cataclysmdda/CataclysmDDA.java +++ b/android/app/src/main/java/com/cleverraven/cataclysmdda/CataclysmDDA.java @@ -57,4 +57,17 @@ public void run() { public boolean getDefaultSetting(final String settingsName, boolean defaultValue) { return PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(settingsName, defaultValue); } + + public String getSystemLang() { + String language = getResources().getConfiguration().locale.toString(); + // remove suffix we are not interested in if it exists, eg. "en_US_WIN" -> "en_US" + int first_underscore = language.indexOf("_"); + if(first_underscore != -1) { + int second_underscore = language.indexOf("_", first_underscore + 1); + if(second_underscore != -1) { + language = language.substring(0, second_underscore); + } + } + return language; + } } diff --git a/src/translations.cpp b/src/translations.cpp index 96d3787b21c32..30e477c2d1a5b 100644 --- a/src/translations.cpp +++ b/src/translations.cpp @@ -41,6 +41,12 @@ std::string getOSXSystemLang(); #endif +#if defined(__ANDROID__) +#include +#include "sdl_wrappers.h" // for SDL_AndroidGetJNIEnv() +std::string getAndroidSystemLang(); +#endif + // Names depend on the language settings. They are loaded from different files // based on the currently used language. If that changes, we have to reload the // names. @@ -205,15 +211,19 @@ std::string locale_dir() void set_language() { - std::string win_or_mac_lang; + std::string system_lang; #if defined(_WIN32) - win_or_mac_lang = getLangFromLCID( GetUserDefaultLCID() ); -#endif -#if defined(MACOSX) - win_or_mac_lang = getOSXSystemLang(); + system_lang = getLangFromLCID( GetUserDefaultLCID() ); +#elif defined(MACOSX) + system_lang = getOSXSystemLang(); +#elif defined(__ANDROID__) + system_lang = getAndroidSystemLang(); #endif + if( !isValidLanguage( system_lang ) ) { + system_lang = std::string(); + } // Step 1. Setup locale settings. - std::string lang_opt = get_option( "USE_LANG" ).empty() ? win_or_mac_lang : + std::string lang_opt = get_option( "USE_LANG" ).empty() ? system_lang : get_option( "USE_LANG" ); if( !lang_opt.empty() ) { // Not 'System Language' @@ -323,6 +333,27 @@ std::string getOSXSystemLang() } #endif +#if defined(__ANDROID__) +std::string getAndroidSystemLang() +{ + JNIEnv *env = ( JNIEnv * )SDL_AndroidGetJNIEnv(); + jobject activity = ( jobject )SDL_AndroidGetActivity(); + jclass clazz( env->GetObjectClass( activity ) ); + jmethodID method_id = env->GetMethodID( clazz, "getSystemLang", "()Ljava/lang/String;" ); + jstring ans = ( jstring )env->CallObjectMethod( activity, method_id, 0 ); + const char *ans_c_str = env->GetStringUTFChars( ans, 0 ); + if( ans_c_str == nullptr ) { + // fail-safe if retrieving Java string failed + return std::string(); + } + std::string lang( ans_c_str ); + env->ReleaseStringUTFChars( ans, ans_c_str ); + env->DeleteLocalRef( activity ); + env->DeleteLocalRef( clazz ); + return lang; +} +#endif + #else // !LOCALIZE std::string locale_dir()