【问题标题】:Change language programatically in Android with country code使用国家/地区代码在 Android 中以编程方式更改语言
【发布时间】:2016-06-05 05:19:21
【问题描述】:

我想将我的应用区域设置更改为 pt_BR。我做了以下事情。

更改语言环境的代码:

Locale locale;
if (localeName.contains("_")) {
           String localNameArray[] = localeName.split("_");
           locale = new Locale(localNameArray[0], localNameArray[1]);
} else {
       locale = new Locale(localeName);
}

Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);

字符串 localeName 包含 pt_BR。

创建了一个名为 values-pt-rBR 的新文件夹,并在此文件夹中添加了 strings.xml 文件。

但是,当我将应用的语言更改为巴西葡萄牙语 (pt_BR) 时,更改不会得到反映。

我已经检查了以下链接,但在这里找不到任何解决方案:

Change language programatically in Android with country

Setting application locale to pt_BR programmatically

【问题讨论】:

  • 您是否尝试在语言环境更改后获取资源?
  • 是的,我尝试在 android 应用程序中更改语言环境后获取字符串,但它返回英语字符串。
  • 这是否适用于另一种语言(无区域)?还尝试将位置设置为 getResources().getConfiguration() 而不是创建新的。
  • 是的,它适用于其他语言。好的,我会尝试建议的解决方案。
  • 我尝试过使用 getResources().getConfiguration() 解决方案,但它也不起作用。如果有人有这些工作,请发布解决方案。

标签: android locale


【解决方案1】:

字符串文件:values/string.xml 和 values-pt-rBr/string.xml

        setLocale(new Locale("en"));
        String eng = getString(R.string.hello_world);
        setLocale(new Locale("pt", "Br"));
        String bra = getString(R.string.hello_world);
        if (!eng.equals(bra)) {
            Log.i("locale_test", "it works!");
        }


public void setLocale(final Locale locale) {
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Locale.setDefault(locale);
    android.content.res.Configuration conf = res.getConfiguration();
    conf.locale = locale;
    res.updateConfiguration(conf, dm);
}

更新: 从 android N 开始需要新方法
创建 ContextWrapper 类并在活动的 attachBaseContext(Context) 方法中使用它

public class ContextWrapper extends android.content.ContextWrapper {

 public ContextWrapper(final Context base) {
    super(base);
 }

 public static ContextWrapper wrap(Context context, Locale newLocale) {

    Resources res = context.getResources();
    Configuration configuration = res.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);

        LocaleList localeList = new LocaleList(newLocale);
        LocaleList.setDefault(localeList);
        configuration.setLocales(localeList);
    } else {
        configuration.setLocale(newLocale);
        DisplayMetrics dm = res.getDisplayMetrics();
        res.updateConfiguration(configuration, dm);
    }
    configuration.setLayoutDirection(newLocale);
    context = context.createConfigurationContext(configuration);

    return new ContextWrapper(context);
 }

}

@Override protected void attachBaseContext(final Context newBase) {
    String savedLocale = LocaleUtils.getSavedLocale();
    if (isNotEmpty(savedLocale)) {
        Locale appLocale = new Locale(savedLocale);
        ContextWrapper wrapped = ContextWrapper.wrap(newBase, appLocale);
        super.attachBaseContext(wrapped);
    } else {
        super.attachBaseContext(newBase);
    }
}

【讨论】:

  • 什么是 SalonyFormatter 类?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多