Java Gold Countermeasures: Localization

Introduction

This paper summarizes the following items as a summary of Java Gold learning. (Also for those who are learning Java Gold or want to know about localization.)

What is a locale?

java.util.Locale

Show the notation rules for things that differ by language and country.

Example: Units, symbols, dates, currencies, etc.

Use the locale when you want to change the display for each country where the application is used. (Characters are divided into Japanese and English depending on the country where the app is used, etc.)

There are three main methods for creating an instance.

■ Main constructors and methods

constructor Contents
Local(String language, String country) Argument language/Generate object from country code
//Instantiation with new
//ja ・ en: Language name code | JP ・ US: Country code
Locale localeJp = new Locale("ja","JP");  //Japanese locale
Locale localeUs = new Locale("en","US");  //American locale

//Instance generation by Locale class constant
Locale localeJp = Locale.JAPAN;
Locale localeUs = Locale.US;

// getDefault()Instantiation by method
//When executed in Japan, a Japanese Locale instance is generated.
Locale localeJp = Locale.getDefault();
Method Contents
static getDefault() Get the current value of the default locale
final String getDisplayCountry() Return the country name of the locale
final String getDisplayLanguage() Return the language name of the locale
String getCountry() Return the country code of the locale
String getLanguage() Return the language name code of the locale
// ja_JP is stored (when OS is Japan)
Locale locale = Locale.getDefault();

//Japan is stored
String country = locale.getDisplayCountry();

//Japanese is stored
String language = locale.getDisplayLanguage();

//JP is stored
String country_code = locale.getCountry();

//ja is stored
String language_code = locale.getLanguage();

//Remarks: Other instantiation methods
Locale locale = new Locale.Builder().setLanguage("ja")
                                    .setScript("Jpan")
                                    .setRegion("JP")
                                    .build();
// setScript(): The argument is ISO 15924 alpha-4 Script code (see Javadoc)
// build():Locale.Create Local object with Builder method

Resource bundle

java.util.ResourceBundle

Correspond to different notation rules for each language and country according to the locale. This makes it possible to switch between Japanese notation and English notation according to the locale.

The main methods used are as follows.

Method Contents
boolean containsKey(String key) Returns true if the key specified by the argument is in the resource
final Object getObject Return the object associated with the key specified by the argument
final String getString(String key) Return the character string associated with the key specified by the argument
final String[] getStringArray(String key) Returns an array of character strings associated with the key specified by the argument
Set keySet() Return all keys included in the bundle

In addition, there are the following as subclasses.

Subclass Contents
ListResourceBundle Manage resources for locales in a convenient and easy-to-use list
PropertyResourceBuilder Manage resources for the locale using a set of static strings from the properties file

■ When using ListResourceBundle

The definition rules when using this are as follows.

  • Definition method

** ▼ Implementation example **

Resource.java


public class Resource extends ListResourceBundle {

	public Object[][] getContents() {
		Object[][] contents = {
				{"apple", "Apple"},
				{"orange", "Orange"}
		};
		return contents;
	}
}

Resource_en_US.java


public class Resource_en extends ListResourceBundle {

	protected Object[][] getContents() {
		Object[][] contents = {
				{"apple", "apple"},
				{"orange", "orange"}
		};
		return contents;
	}
}

Locale by setting base name_language code_country code.java The file of the language corresponding to the object of is referenced.

For the default locale instance, the base name resource is referenced. If only the language code is specified for the locale instance, only the language code needs to be specified after the base name.

Main.java



//The output result is as follows
//Apple:Orange
// apple:orange

class Main {
	public static void main(String[] args) {

		Locale localeJa = Locale.JAPAN;
		Locale localeUs = Locale.US;

		//If the second argument is not specified, specify the default locate.
		List<Locale> locales =
				new ArrayList<Locale>(Arrays.asList(localeJa, localeUs));

		for(Locale locale : locales) {
			//package name.Specify the file name
			ResourceBundle rb =
				ResourceBundle.getBundle("resource.Resource", locale);

			//If you want to get something other than String, cast it using the getObject method
			System.out.println(rb.getString("apple") + ":" + rb.getString("orange"));
		}
	}
}

■ When using PropertyResourceBuilder

The definition rules when using this are as follows.

** ▼ Implementation example **

image.png

Source.properties


apple=J_apple
orange=J_orange

Source_en_US.properties


apple=U_apple
orange=U_orange

Main.java



//Output result
// J_appleJ_orange
// U_appleU_orange

class Main {
	public static void main(String[] args) throws MalformedURLException {

        File dicDir = Paths.get(".\\resource").toFile();

        URLClassLoader urlLoader;
        urlLoader = new URLClassLoader(new URL[]{dicDir.toURI().toURL()});

		Locale localeJp = Locale.JAPAN;
		Locale localeUs = Locale.US;

		List<Locale> locales =
				new ArrayList<Locale>(Arrays.asList(localeJp, localeUs));

		for (Locale locale : locales) {
			//Specify only the file name
			ResourceBundle rb =
					ResourceBundle.getBundle("Source" ,locale ,urlLoader);

			System.out.println(rb.getString("apple") + rb.getString("orange"));

		}
	}
}

References

Recommended Posts

Java Gold Countermeasures: Localization
Java Gold Countermeasures: Format
java error countermeasures
Countermeasures for Java OutOfMemoryError
Java8 Gold exam memorandum
I started Java Gold (Chapter 1-1)
On passing Java Gold SE 8
I took Java SE8 Gold.
Oracle Certified Java Programmer, Gold SE 8
Story of passing Java Gold SE8
Java
What I learned with Java Gold