【问题标题】:Accessing locale specific strings in android layout在 android 布局中访问特定于语言环境的字符串
【发布时间】:2018-04-02 00:44:20
【问题描述】:

我正在尝试使用以下解决方案以编程方式更改 android 上的语言环境:

mConfiguration = new Configuration(getApplicationContext().getResources().getConfiguration());
mConfiguration.setLocale(new Locale("es"));

然后当我想要一个字符串资源时使用这个配置,如下所示:

public static String getString(int id)
{
    return getApplicationContext().createConfigurationContext(mConfiguration).getResources().getString(id);
}

对于我想要使用的每种语言,我确实有单独的文件夹,并且每当我调用上述 getString(R.string.thisPerson) 函数时,我都会获取特定于语言的字符串。我在所有特定于语言环境的 strings.xml 文件中都有 thisPerson 字符串。

但是,每当我直接在布局文件中引用字符串资源时,如下所示:

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:text="@string/thisPerson"
    android:layout_marginRight="10dp"
    />

上述字符串资源不尊重语言环境。我总是得到默认字符串(来自默认的strings.xml 文件),而不是来自当前语言环境的字符串。我可能会遗漏什么,所以我没有在视图布局中获取特定于语言环境的字符串,而是在我调用 getString() 函数时得到它?

【问题讨论】:

    标签: android locale


    【解决方案1】:
    1. 通过选择您要翻译的本地语言,在 res 中创建一个 Value 文件夹
    2. 在此文件夹中创建一个字符串文件并将所有已翻译的字符串添加到其中
    3. 查找您创建的“应用程序”类
    4. 在Application类内部创建Local类的静态实例,并在Application类的onCreate()方法中初始化
    5. 使用下面给出的这个方法来改变语言配置。

      public void setLocale() {

      if (AppClass.appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("") ||
              AppClass.appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equalsIgnoreCase("en")) {
      
          AppClass.appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "en");
          AppClass.myLocale = Locale.US;
      
      } else if (AppClass.appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("de")) {
          AppClass.appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "de");
          AppClass.myLocale = Locale.GERMAN;
      }
      
      if (AppClass.myLocale != null) {
          Locale.setDefault(AppClass.myLocale);
          android.content.res.Configuration configuration = new android.content.res.Configuration();
          configuration.locale = AppClass.myLocale;
          getBaseContext().getResources().updateConfiguration(
                  configuration,
                  getBaseContext().getResources().getDisplayMetrics());
      }
      
      
      Intent i = getBaseContext().getPackageManager()
              .getLaunchIntentForPackage(getBaseContext().getPackageName());
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(i);
      overridePendingTransition(0, 0);
      

      }

    6. 在 onCreate() 方法中的 appclass 中创建以下方法以更改语言,即使在应用程序被杀死并打开后也可以更改语言

      if (appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("") ||
              appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equalsIgnoreCase("en"))
      {
      
          appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "en");
          myLocale =Locale.US;
      
      } else if (appPreferance.getValueFromAppPreferance(AppPreferance.APP_LNG).equals("de")) {
          appPreferance.setValueInAppPreferance(AppPreferance.APP_LNG, "de");
          myLocale = Locale.GERMAN;
      }
      
      if (myLocale != null) {
          Locale.setDefault(myLocale);
          android.content.res.Configuration configuration = new android.content.res.Configuration();
          configuration.locale = myLocale;
          getBaseContext().getResources().updateConfiguration(
                  configuration,
                  getBaseContext().getResources().getDisplayMetrics());
      }
      
    7. 这里的appPreferance是一个全局偏好类的实例,用于全局存储和访问值

    【讨论】:

      【解决方案2】:
      Locale locale = new Locale("ru");
      Locale.setDefault(locale);
      Configuration config = getBaseContext().getResources().getConfiguration();
      config.locale = locale;
      getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());
      

      您可以在here查看更多详情

      更新

      如果您需要支持更高的 API 级别,您可以改用此解决方案: link

      【讨论】:

      • 感谢您的回答。但是,此解决方案仅适用于 API 级别
      • 即使我将 context.createConfigurationContext() 用于更高的 API(如其他答案中所建议的那样),它仍然不起作用。
      • 从我所看到的 updateConfiguration 已被 api 级别 25 弃用
      猜你喜欢
      • 2011-09-25
      • 2010-09-23
      • 2012-07-18
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      相关资源
      最近更新 更多