Multilingual Locale in Java How to use Locale

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.

Introduction

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.

What I was worried about

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?

Interpretation

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);

  }
}

Execution result

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

Multilingual Locale in Java How to use Locale
How to use classes in Java?
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
How to use Java variables
[Java] How to use Optional ①
Notes on how to use regular expressions in Java
How to use Lombok in Spring
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
How to learn JAVA in 7 days
[Processing × Java] How to use variables
How to use InjectorHolder in OpenAM
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to name variables in Java
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
How to concatenate strings in java
How to use JSON data in WebSocket communication (Java, JavaScript)
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
How to implement Kalman filter in Java
[Java] How to use the File class
How to use custom helpers in rails
[Java] How to use the hasNext function
How to use named volume in docker-compose.yml
How to use submit method (Java Silver)
[Java] How to use the HashMap class
How to do base conversion in Java
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to implement coding conventions in Java
How to use Java classes, definitions, import
How to embed Janus Graph in Java
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to get the date in java
[Processing × Java] How to use the function
How to use environment variables in RubyOnRails
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2