Article Directory
The so-called internationalization of the program refers to a program code can achieve the same language to describe different depending on the country, but the core business processing procedures are the same
Introduction to international issues
Now suppose there is a world recognized business management platform, then the business owners decided to do this product be extended to all large listed companies around the world, so these companies may come from: China, the United States, Germany and so on and so on, so in such a case, what is the problem first thing to consider is it?
After the analysis can be found, if it is to achieve international program development, then the problem to be solved is that the following two points:
- You can define how to save the text file information
- How can you read the specified resource information encoded according to different areas of language
Locale class
Can be found through analysis, if it is to achieve international, you first need to solve is the coding region and language of users in different countries of the problem, and provides a description of special coding region and language classes in the java.util package: Locale, you can then be used two major class Locale constructors instantiate
Locale class constructor:
- Constructors: public Locale (String language)
- Constructors: public Locale (String language, String country)
At this point need is country and language codes, and Chinese code: zh_CN, the code of the United States English is en_US
Examples :( instantiate an object class Locale)
package com.java.springtest.international;
import java.util.Locale;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
// 中文环境
Locale locale = new Locale("zh","CN");
System.out.println(locale);
}
}
Output:
But this is to manually select the language, if now in order to automatically get the current operating environment, you can now carry on using the Locale class itself the default instantiate
- Read local default environment: public static Locale getDefault ()
package com.java.springtest.international;
import java.util.Locale;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
// 获取默认环境
Locale locale = Locale.getDefault();
System.out.println(locale);
}
}
Output:
in the actual development process, many people may not care about the coding of countries and languages, so in order to simplify the development, Locale class will also be some of the more famous of the country's encoding settings for the world's constant
package com.java.springtest.international;
import java.util.Locale;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
// 使用 Locale 类的常量
Locale locale = Locale.CHINA;
System.out.println(locale);
}
}
Why can get enough of it based on constant? Open source Locale find that it is their own definition
static public final Locale CHINA = SIMPLIFIED_CHINESE;
static public final Locale SIMPLIFIED_CHINESE = createConstant("zh", "CN");
The advantage of using constants is that some areas can avoid the tedious coding information
Read the resource file: ResourceBundle
To read operation mainly depends on the resource file is complete java.util.ResourceBundle class, such is defined as follows:
public abstract class ResourceBundle extends Object
It can be found ResourceBundle is an abstract class, if we want to instantiate such an object can be done directly use a static class static method provided:
- Get ResourceBundle class object: public static final ResourceBundle getBundle (String baseName)
- -baseName: Description is the name of the resource file, but no suffix (com.java.springtest.message.Message)
- Read the content of the resource (read according to key): public final String getString (String key)
Example: Use ResourceBundle class read content
First to write a simple resource file, as follows (note that the resource file path resources need to put under the classpath):
code implementation:
package com.java.springtest.international;
import java.util.ResourceBundle;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
// Message 不需要后缀,如果资源没有放在包里面,则直接编写资源名称即可
ResourceBundle resourceBundle = ResourceBundle.getBundle("Message");
// 根据 key 获取内容
String info = resourceBundle.getString("info");
// 打印输出
System.out.println(info);
}
}
When resources during the data read key must exist, if not, the following message appears abnormal, as follows:
package com.java.springtest.international;
import java.util.ResourceBundle;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("Message");
String info = resourceBundle.getString("inFo");
System.out.println(info);
}
}
Exception in thread "main" java.util.MissingResourceException:
Can't find resource for bundle java.util.PropertyResourceBundle, key inFo
Achieve national programming
That rely on the resource file, Locale, ResourceBundle class processing operations can be achieved internationalization, then the following procedures to achieve international (core key: read resource information)
Examples :( code implementation)
1, the establishment of a com.java.springtest.message.Message_zh_CN.properties in the classpath (Chinese resources)
info = 欢迎您的访问!
2, establish a com.java.springtest.message.Message_en_US.properties in the classpath (Resource in English)
info = Welcome!
Now add the resource file is no default region, we have defined three resource files
3, resource information specified area by loading program
package com.java.springtest.international;
import java.util.ResourceBundle;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("Message");
String info = resourceBundle.getString("info");
System.out.println(info);
}
}
ResourceBundle class to read again this time using the resources of time and did not set an explicit Locale object, but found Message_zh_CN file works, why do not the Message? Because this method which is currently loaded by default Locale Local Resources:
// Locale 类中的 getBundle() 方法
@CallerSensitive
public static final ResourceBundle getBundle(String baseName)
{
// 获取当前的资源
return getBundleImpl(baseName, Locale.getDefault(),
getLoader(Reflection.getCallerClass()),
getDefaultControl(baseName));
}
4, if there is now a need to modify the current to be Locale environment, a method may be used ResourceBundle class:
- 获取 ResourceBundle:public static final ResourceBundle getBundle(String baseName,
Locale locale)
package com.java.springtest.international;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
Locale locale = new Locale("en","US");
ResourceBundle resourceBundle = ResourceBundle.getBundle("Message",locale);
String info = resourceBundle.getString("info");
System.out.println(info);
}
}
Output:
If you now have a resource file when there is a designated area, then the information is not provided in the area of resource files will not be read
Sequential read resource file : read the resource file designated area> default local resources> public resources (the region is not set)
Message Format
If one user is now a successful login, then usually display such information "XXX, to welcome you," that this time will display the user name, then the time if the content is stored in the resource file inside, then it needs to be described by the placeholder, while the data read out of the message needs to be processed also formatted
For example :( modify the resource file)
resources name | Resource Information |
---|---|
[ Chinese resource file ] com.java.springtest.message.Message_zh_CN.properties | Welcome info = {0}, {1} is the current date |
[ English resource file ] com.java.springtest.message.Message_en_US.properties | info = Welcome{0},date:{1} |
If necessary, you can continue to add content {1}, {2} and the like
At this time, if you want to read the resource information will be read out together placeholder, so in this case needs to be formatted using MessageFormat class
provides a formatted text MessageFormat in Methods: public static String format (String pattern,
Object ... arguments The)
For example :( formatted text internationalization)
package com.java.springtest.international;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
/**
* @author Woo_home
* @create by 2020/1/30
*/
public class JavaAPIDemo {
public static void main(String[] args) {
Locale locale = new Locale("en","US");
ResourceBundle resourceBundle = ResourceBundle.getBundle("Message",locale);
String info = resourceBundle.getString("info");
System.out.println(MessageFormat.format(info,"international",new SimpleDateFormat("yyyy-MM-dd").format(new Date())));
}
}
If the resource file which has encountered in the development of {0}, {1} represents the structure is a placeholder for this information must be formatted