toUpperCaseLocale()VS与toUpperCase() - 现在瞄准的Andr​​oid 4.2现在、toUpperCase、VS、toUpperCaseLocale

2023-09-04 11:01:19 作者:明知是戲,還入戲太深

由于我的目标一个新的SDK版本的Andr​​oid,我已经收到这条线code警告:

Since I targeted a newer SDK version of Android, I have been getting a warning on this line of code:

return getString(R.string.usertab1).toUpperCase()

当我悬停,它说:

隐式使用默认语言环境是错误的常见原因:使用与toUpperCase(区域)而不是

Implicitly using the default locale is a common source of bugs: Use toUpperCase(Locale) instead.

有谁知道如何删除这个错误?为什么现在它是一个preferred的方式来使用这种方法吗?

Does anyone know how to remove this error? And why is it now a preferred way to use this method?

我得到这样的答案,使用与toUpperCase(区域)但有麻烦实现它。哪里的区域设置对象来自哪里?

I get this is the answer, to use toUpperCase(Locale) but having trouble implementing it. Where does the Locale object come from?

推荐答案

您可以的明确的使用默认的语言环境:

You could explicitly use the default locale:

return getString(R.string.usertab1).toUpperCase(Locale.getDefault());

基本上,你不想要的隐含的允许设备使用默认值,因为这可能意味着你根本无视事实,它可能是一个问题。对于机器可读的内容,你可能要指定一个特定的区域(如 Locale.ENGLISH ),以确保您始终获得你想要的复用性出来的数据。用于显示用户显式指定默认的语言环境应该是不错。

Basically, you don't want to implicitly allow the device to use the default, because this can mean you simply ignored the fact that it could be an issue. For machine-readable content, you may want to specify a specific locale (such as Locale.ENGLISH) to ensure you always get the reusability you want out of the data. For showing the user, explicitly specifying the default locale should be fine.

有关更完整的阅读:

一个常见的​​错误是隐含使用默认的语言环境时产生输出意思是机器可读的。这往往工作在显影剂的测试器件(特别是,因为许多开发人员使用EN_US),但一个装置,它的用户是在一个更复杂的区域上运行时失败。

A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developer's test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.

例如,如果你格式化整数某些地区将使用非ASCII十进制数字。再举一个例子,如果你格式化浮点数某些地区会使用作为小数点和的数字分组。这是正确的人类可读的输出,但可能会造成问题,如果presented到另一台计算机( parseDouble(字符串)无法解析这样的一个数字,例如) 。你也应该警惕的与toLowerCase()与toUpperCase()重载不带走一片区域设置:在土耳其为例,该字符将不会被转换为。这是土耳其的文本(如用户输入)正确的行为,但不适合,比如HTTP头。

For example, if you're formatting integers some locales will use non-ASCII decimal digits. As another example, if you're formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping. That's correct for human-readable output, but likely to cause problems if presented to another computer (parseDouble(String) can't parse such a number, for example). You should also be wary of the toLowerCase() and toUpperCase() overloads that don't take a Locale: in Turkey, for example, the characters 'i' and 'I' won't be converted to 'I' and 'i'. This is the correct behavior for Turkish text (such as user input), but inappropriate for, say, HTTP headers.

- 区域设置开发者文档

-- Locale developer documentation