I was a little worried about how to use Locale when I touched Java for the first time in a long time and supported multiple languages, so I made a code for checking.
I want to get the value of Locale and change the language processing automatically. I compared the result of Locale.getDefault () with Locale.JAPANESE, which represents Japanese, and tried to display a Japanese message if they were the same, but the value was not as expected.
The value of Locale.getDefault () was ja_JP, but the value of Locale.JAPANESE was ja, so even if compared, it could not be displayed in Japanese. Since the value of Locale.JAPAN was ja_JP, should I compare and judge the value of Locale.getDefault () and Locale.JAPAN to display Japanese?
Locale.JAPAN stands for Japan (country) and Locale.JAPANESE stands for Japanese (language). Even if you live in Japan, some people use English as their language, so it's a shame to compare it with Locale.JAPAN. It is good to compare the value obtained by Locale.getDefault (). GetLanguage () with the value of Locale.JAPANESE.getLanguage ().
Troublesome English includes American English en_US, British English en_GB, and Aussie English en_AU, but the display can be in English. Does British English need to change my to me or be verbs like Cockney? Is it going to be a so-called mockney, like the strange Kansai dialect spoken by people in Tokyo? : thinking:
import java.util.Locale;
public class LocaleTest{
public static void main(String[] args){
Locale localization=Locale.getDefault();
System.out.println("java.version:" + System.getProperty("java.version"));
System.out.println ("localization.toString ():" + localization.toString ()); // Display Locale type as a string
System.out.println ("localization.getCountry ():" + localization.getCountry ()); // Country
System.out.println ("localization.getLanguage ():" + localization.getLanguage ()); // Language
System.out.println ("localization.getDisplayVariant ():" + localization.getDisplayVariant ()); // Variant
System.out.println ("Locale.getDefault ():" + localization); // Locale type
System.out.println ("localization.getLanguage ():" + localization.getLanguage ()); // String type
System.out.println ("Locale.JAPAN (country):" + Locale.JAPAN);
System.out.println ("Locale.JAPAN (Country.getCountry ()):" + Locale.JAPAN.getCountry ());
System.out.println ("Locale.JAPAN (Country.getLanguage ()):" + Locale.JAPAN.getLanguage ());
? String msg = localization == Locale.JAPAN "! Hello world": "Hello World";
System.out.println(msg);
System.out.println ("Locale.JAPANESE (Japanese):" + Locale.JAPANESE);
System.out.println ("Locale.JAPANESE (Japanese.getCountry ()):" + Locale.JAPANESE.getCountry ());
System.out.println ("Locale.JAPANESE (Japanese.getLanguage ()):" + Locale.JAPANESE.getLanguage ());
? Msg = localization == Locale.JAPANESE "! Hello world": "Hello World";
System.out.println(msg);
System.out.println("localization.getLanguage()==Locale.JAPANESE.getLanguage()");
? Msg = localization.getLanguage () == Locale.JAPANESE.getLanguage () "! Hello world": "Hello World";
System.out.println(msg);
}
}
java.version:1.8.0_181
localization.toString():ja_JP
localization.getCountry() :JP
localization.getLanguage() :ja
localization.getDisplayVariant():
Locale.getDefault():ja_JP
localization.getLanguage():ja
Locale.JAPAN (country): ja_JP
Locale.JAPAN (Country.getCountry ()): JP
Locale.JAPAN (Country.getLanguage ()): ja
Hello World!
Locale.JAPANESE (Japanese): ja
Locale.JAPANESE (Japanese.getCountry ()):
Locale.JAPANESE (Japanese.getLanguage ()): ja
Hello World
localization.getLanguage()==Locale.JAPANESE.getLanguage()
Hello World!
Recommended Posts