为什么会Resources.getString()间歇性地从错误的语言环境返回字符串? [英] Why might Resources.getString() intermittently return strings from the wrong locale?

查看:1152
本文介绍了为什么会Resources.getString()间歇性地从错误的语言环境返回字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序与英文字符串值/ strings.xml中。对于在该文件中的每个字符串,我已经在价值观-JA / strings.xml中以该字符串的日语翻译的条目。如果我设置了仿真器,一个的Nexus One或Nexus S的日本,用户界面​​显示在整个日本的文字。大部分时间

I have an Android application with English strings in values/strings.xml. For each string in that file, I have an entry in values-ja/strings.xml with the Japanese translation of that string. If I set the emulator, a Nexus One or Nexus S to Japanese, the UI shows Japanese text throughout. Most of the time.

有时候,用户界面​​的某些部分将出现在英国,尽管目前的区域设置为JA-JP。

Sometimes, some portion of the UI will appear in English, even though the current locale is ja-JP. For instance, I wrote this test code in the onCreate() method of one of my activities:

Log.e(TAG, "Default locale = '" + Locale.getDefault().toString() + "'");
Log.e(TAG, "My string = '" + getResources().getString(R.string.my_string) + "'");

有时候我会在LogCat中看到:

Sometimes I will see in LogCat:

Default locale is 'ja_JP'
My string is '日本'

其他时候,我会看到:

Other times I will see:

Default locale is 'ja_JP'
My string is 'English'

有时候,这个问题是由旋转手机解决。有时,它是由退出并重新启动应用程序解决。有时只有一个单一屏幕的一部分是英文。有时会发生与通过code拉出资源串这个问题,有时它发生只能由布局引用的字符串。无处在我的应用我称Locale.setDefault(),所以这不是导致问题

Sometimes this issue is resolved by rotating the phone. Sometimes it's resolved by exiting and restarting the app. Sometimes only a portion of a single screen is in English. Sometimes this issue occurs with strings that are pulled out of the resources via code, and sometimes it occurs with strings that are only referenced by a layout. Nowhere in my application do I call Locale.setDefault(), so that's not causing the issue.

更新

我已经找到一种方法来解决此问题的具体活动。在该活动的onCreate():

I've found a way to correct the issue for a specific activity. In that activity's onCreate():

Log.e(TAG, "getString: '" + getString(R.string.my_string) + "'");
Log.e(TAG, "getResources().getConfiguration(): '" +
      getResources().getConfiguration().toString() + "'");
Log.e(TAG, "getResources().getDisplayMetrics(): '" +
      getResources().getDisplayMetrics().toString() + "'");

Log.e(TAG, "Setting configuration to getConfiguration()");
getResources().updateConfiguration(getResources().getConfiguration(),
     getResources().getDisplayMetrics());

Log.e(TAG, "getString: '" + getString(R.string.my_string) + "'");
Log.e(TAG, "getResources().getConfiguration(): '" +
      getResources().getConfiguration().toString() + "'");
Log.e(TAG, "getResources().getDisplayMetrics(): '" +
      getResources().getDisplayMetrics().toString() + "'");

这导致在LogCat中的以下内容:

This results in the following in LogCat:

getString: 'English'
getResources().getConfiguration(): '{ scale=1.0 imsi=0/0 loc=ja_JP touch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=8}'
getResources().getDisplayMetrics(): 'DisplayMetrics{density=1.5, width=480, height=800, scaledDensity=1.5, xdpi=254.0, ydpi=254.0}'
Setting configuration to getConfiguration()
getString: '日本'
getResources().getConfiguration(): '{ scale=1.0 imsi=0/0 loc=ja_JP touch=3 keys=1/1/2 nav=3/1 orien=1 layout=34 uiMode=17 seq=8}'
getResources().getDisplayMetrics(): 'DisplayMetrics{density=1.5, width=480, height=800, scaledDensity=1.5, xdpi=254.0, ydpi=254.0}'

正如你可以从日志,没有在当前配置变化看,但的getString()给出了不同的结果。

As you can see from the log, nothing in the current configuration changes, but getString() gives different results.

这是不切实际的,使用此解决方案在我的应用程序的每个地方的资源可能会被使用,但希望这提供了一个关于什么错误的提示。

It's impractical to use this workaround in every place in my application where a resource might be used, but hopefully this provides a hint about what's going wrong.

推荐答案

这只是一个理论,但的你可能会基本上泄露的上下文。,旧的活动可能会被报告的字符串值,而不是新创建的。

This is just a theory but you could be leaking a Context. Basically, the old activity might be reporting the string values rather than the newly created on.

一个方法来测试,这是:

A way to test this is:

  1. 更改TAG一个成员变量(不是静态的!)。
  2. 在OnCreate中,设置TAG = this.toString(),这将会把活动作为标签的内存地址。
  3. 有活动打印出来使用初始语言环境的东西。
  4. 在做任何更改地区。这应该(永远验证了这一点),重新启动该活动,你会得到一个新的活动。如果你这么做。然后看日志,看看内存地址改变的标记。如果存储器地址相同的上下文被泄露之前。

这篇关于为什么会Resources.getString()间歇性地从错误的语言环境返回字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆