仅获取我的Android应用程序附带的语言环境列表

时间:2015-09-15 15:07:13

标签: android locale

我知道有几种方法可以获取设备支持的所有语言环境的列表。有没有人能够获得您的应用中包含的语言环境列表?

使用以下代码我知道我可以获得设备支持的列表:

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

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

但我希望我的应用程序发送w/列表,以便我可以将其与手机支持创建子集列表的列表进行比较,具体取决于电话支持和应用支持。

显然,我可以发送w/一个包含我支持的语言列表的文件,但这不是我想要的路线。

**解决方案** 使用评论中引用的帖子这是我的解决方案

    /**
 * Returns the locale's that have been translated.
 * @param compairsonStringResourceID - String in your strings.xml to compare other languages against Default locale of 'en' will be assumed
 * @param onlyThoseSupportedByThePhone - Only return those that are supported by the phone 
 * @return
 */
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;
}

1 个答案:

答案 0 :(得分: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;

}