[Java] Java Gold Measures: Localization

4 minute read

Introduction

This article summarizes the following items as a summary of Java Gold learning. (Similarly, if you are learning Java Gold or want to know about localization, please refer to it.)

  • Locale
  • Resource bundle

What is a locale

java.util.Locale

The following shows the notational conventions for different languages and countries.

Example: Unit, symbol, date, currency, etc.

Use the locale when you want to change the display for each country where the application is used. (Depending on the country in which the app is used, characters may be divided into Japanese and English, etc.)

There are mainly three types of instance generation methods below.

  • new
  • Local class constants
  • getDefault() method

■Main constructor methods

Constructor Content
Local(String language, String country) Generate object from language/country code of argument
// instantiate with new
// ja/en: Language name code | JP/US: Country name code
Locale localeJp = new Locale("ja","JP"); // Japanese locale
Locale localeUs = new Locale("en","US"); // American locale

// Instantiation with constant of Locale class
Locale localeJp = Locale.JAPAN;
Locale localeUs = Locale.US;

// Instance generation by getDefault() method
// Running in Japan will create a Japanese Locale instance
Locale localeJp = Locale.getDefault();
Method Content
static getDefault() Get current value of default locale
final String getDisplayCountry() Returns the country name of the locale
final String getDisplayLanguage() Returns the language name of the locale
String getCountry() Returns the country code for the locale
String getLanguage() Returns 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();

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

// Remark: Other instance generation method
Locale locale = new Locale.Builder().setLanguage("ja")
                                    .setScript("Jpan")
                                    .setRegion("JP")
                                    .build();
// setScript(): argument is ISO 15924 alpha-4 script code (see Javadoc)
// build(): Create a Local object using the Locale.Builder method

Resource bundle

java.util.ResourceBundle

Correspond to the notational conventions that differ for each language and country according to the locale. This allows you to switch between Japanese and English notation depending on the locale.

The methods mainly used are as follows.

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

Also, there are the following subclasses.

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

■ When using ListResourceBundle

The definition rule when using this is as follows.

  • Definition method
    • Create a public class that inherits ListResourceBundle
    • Override getContents() method to create a list of resources in an array
    • Resource is created as an array with key and value as elements

▼ 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 name_language code_country code.java by setting locale The file of the language corresponding to the object of is referred to.

In case of default locale instance, the resource of base name is referenced. If only the language code is specified for the instance of the locale, only the language code is required 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;

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

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

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

■ When using PropertyResourceBuilder

The definition rule when using this is as follows.

  • Property file name is base_name_language_code_country_code.properties
  • The resource key and value are described in the property file in the form of key = value.

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

  }
 }
}

参考文献

  • https://docs.oracle.com/javase/jp/7/api/java/util/Locale.html
  • https://docs.oracle.com/javase/jp/8/docs/api/java/util/ResourceBundle.html