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.)
java.util.Locale
Show the notation rules for things that differ by language and country.
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.
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
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 |
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 |
The definition rules when using this are as follows.
- Definition method
Create a public class that inherits ListResourceBundle
Override the getContents () method to create a list of resources in an array
Resources are created as an array with keys and values as elements
[Quote](https://www.amazon.co.jp/%E3%82%AA%E3%83%A9%E3%82%AF%E3%83%AB%E8%AA%8D%E5% AE% 9A% E8% B3% 87% E6% A0% BC% E6% 95% 99% E7% A7% 91% E6% 9B% B8-Java% E3% 83% 97% E3% 83% AD% E3% 82% B0% E3% 83% A9% E3% 83% 9E-Gold-SE-8-ebook / dp / B01J1LPKJY / ref = sr_1_2? __mk_ja_JP =% E3% 82% AB% E3% 82% BF% E3% 82 % AB% E3% 83% 8A & dchild = 1 & keywords = gold + java & qid = 1593530267 & sr = 8-2)
** ▼ 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"));
}
}
}
The definition rules when using this are as follows.
** ▼ Implementation example **
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"));
}
}
}
Recommended Posts