【问题标题】:Chage the default language of an Android app更改 Android 应用的默认语言
【发布时间】:2018-08-27 14:26:15
【问题描述】:

我正在开发一个只有捷克语内容的应用程序。如果我正确理解了 Android 文档,那么每个 Android 应用程序的默认语言都是英语。但这对我不起作用,因为我使用的数量字符串在捷克语中有不同的语法规则,所以复数不能正常工作。我需要一种方法来强制将默认字符串资源的语言环境设置为cs 来修复它。我知道我可以为捷克语言环境创建一个新的资源 XML 并覆盖那里的字符串,但我想解决这个问题,因为那样我会在两个不同的资源文件中拥有相同的字符串。 (这也会使复数在非捷克设备上再次被打破。)

感谢您的任何建议。

【问题讨论】:

  • 实际上默认值可以是任何语言。值文件的本地化意味着,如果设备上的活动区域设置是那个,它将显示这些值,否则将显示默认值。所以把你的捷克语字符串放在默认的 values.xml 中,你会没事的。
  • 对于复数规则,一、零、多、其他选项不够?
  • @TibiG in Czech 我需要三个不同的字符串:一个、少数 (2-4)、其他
  • 在英文中很少适用于不同的区间?
  • @TibiG few 变体在默认 strings.xml 中被完全忽略(other 的值甚至用于数量 2-4),所以我想英语根本不使用它

标签: android localization


【解决方案1】:
 // Change locale settings in the app.
public static void setLocale(Context context, String languageCode) {
    Resources resources = context.getResources();
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    android.content.res.Configuration configuration = resources.getConfiguration();
    configuration.locale = new Locale(languageCode.toLowerCase());
    resources.updateConfiguration(configuration, displayMetrics);
}

在任何地方调用该方法,例如:

setLocale(this, ENGLISH_CODE);

【讨论】:

    【解决方案2】:

    我终于找到了解决方案,尽管它更像是一种解决方法。我使用了gunhansancarʼs LocaleHelper 的一部分,并在每个活动中覆盖了attachBaseContext() 方法:

    public class LocaleHelper {
        public static Context onAttach(Context context, String lang) {
            return setLocale(context, lang);
        }
    
        private static Context setLocale(Context context, String language) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return updateResources(context, language);
            }
            return updateResourcesLegacy(context, language);
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        private static Context updateResources(Context context, String language) {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            Configuration configuration = context.getResources().getConfiguration();
            configuration.setLocale(locale);
            configuration.setLayoutDirection(locale);
    
            return context.createConfigurationContext(configuration);
        }
    
        @SuppressWarnings("deprecation")
        private static Context updateResourcesLegacy(Context context, String language) {
            Locale locale = new Locale(language);
            Locale.setDefault(locale);
            Resources resources = context.getResources();
            Configuration configuration = resources.getConfiguration();
            configuration.locale = locale;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLayoutDirection(locale);
            }
            resources.updateConfiguration(configuration, resources.getDisplayMetrics());
            return context;
        }
    }
    
    public class MyActivity extends Activity {
        @Override
        protected void attachBaseContext(Context newBase) {
            super.attachBaseContext(LocaleHelper.onAttach(newBase, "cs"));
        }
    }
    

    现在我的所有捷克语字符串和复数都在默认的 strings.xml 文件中,即使在非捷克语设备上也能正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-08
      相关资源
      最近更新 更多