3

I know there are several ways that I can get the list of all of the Locales supported by the device. Has anyone been able to get the list of locales that you have included in your app?

Using the following code I know I can get the list the device supports:

String[] languages =  getAssets().getLocales();

or

String[] languages = Resources.getSystem().getAssets().getLocales();

But I want the list that is shipped w/ my app so i can compare it against the list that the phone supports to create a subset list depending on phone support and app support.

Obviously I could ship w/ a file that has the list of my supported languages, but this is not a route I want to take.

d3n13d1
  • 200
  • 12

1 Answers1

0
public ArrayList<Locale> getTranslatedLocales(int compairsonStringResourceID, boolean onlyThoseSupportedByThePhone) {

Locale english = Locale.ENGLISH;

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
Locale assignedLanguage = configuration.locale;
ArrayList<Locale> loc = new ArrayList<Locale>();  
ArrayList<String> processed = new ArrayList<String>();
String englishConst = english.getLanguage();
if(onlyThoseSupportedByThePhone) {
    String[] locales =  resources.getAssets().getLocales();
    for (String string : locales) {
        if(!string.startsWith(englishConst) && !processed.contains(string)) {
            processed.add(string);
            loc.add(new Locale(string));
        }
    }
} else {
    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
        String language = locale.getLanguage();
        if(!locale.getLanguage().equals(englishConst) && !processed.contains(language)) {
            processed.add(language);
            loc.add(locale);
        }
    }
}

configuration.locale = english;
Resources res = new Resources(getAssets(), metrics, configuration);
String compareString = res.getString(compairsonStringResourceID);

ArrayList<Locale> supported = new ArrayList<Locale>();
supported.add(english);

for (int i = 0; i < loc.size(); i++) {
    configuration.locale = loc.get(i);
    res = new Resources(getAssets(), metrics, configuration);
    Resources res2 = new Resources(getAssets(), metrics, configuration);
    String s2 = res2.getString(compairsonStringResourceID);

    if(!compareString.equals(s2)){
        supported.add(configuration.locale);
    }
}

configuration.locale = assignedLanguage;
setLocale(assignedLanguage);
return supported;

}

d3n13d1
  • 200
  • 12