Skip to content

Commit

Permalink
Retrieve system language on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettDong committed Mar 11, 2021
1 parent 48f45e8 commit 7bc9d40
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
43 changes: 37 additions & 6 deletions src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
std::string getOSXSystemLang();
#endif

#if defined(__ANDROID__)
#include <jni.h>
#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.
Expand Down Expand Up @@ -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<std::string>( "USE_LANG" ).empty() ? win_or_mac_lang :
std::string lang_opt = get_option<std::string>( "USE_LANG" ).empty() ? system_lang :
get_option<std::string>( "USE_LANG" );
if( !lang_opt.empty() ) {
// Not 'System Language'
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 7bc9d40

Please sign in to comment.