[英]How to correctly use java.util.Locale to check for Australian locale
Question : How do I correctly use java.util.locale for checking a user's locale? 问题:如何正确使用java.util.locale检查用户的语言环境?
Summary : The legacy code I have uses a predefined static in Locale to check if a user is, for example, in France ... 摘要:我拥有的旧代码在Locale中使用预定义的静态代码来检查用户是否例如在法国...
if(Locale.FRANCE.equals(locale) || Locale.FRENCH.equals(locale)) {
// do stuff
}
I wish to add some code to check if a user is in Australia. 我希望添加一些代码来检查用户是否在澳大利亚。 However, Locale only has a limited set of predefined statics, and AUSTRALIA is not one of them.
但是,语言环境只有一组有限的预定义静态变量,而澳大利亚不是其中之一。 I appear to be able to do the following ...
我似乎能够做到以下几点...
if(new Locale("AU").equals(locale)) {
// do stuff
}
However, this is inconsistent with the existing code. 但是,这与现有代码不一致。 What is the correct way of doing it?
正确的做法是什么? If the first example I have given is correct, why is the predefined list of statics so limited?
如果我给出的第一个示例是正确的,为什么预定义的静态列表如此受限制?
No. new Locale( "AU" )
would be the language "AU" (whatever that is). 否
new Locale( "AU" )
将是语言“ AU”(无论是哪种语言)。 You need the two argument constructor! 您需要两个参数的构造函数!
The Locale.equals()
methods compares both language
, country
and variant
. Locale.equals()
方法同时比较language
, country
和variant
。 You should probably check like this: 您可能应该这样检查:
if ( "AU".equals( locale.getCountry() ) ) { /* do stuff */ }
As for why the list of predefined Locale
's are so limited: Pass. 至于为什么预定义的
Locale
列表如此有限的原因:Pass。 We should probably be honored that there is anything but en_US
at all :-) 我们可能应该感到荣幸,除了
en_US
,什么都没有:-)
Cheers, 干杯,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.