仅获取我的Android应用程序随附的语言环境列表 [英] Get ONLY the list of locales that are shipped with my Android applicaiton

查看:61
本文介绍了仅获取我的Android应用程序随附的语言环境列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

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

但是我想要应用程序附带的列表w/,以便我可以将其与手机支持的列表进行比较,以根据手机支持和应用程序支持来创建子列表.

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.

很明显,我可以运送w/包含我支持的语言列表的文件,但这不是我想采用的途径.

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.

推荐答案

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;

}

这篇关于仅获取我的Android应用程序随附的语言环境列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆