Skip to content Skip to sidebar Skip to footer

Get Only The List Of Locales That Are Shipped With My Android Applicaiton

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

Solution 1:

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;

}


Post a Comment for "Get Only The List Of Locales That Are Shipped With My Android Applicaiton"