Android:在没有“kn”区域设置的手机中显示卡纳达语

Rah*_*ran 5 android locale

我有一台三星 Galaxy y Duo 和一台 Google Nexus 5。我的应用程序在两部手机上运行。我必须以两种语言(卡纳达语和英语)提供此应用程序。我通过切换区域设置来选择语言(按照惯例)。

Nexus 5 的区域设置列表中有区域设置“kn”,而 Y Duos 中没有“kn”。但是当我尝试在values-kn文件夹中使用String.xml时,卡纳达语文本在Y Duos中正确呈现,即使它没有“kn”区域设置。

我有一段代码,用于查找“kn”区域设置并启用/禁用卡纳达语言功能。下面给出。

public boolean isLocalePresent(Locale newLocale) {
    Locale[] locales = Locale.getAvailableLocales();
    for (Locale locale : locales) {
        if (locale.getLanguage().equals(newLocale.getLanguage()) {
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼它;

if (isLocalePresent(new Locale("kn"))) {
    // change default locale to "kn"
}
Run Code Online (Sandbox Code Playgroud)

谁能告诉我如何让我的应用程序在所有可以呈现卡纳达文本的手机中使用卡纳达文本。?即使区域设置列表中不存在“kn”区域设置,我如何知道手机是否支持卡纳达语文本渲染?

小智 1

我有同样的问题。尝试了一些选项,并在我的应用程序中使用以下代码

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        setLocale(new Locale("kn"));
    }

    public void setLocale(Locale newLocale) {
        Locale.setDefault(newLocale);
        android.content.res.Configuration config = new android.content.res.Configuration();
        config.locale = newLocale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都按预期进行。