Improve method of enumerating available locales (#374)

* Change method of enumerating translated locales
This commit is contained in:
0140454 2018-08-02 16:51:10 +08:00 committed by m2049r
parent 03efedf35c
commit ae75a34977
17 changed files with 39 additions and 37 deletions

View File

@ -45,8 +45,17 @@ android {
// Map for the version code that gives each ABI a value.
def abiCodes = ['armeabi-v7a': 1, 'arm64-v8a': 2, 'x86': 3, 'x86_64': 4]
// Enumerate translated locales
def availableLocales = ["en"]
new File("app/src/main/res/").eachFileMatch(~/^values-.*/) { file ->
def languageTag = file.name.substring(7).replace("-r", "-")
availableLocales.add(languageTag)
}
// APKs for the same app that all have the same version information.
android.applicationVariants.all { variant ->
// Update string resource: available_locales
variant.resValue("string", "available_locales", availableLocales.join(","))
// Assigns a different version code for each output APK.
variant.outputs.all {
output ->

View File

@ -966,29 +966,29 @@ public class LoginActivity extends SecureActivity
}
public void onChangeLocale() {
final ArrayList<Locale> translatedLocales = LocaleHelper.getAvailableLocales(LoginActivity.this);
String[] localeDisplayName = new String[1 + translatedLocales.size()];
final ArrayList<Locale> availableLocales = LocaleHelper.getAvailableLocales(LoginActivity.this);
String[] localeDisplayName = new String[1 + availableLocales.size()];
Collections.sort(translatedLocales, new Comparator<Locale>() {
Collections.sort(availableLocales, new Comparator<Locale>() {
@Override
public int compare(Locale locale1, Locale locale2) {
String localeString1 = LocaleHelper.getLocaleString(LoginActivity.this, locale1, LocaleHelper.COMPARED_RESOURCE_ID);
String localeString2 = LocaleHelper.getLocaleString(LoginActivity.this, locale2, LocaleHelper.COMPARED_RESOURCE_ID);
String localeString1 = LocaleHelper.getDisplayName(locale1, true);
String localeString2 = LocaleHelper.getDisplayName(locale2, true);
return localeString1.compareTo(localeString2);
}
});
localeDisplayName[0] = getString(R.string.language_system_default);
for (int i = 1; i < localeDisplayName.length; i++) {
Locale locale = translatedLocales.get(i - 1);
localeDisplayName[i] = LocaleHelper.getLocaleString(LoginActivity.this, locale, LocaleHelper.COMPARED_RESOURCE_ID);
Locale locale = availableLocales.get(i - 1);
localeDisplayName[i] = LocaleHelper.getDisplayName(locale, true);
}
int currentLocaleIndex = 0;
String currentLocaleName = LocaleHelper.getLocale(LoginActivity.this);
if (!currentLocaleName.isEmpty()) {
Locale currentLocale = Locale.forLanguageTag(currentLocaleName);
String currentLocalizedString = LocaleHelper.getLocaleString(LoginActivity.this, currentLocale, LocaleHelper.COMPARED_RESOURCE_ID);
String currentLocalizedString = LocaleHelper.getDisplayName(currentLocale, true);
currentLocaleIndex = Arrays.asList(localeDisplayName).indexOf(currentLocalizedString);
}
@ -1000,7 +1000,7 @@ public class LoginActivity extends SecureActivity
dialog.dismiss();
LocaleHelper.setLocale(LoginActivity.this,
(i == 0) ? "" : translatedLocales.get(i - 1).toLanguageTag());
(i == 0) ? "" : availableLocales.get(i - 1).toLanguageTag());
startActivity(getIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK));
}
});

View File

@ -12,27 +12,30 @@ import java.util.HashSet;
import java.util.Locale;
public class LocaleHelper {
public static final int COMPARED_RESOURCE_ID = R.string.language;
private static final String PREFERRED_LOCALE_KEY = "preferred_locale";
private static Locale SYSTEM_DEFAULT_LOCALE = Locale.getDefault();
public static ArrayList<Locale> getAvailableLocales(Context context) {
ArrayList<Locale> locales = new ArrayList<>();
HashSet<String> localizedStrings = new HashSet<>();
String[] availableLocales = context.getString(R.string.available_locales).split(",");
for (String localeName : context.getAssets().getLocales()) {
Locale locale = Locale.forLanguageTag(localeName);
String localizedString = getLocaleString(context, locale, COMPARED_RESOURCE_ID);
if (localizedStrings.add(localizedString)) {
locales.add(locale);
}
for (String localeName : availableLocales) {
locales.add(Locale.forLanguageTag(localeName));
}
return locales;
}
public static String getDisplayName(Locale locale, boolean sentenceCase) {
String displayName = locale.getDisplayName(locale);
if (sentenceCase) {
displayName = toSentenceCase(displayName, locale);
}
return displayName;
}
public static String getLocale(Context context) {
return getPreferredLocale(context);
}
@ -55,10 +58,14 @@ public class LocaleHelper {
SYSTEM_DEFAULT_LOCALE = locale;
}
public static String getLocaleString(Context context, Locale locale, int resId) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
return context.createConfigurationContext(configuration).getString(resId);
private static String toSentenceCase(String str, Locale locale) {
if (str.isEmpty()) {
return str;
}
int firstCodePointLen = str.offsetByCodePoints(0, 1);
return str.substring(0, firstCodePointLen).toUpperCase(locale)
+ str.substring(firstCodePointLen);
}
private static String getPreferredLocale(Context context) {

View File

@ -319,7 +319,6 @@
<string name="tx_subaddress">Subadresse #%1$d</string>
<string name="generate_address_label_sub">Öffentliche Subadresse #%1$d</string>
<string name="language">Deutsch</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -318,7 +318,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Ελληνικά</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -304,7 +304,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Español</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -322,7 +322,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Français</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -320,7 +320,6 @@
<string name="tx_subaddress">Alcím #%1$d</string>
<string name="generate_address_label_sub">Nyilvános alcím #%1$d</string>
<string name="language">Magyar</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -320,7 +320,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Subaddress pubblico #%1$d</string>
<string name="language">Italiano</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -318,7 +318,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Norsk bokmål</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -322,7 +322,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Português</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -318,7 +318,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Română</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -321,7 +321,6 @@
<string name="tx_subaddress">Субадрес #%1$d</string>
<string name="generate_address_label_sub">Публичный субадрес #%1$d</string>
<string name="language">Русский</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -303,7 +303,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">Svenska</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>

View File

@ -316,7 +316,6 @@
<string name="tx_subaddress">附属地址 #%1$d</string>
<string name="generate_address_label_sub">附属公开地址 #%1$d</string>
<string name="language">中文(中国)</string>
<string name="menu_language">语言</string>
<string name="language_system_default">使用系统语言</string>
</resources>

View File

@ -317,7 +317,6 @@
<string name="tx_subaddress">子地址 #%1$d</string>
<string name="generate_address_label_sub">公開子地址 #%1$d</string>
<string name="language">中文(台灣)</string>
<string name="menu_language">語言</string>
<string name="language_system_default">使用系統語言</string>
</resources>

View File

@ -366,7 +366,6 @@
<string name="tx_subaddress">Subaddress #%1$d</string>
<string name="generate_address_label_sub">Public Subaddress #%1$d</string>
<string name="language">English</string>
<string name="menu_language">Language</string>
<string name="language_system_default">Use System Language</string>
</resources>