The ServeltRequest has a getLocale() method, the definition is:
Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.
I wrote the following simple program:
Locale loc = new Locale("ja", "JP", "JP");
Calendar calendar = Calendar.getInstance(loc);
System.out.println(calendar.get(calendar.YEAR));
It successfully shows the Japanese Imperial Calendar instead of the GregorianCalendar.
Now, my question is: how can a user set up his browser eg Chrome so that request.getLocale()
will return Locale("ja", "JP", "JP")
? I tried to add the Japanese language but still I get the GregorianCalendar.
From the source for private static Calendar createCalendar(TimeZone zone, Locale aLocale)
(the method that is called internally when you call getCalendar(locale)
:
if (aLocale.hasExtensions()) {
String caltype = aLocale.getUnicodeLocaleType("ca");
if (caltype != null) {
switch (caltype) {
...
case "japanese":
cal = new JapaneseImperialCalendar(zone, aLocale);
break;
...
}
}
}
if (cal == null) {
...
if (aLocale.getVariant() == "JP" && aLocale.getLanguage() == "ja"
&& aLocale.getCountry() == "JP") {
cal = new JapaneseImperialCalendar(zone, aLocale);
} else {
cal = new GregorianCalendar(zone, aLocale);
}
}
As you can see in the second part, the locale needs to have country = JP, language = ja and variant = JP, ie a locale string of ja_JP_JP
.
The problem is that browsers don't seem to directly support setting a country and variant in the language settings, so you might have to infer that yourself - depending on your needs:
ja
assume the country and the variant to both be JP
JP
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.