Introduction to a concept
The so-called internationalization refers to a program can be adapted to multiple languages at the same time, for example, Chinese use the interface is displayed in Chinese, foreigners use the interface is displayed in English or other languages. To achieve the internationalization of the program, you need to use these three classes:
(1) Java.util.Locale: Denotes a national language class;
(2) java.util.ResourceBundle: Used to access resource files;
(3) Java.text.MessageFormat: Formatting a placeholder string in a resource file
The path to the example used below:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/3F/wKioL1b6pCHgSSHvAAAehsKcQ3U641.png "title=" 20160313235743_14698.png "alt=" Wkiol1b6pchgsshvaaaehskcq3u641.png "/>
Two A simple demo
Use the ResourceBundle class to get the contents of the resource file:
Package Javase.international;import Java.util.resourcebundle;public class InterDemo1 {public static void main (string[] args) {ResourceBundle rbundle = Resourcebundle.getbundle ("Javase.international.properties.Message");//load resource file, Note Here the file path System.out.println ("site:" + rbundle.getstring ("website")); Get content from resource file}}
Resource file: messagemessage.properties:
Website = http://www.zifangsky.cn
Output:
Site: http://www.zifangsky.cn
This example is very simple, is to load the corresponding property from the resource file, it should be noted that:
(1) The key value in the resource file is saved in the form of "key = value";
(2) about the path problem of loading a resource file using ResourceBundle: [Package name]. [File name body], so we know that if the resource file is in the SRC directory, this is OK: resourcebundle.getbundle ("Message");
Three international complete examples, output different content for Chinese and English respectively
There are two common ways to construct a locale class:
define |
description |
locale (String language) |
Constructs a locale according to the language code |
locale (String language, String country) |
Constructs a locale based on language and country/region |
package javase.international;import java.util.locale;import java.util.resourcebundle;public class helloworld {public static void main ( String[] args) {resourcebundle zh_rbundle = resourcebundle.getbundle (" Javase.international.properties.HelloWorld ", new locale (" zh ", " CN ")); // Load the Chinese properties file Resourcebundle en_rbundle = resourcebundle.getbundle (" Javase.international.properties.HelloWorld ", new locale (" en ", " US ")); // Load the English attribute file System.out.println ("Chinese:" + zh_rbundle.getstring ("info")); System.out.println ("English:" + en_rbundle.getstring ("info"));}}
Resource file Helloworld_zh_cn.properties:
info = \u4f60\u597d\uff0c\u4e16\u754c\uff01
Note: You need to use Unicode encoding here
Resource file Helloworld_en_us.properties:
info = Hello World!
Output:
English: Hello, world! English: Hello World!
Four-Process dynamic text
In the demo above, all the resource files are fixed, if you want to include some dynamic text in the output message, then you need to use the placeholder "{number}" to dynamically represent the position of the text
package javase.international;import java.text.messageformat; Import java.util.locale;import java.util.resourcebundle;public class demo {public static void main (String[] args) {ResourceBundle zh_rBundle = Resourcebundle.getbundle ("Javase.international.properties.Code", new locale ("zh", "CN")); Load Chinese properties file Resourcebundle en_rbundle = resourcebundle.getbundle (" Javase.international.properties.Code ", new locale (" en ", " US ")); // loading English attribute file string str1 = zh_rbundle.getstring ("say"); String str2 = en_rbundle.getstring ("say"); System.out.println ("Chinese:" + messageformat.format (str1, "PHP")); System.out.println ("English:" + messageformat.format (str2, "PHP"));}}
Property file Code_zh_cn.properties:
Say = {0}\u662f\u6700\u597d\u7684\u7f16\u7a0b\u8bed\u8a00
Property file Code_en_us.properties:
Say = {0} is the best programming language
Output:
English: PHP is the best programming language: PHP is the programming language
This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1758244
Java Basic Series 7: Introduction to Internationalization programs