【问题标题】:Get string from default locale using string in specific locale使用特定语言环境中的字符串从默认语言环境获取字符串
【发布时间】:2011-09-25 10:53:53
【问题描述】:

好的,我知道标题听起来很疯狂:)

这就是我想要的。我的应用程序针对设备用户进行了本地化,但我发送回服务器的信息必须全部为英文。我的默认应用语言环境英语。

例如,我有数组:

  • 苹果
  • 橙子
  • 桃子

我已经本地化了数组:

  • Яблоки
  • Апельсины
  • Персики

当俄罗斯用户看到列表并选择几个项目时 - 我需要获取相应的英文版本。

我想我的答案归结为如何做 getString() 和传递语言环境? 或者如何在特定语言环境中获取 Array?

【问题讨论】:

    标签: android localization


    【解决方案1】:

    你必须用别的东西来识别元素,比如 id,甚至是英文名称。

    无法获取给定本地化字符串的原始字符串。原因之一是字符串的本地化不是传递函数,但还有很多其他原因导致这不是一个好的方向。

    【讨论】:

      【解决方案2】:

      即使设备上的默认语言环境不同,以下代码也会检索波兰语的本地化字符串:

      Configuration conf = getResources().getConfiguration();
      conf.locale = new Locale("pl");
      DisplayMetrics metrics = new DisplayMetrics();
      getWindowManager().getDefaultDisplay().getMetrics(metrics);
      Resources resources = new Resources(getAssets(), metrics, conf);
      /* get localized string */
      String str = resources.getString(R.string.hello);
      

      我希望这也适用于数组等其他资源类型,因此将“pl”替换为“en”就足够了...

      【讨论】:

      • 我真的不认为这样操作本地化是个好方法。如果我理解正确,我需要确保在所有其他本地化内容没有开始变得疯狂之前将视图切换回正确的语言......
      • @katit 我没有测试过,但是如果你继续在你的上下文对象上调用getResources(),而不是使用为给定语言环境创建的资源,它应该总是返回正确本地化的资源当前设备区域设置,您不必切换回来,我希望我能正确理解您的担忧
      • 另外,为了避免修改您当前的配置,请像这样:Configuration conf = new Configuration(getResources().getConfiguration());
      • 这将改变您的全局设置,因为新配置将注入您使用 getAssets() 获取的 AssetManager。您可能希望在获取字符串后恢复设置(当您这样做时,更改现有配置可能比创建新资源更容易)
      【解决方案3】:

      如果您的设备没有您要强制的语言环境,则此“强制”本地化将不起作用。

      无法证明这一点“就像这样”,但刚才我正在努力解决这样的问题,我正在强制资源处于语言环境/语言女巫未安装在设备上(在 android 2.3.3 上),并且仍然从 values-en (english) 资源中获取字符串。

      在其他设备上,使用您强制的区域设置(但不必设置为当前区域设置),您将获得正确的资源。

      【讨论】:

        【解决方案4】:

        我相信,这是安全的方法(无需触及当前配置):

        Configuration conf = new Configuration();
        conf.setToDefaults();   // That will set conf.locale to null (current locale)
        // We don't want current locale, so modify it
        conf.locale = new Locale("");   // Or conf.locale = Locale.ROOT for API 9+
                                        // or conf.setLocale(Locale.ROOT) for API 14+
        // Since we need only strings, we can safely set metrics to null
        Resources rsrcDflt = new Resources(getAssets(), null, conf);
        String apples = srcDflt.getString(R.string.apples); // Got it at last!
        

        【讨论】:

        • 实际上构造函数 new Resources(getAssets(), null, conf) 会覆盖您 APK 的系统配置。因此,在创建该对象后,您将始终获得默认版本的字符串。
        【解决方案5】:

        如果您使用 JB 2.2.x 或更高版本(基本上 API >= 17),您可以使用 createConfigurationContext 做:

        public String translate(Locale locale, int resId) {  
            Configuration config = new Configuration(context.getResources().getConfiguration()); 
            config.setLocale(locale);   
            return context.createConfigurationContext(config).getText(resId);
        }
        

        【讨论】:

          【解决方案6】:

          我在保存“状态名称”和“体型”首选项的 ListPreference 中遇到了同样的问题。 ListPreference 从保存在本地化“值”文件夹中的数组中获取其值和条目。 列表值相同(英语),但条目已本地化(英语和希伯来语)。

          我终于用了下面的代码:

          public void OnSharedPreferenceChanged (SharedPreferences sharedPreferences, String key)
          {
              SharedPreferences.Editor editor = sharedPreferences.edit();
              Preference pref = settings.findPreference(key);
              pref.setSummary(sharedPreferences.getString(key, ""));
          
              if (key.equalsIgnoreCase(PREF_STATE) 
                  || key.equalsIgnoreCase(PREF_BODY_FIGURE))
              {
                  editor.putString(key, ((ListPreference) pref).getValue());
                  editor.commit();
              }
          }
          

          这样,首选项始终向用户显示本地化字符串(作为摘要),但将英文字符串保存在最终发送到服务器的首选项内存中。

          【讨论】:

            【解决方案7】:

            这个没有副作用:API17及更高

            Configuration config = new Configuration(context.getResources().getConfiguration()); 
            config.setLocale(locale);   
            return context.createConfigurationContext(config).getResources();
            

            【讨论】:

            • 这对我有用,谢谢!注意:要支持confg.locale = locale;,然后创建一个新的Resources对象:new Resources(context.getAssets(), context.getResources.getDisplayMetrics(), configuration);
            【解决方案8】:

            我结合了在所有 Android 版本上工作所需的两种方法。 See my answer here.

            【讨论】:

              猜你喜欢
              • 2010-09-23
              • 2013-09-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多