Skip to content

Commit

Permalink
Match Android system language by prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettDong committed Mar 13, 2021
1 parent 7bc9d40 commit 2b1c95e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ public boolean getDefaultSetting(final String settingsName, boolean 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;
return getResources().getConfiguration().locale.toLanguageTag().replace('-', '_');
}
}
16 changes: 11 additions & 5 deletions src/translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,6 @@ void set_language()
#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() ? system_lang :
get_option<std::string>( "USE_LANG" );
Expand Down Expand Up @@ -346,11 +343,20 @@ std::string getAndroidSystemLang()
// fail-safe if retrieving Java string failed
return std::string();
}
std::string lang( ans_c_str );
const std::string lang( ans_c_str );
env->ReleaseStringUTFChars( ans, ans_c_str );
env->DeleteLocalRef( activity );
env->DeleteLocalRef( clazz );
return lang;
DebugLog( D_INFO, D_MAIN ) << "Read Android system language: '" << lang << '\'';
const std::vector<options_manager::id_and_option> available_languages =
get_options().get_option( "USE_LANG" ).getItems();
for( const options_manager::id_and_option &available_language : available_languages ) {
if( string_starts_with( lang, available_language.first ) ) {
return available_language.first;
}
}
DebugLog( D_WARNING, D_MAIN ) << "Unrecognised language: '" << lang << "', fallback to English";
return std::string();
}
#endif

Expand Down

0 comments on commit 2b1c95e

Please sign in to comment.