2017-02-28 114 views
1

字符串资源的应用程序支持3种语言:Android的牛轧糖忽略位于RES /值-ZH-RTW

  • 英语
  • 简体中国
  • 中国传统

资源位于分别在目录中:

  • 个RES /值,因为它是文档的相应页面上描述
  • RES /值-ZH-RCN
  • RES /值-ZH-RTW

在预牛轧糖版本的所有工作 - https://developer.android.com/guide/topics/resources/providing-resources.html

当用户将设备切换到任何简体中文语言时,目前在牛轧糖上使用来自values-zh-rCN的资源。但是,当用户将设备语言切换到任何繁体中文应用程序时,仍然使用values-zh-rCN中的值(如果您假定传统资源不存在,看起来像是有效的行为)。最后,如果我从项目中删除目录值zh-rCN,应用程序将完全忽略传统资源并使用默认英文版本。

有没有人遇到过这样的问题,除了向Google报告错误之外,是否有解决方案?

回答

1

好吧,我已经报告过这个bug - https://code.google.com/p/android/issues/detail?id=235561 现在,我已经实现了这种解决方法,也许有人会觉得它有用。
首先,我们需要创建一个BroadcastReciever,将系统语言改变听:

private class LanguageChangeReceiver extends BroadcastReceiver { 

    public static boolean isNougatTraditionalChinese() { 
    return isNougat() && Locale.getDefault().toLanguageTag().contains("zh-Hant"); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (!application.getConfiguration().isLanguageSet()) { 
      if (isNougatTraditionalChinese()) { 
       String[] supportedLangTags = context.getResources().getStringArray(R.array.language_values); 
       application.getConfiguration().setLanguagePreference(supportedLangTags[2]); 
       application.updateLanguage(); 
      } 

     } 
    } 
} 

...应用语言更新中看起来大致这样...

private void updateLanguage(String langTag) { 
    Locale myLocale = L10nUtils.createLocaleByTag(langTag); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    android.content.res.Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
} 

...和在上下文中注册BroadcastReciever ...

context.registerReceiver(new LanguageChangeReceiver(), new IntentFilter(Intent.ACTION_LOCALE_CHANGED)); 
+0

请问您可以指定数组R.array.language_values和createLocaleByTag方法。谢谢! –

相关问题