Use `LocaleListCompat.getDefault()` to get system locales list

and swapped out `ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())`
This commit is contained in:
Ray c 2024-04-10 23:23:00 +00:00
parent 9271c51966
commit d3b22f517e
2 changed files with 7 additions and 59 deletions

View File

@ -50,7 +50,6 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.core.os.ConfigurationCompat;
import androidx.core.os.LocaleListCompat;
import com.bumptech.glide.Glide;

View File

@ -75,64 +75,13 @@ public class App implements Comparable<App>, Parcelable {
public static LocaleListCompat getLocales() {
LocaleListCompat cached = systemLocaleList;
if (cached == null) {
/**
* 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());
// Tries to get the device locales list set by the user in system settings.
// The official docs are less than apparent in this regard, but empirically,
// `ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration())`
// seems to push back languages marked "May not be available in some apps"
// in Settings UI while `LocaleListCompat.getDefault()` appears to preserve
// the user-preferred order so we prefer the latter here
cached = LocaleListCompat.getDefault();
systemLocaleList = cached;
}
return cached;