From ae75a34977a1efb8d37c8e08837138eada705772 Mon Sep 17 00:00:00 2001 From: 0140454 Date: Thu, 2 Aug 2018 16:51:10 +0800 Subject: [PATCH] Improve method of enumerating available locales (#374) * Change method of enumerating translated locales --- app/build.gradle | 9 +++++ .../com/m2049r/xmrwallet/LoginActivity.java | 18 +++++----- .../m2049r/xmrwallet/util/LocaleHelper.java | 35 +++++++++++-------- app/src/main/res/values-de/strings.xml | 1 - app/src/main/res/values-el/strings.xml | 1 - app/src/main/res/values-es/strings.xml | 1 - app/src/main/res/values-fr/strings.xml | 1 - app/src/main/res/values-hu/strings.xml | 1 - app/src/main/res/values-it/strings.xml | 1 - app/src/main/res/values-nb/strings.xml | 1 - app/src/main/res/values-pt/strings.xml | 1 - app/src/main/res/values-ro/strings.xml | 1 - app/src/main/res/values-ru/strings.xml | 1 - app/src/main/res/values-sv/strings.xml | 1 - app/src/main/res/values-zh-rCN/strings.xml | 1 - app/src/main/res/values-zh-rTW/strings.xml | 1 - app/src/main/res/values/strings.xml | 1 - 17 files changed, 39 insertions(+), 37 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d454073e..a55f8824 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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 -> diff --git a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java index b67224b5..d63a424c 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java +++ b/app/src/main/java/com/m2049r/xmrwallet/LoginActivity.java @@ -966,29 +966,29 @@ public class LoginActivity extends SecureActivity } public void onChangeLocale() { - final ArrayList translatedLocales = LocaleHelper.getAvailableLocales(LoginActivity.this); - String[] localeDisplayName = new String[1 + translatedLocales.size()]; + final ArrayList availableLocales = LocaleHelper.getAvailableLocales(LoginActivity.this); + String[] localeDisplayName = new String[1 + availableLocales.size()]; - Collections.sort(translatedLocales, new Comparator() { + Collections.sort(availableLocales, new Comparator() { @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)); } }); diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/LocaleHelper.java b/app/src/main/java/com/m2049r/xmrwallet/util/LocaleHelper.java index bd02b9b6..d54ff26d 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/util/LocaleHelper.java +++ b/app/src/main/java/com/m2049r/xmrwallet/util/LocaleHelper.java @@ -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 getAvailableLocales(Context context) { ArrayList locales = new ArrayList<>(); - HashSet 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) { diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index e9682e63..7e5dd0a5 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -319,7 +319,6 @@ Subadresse #%1$d Öffentliche Subadresse #%1$d - Deutsch Language Use System Language diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 71f25bff..831a1ee3 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -318,7 +318,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Ελληνικά Language Use System Language diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 95c90aab..d331f2c1 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -304,7 +304,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Español Language Use System Language diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 23c446fc..c6ab669f 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -322,7 +322,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Français Language Use System Language diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index 11409b67..60af282b 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -320,7 +320,6 @@ Alcím #%1$d Nyilvános alcím #%1$d - Magyar Language Use System Language diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index 26ccc738..695dabba 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -320,7 +320,6 @@ Subaddress #%1$d Subaddress pubblico #%1$d - Italiano Language Use System Language diff --git a/app/src/main/res/values-nb/strings.xml b/app/src/main/res/values-nb/strings.xml index 1ebc9697..6eb5cccb 100644 --- a/app/src/main/res/values-nb/strings.xml +++ b/app/src/main/res/values-nb/strings.xml @@ -318,7 +318,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Norsk bokmål Language Use System Language diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index 8c34bede..d60b3328 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -322,7 +322,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Português Language Use System Language diff --git a/app/src/main/res/values-ro/strings.xml b/app/src/main/res/values-ro/strings.xml index 33674621..351e1f5d 100644 --- a/app/src/main/res/values-ro/strings.xml +++ b/app/src/main/res/values-ro/strings.xml @@ -318,7 +318,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Română Language Use System Language diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 762a5249..cdb37f53 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -321,7 +321,6 @@ Субадрес #%1$d Публичный субадрес #%1$d - Русский Language Use System Language diff --git a/app/src/main/res/values-sv/strings.xml b/app/src/main/res/values-sv/strings.xml index c8483cce..7fd0c1d4 100644 --- a/app/src/main/res/values-sv/strings.xml +++ b/app/src/main/res/values-sv/strings.xml @@ -303,7 +303,6 @@ Subaddress #%1$d Public Subaddress #%1$d - Svenska Language Use System Language diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index f68741bb..3a49451c 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -316,7 +316,6 @@ 附属地址 #%1$d 附属公开地址 #%1$d - 中文(中国) 语言 使用系统语言 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index a3eacb75..5a331c45 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -317,7 +317,6 @@ 子地址 #%1$d 公開子地址 #%1$d - 中文(台灣) 語言 使用系統語言 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a303b429..d50423c5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -366,7 +366,6 @@ Subaddress #%1$d Public Subaddress #%1$d - English Language Use System Language