改变编程语言环境中不工作的一些设备中不、编程语言、环境、设备

2023-09-06 02:34:32 作者:不离不弃

我有下面这段code:

I've the following piece of code:

/**
 * Sets a new Locale for the APP.
 * @param newLocale - Valid new locale.
 */
private static void setLocale( String newLocale )
{
    Locale locale = new Locale( newLocale );
    Locale.setDefault( locale );

    Configuration config = new Configuration();
    config.locale = locale;

    context.getResources().updateConfiguration( config, context.getResources().getDisplayMetrics() );
}

简单。

然而,当我在一个智能手机(4.1.1)中运行它,它工作得很好。该设备以匹配该语言变化的字符串。

However when I run it in a Smartphone (4.1.1), it does work flawlessly. The device changes Strings in order to match the language.

但随着片剂(4.3),这是行不通的。如果我的输出是这样的:

But with a tablet (4.3), it doesn't work. If I output something like:

Log.d("TAG",Locale.getDefault());

该地点似乎在两台设备上进行更改,但正如我所说,字符串不会被翻译成正确的语言。

The Locale seems to be changed on both devices, but as I said, Strings doesn't get translated to the correct language.

我已经做了很多的调试,我已经发现了对象之间的区别: 退房的配置对象4.1.1:

I've done a lot of debugging, and I've spotted a difference between objects: Check out the Configuration object on 4.1.1:

和检查出的配置对象上的平板电脑(4.3)

And check out the Configuration Object on the tablet (4.3)

正如你所看到的,唯一的显着区别是 userSetLocale 被设置为False的平板电脑。

As you can see, the only notable difference is the userSetLocale which is set to False on tablet.

所以,我检查了谷歌来源$ C ​​$ C(https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/res/Configuration.java),它规定:

So I checked Google SourceCode (https://github.com/android/platform_frameworks_base/blob/master/core/java/android/content/res/Configuration.java), and it states:

 /**
 * Locale should persist on setting.  This is hidden because it is really
 * questionable whether this is the right way to expose the functionality.
 * @hide
 */
public boolean userSetLocale;

看起来这是影响我。因此,正如我不能访问这个值,也不由的getter / setter也不是由公众访问,我用反射,以改变它。

Looks like this is affecting me. So as I can't access this value, nor by getter / setter nor by public access, I used reflection in order to change it.

不过,改变它通过反射,即使我已经看到它在内部改变(布尔值设置为反射后假)后,同样的问题仍然是最多。

However, after changing it by reflection, even though I've seen that it internally changed (boolean is set to false after reflection), same issue is still up.

难道你们有什么秘诀?

同时我会继续测试。

感谢。 测试:

的Nexus 10 - 4.4.2 - 确定 的Nexus 5 - 4.4.2 - 确定 在平板320 dpi的 - 4.4.2 - 确定 在智能手机480 dpi的 - 4.3 - 确定 在智能手机160 dpi的 - 4.1.1 - 确定 在平板160 dpi的 - 4.3 - 不会确定 在智能手机320 dpi的 - 4.1.1 - 确定 Nexus 10 - 4.4.2 - OK Nexus 5 - 4.4.2 - OK Tablet 320 dpi - 4.4.2 - OK SmartPhone 480 dpi - 4.3 - OK SmartPhone 160 dpi - 4.1.1 - OK Tablet 160 dpi - 4.3 - NOT OK SmartPhone 320 dpi - 4.1.1 - OK

推荐答案

嗯,

用户SC爵士提供了合作的答案。但我的code,还曾。我们所面临的问题,是关于一个单一的设备忽略这一区域的变化。所以AFAIK,它只是一个单一的设备,它可能会因为一个越野车的ROM引起的,因为它是一个Genymotion仿真器。

User Sir SC provided an answer which worked. But my code, also worked. The issue we were facing, was about a single device ignoring this locale change. So AFAIK, it's just a single device, and it might be caused because of a buggy ROM, as it was a Genymotion Emulator.

所以,总体来说,答案是:

So overall, the answer is:

的code OP公布,是有效的和工作。因此,从爵士SC的code。

干杯。

 
精彩推荐
图片推荐