Try to resurrect order of less common languages

which seems to be pushed back in `ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()`
This commit is contained in:
Ray c 2024-04-10 05:55:10 +00:00
parent 10920c1f3c
commit 9271c51966
1 changed files with 58 additions and 1 deletions

View File

@ -75,7 +75,64 @@ public class App implements Comparable<App>, Parcelable {
public static LocaleListCompat getLocales() {
LocaleListCompat cached = systemLocaleList;
if (cached == null) {
cached = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
/**
* Tries to get the device locales list set by the user in system settings.
* A common way to get it is via getLocales(Resources.getSystem().getConfiguration()).
* While it reflects the correct order of user's preference most of the time,
* the list obtained this way somehow seems to push back 'less common languages'
* i.e. those bearing the subtitle "May not be available in some apps" in Settings UI
* (which seems to be another way of putting that they are 'untranslated languages'
* for the Android UI itself). While that might make sense in the system context,
* it seems counter-productive for our purpose since it may pre-empt those langauges
* which might otherwise be translated in the repos.
*
* On the other hand, LocaleListCompat.getDefault() seems to return a locales list
* in the correct user-preferred order and appears to be a good replacement.
* Out of an abundance of caution, the remedy here is to use the replacement list
* on a narrow compass where it indeed appears to be the case mentioned above.
*/
LocaleListCompat sysConfigList
= ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
LocaleListCompat defLocaleList = LocaleListCompat.getDefault();
Log.d(TAG, "Getting locales list from system configuration: "
+ sysConfigList.toString());
Log.d(TAG, "Getting default locales list from LocaleListCompat: "
+ defLocaleList.toString());
// We might directly adopt LocaleListCompat.getDefault() instead if further testing
// suggests there is no regression in getting the user's preferred list of languages
cached = sysConfigList;
// For now, only adopt it on the narrow compass there appears to be a 'pushed back' case
if (defLocaleList.size() > 1) {
// Investigate if it's the 'pushed back' case.
// E.g. Given user's list of Langauges in system settings [chr_US,cy_GB,en_US,ja_JP]
// (first two being which "May not be available in some apps",
// the latter two fully translated in Android)
// getLocales(Resources.getSystem().getConfiguration()) : [en_US,chr_US,cy_GB,ja_JP]
// LocaleListCompat.getDefault() : [chr_US,cy_GB,en_US,ja_JP]
int firstPos = defLocaleList.indexOf(sysConfigList.get(0));
int startPos = sysConfigList.indexOf(defLocaleList.get(0));
if (firstPos > 0 && startPos <= firstPos) {
// We might afford the luxury of some digging here since the final locales list
// is cached and we'd be doing it once (until vitiated by the next change).
int i = 0;
for (; i < firstPos; i++) {
if (sysConfigList.get(startPos + i).equals(defLocaleList.get(i))) {
Log.d(TAG, "sysConfigList.get(" + (startPos + i)
+ ") = defLocaleList.get(" + i + "): "
+ defLocaleList.get(i).toString());
continue;
}
break;
}
if (i == firstPos) {
Log.d(TAG, "Locales list from system configuration might have pushed back "
+ (firstPos - startPos + 1)
+ " less common languages, using the other list instead.");
cached = defLocaleList;
}
}
}
Log.d(TAG, "Using locales list: " + cached.toString());
systemLocaleList = cached;
}
return cached;