【问题标题】:Get ONLY the list of locales that are shipped with my Android applicaiton仅获取我的 Android 应用程序附带的语言环境列表
【发布时间】:2015-09-15 15:07:13
【问题描述】:

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

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

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

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

但我想要 w/ 我的应用程序提供的列表,以便我可以将其与手机支持的列表进行比较,以根据手机支持和应用程序支持创建子集列表。

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

【问题讨论】:

  • 感谢工作。将发布我修改后的解决方案
  • @d3n13d1 您能否将您的solution 移至答案并接受您自己的答案。所以其他人实际上看到了答案。

标签: android locale


【解决方案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;

}

【讨论】:

    猜你喜欢
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多