Chapter 3 Internationalization in IOC (CVS Version: V002)
From this chapter, I will open an instance project to a CVS version, I don't know who can provide an FTP space?
3.1 Preface
The title should be "using the IOC function in Spring to realize the internationalization of our development project", and internationalization is not for IOC, but for the entire system you develop.
If you use Eclipse's internationalization, or use Eclipse's "External String" Wizard (Eclipse Main Menu: Source Code -> External Strings), the internationalization function provided by Spring should be very easy to understand, Both of them are basically the same, or the international methods of various Java programs are basically the same.
Let's talk about two components of Eclipse internationalization: *. Properties resource files, get the Message class for the content of the resource file.
Spring and Eclipse are similar: the resource files are the same, and the translation of different languages is placed in different resource files. The same rules are the same; Eclipse's Message class should write (code universal, copy the previous project That is, or use Eclipse's wizard to generate a row), Spring already has a written Message class, we can use it in the IOC's XML file (you can also implement Spring Messagesource interface, write a Message Class, the code is not complicated, but this is not necessary, and it will be provided with Spring).
Whether it is Eclipse's Message class, or Spring's own Message class, or we write a Message class yourself, all using JAVA.util.ResourceBundle classes to implement * .properties files.
The following is to experience the screenshot of the project structure after completion of this chapter first.
3.2 Simple instance
Suppose we have the following procedures, the role of the program is to print a string
Package cn.com.chengang.spring;
Public class messagemetest {
Public static void main (String [] args) {
String str = "chengang";
System.out.println (STR);
}
}
Now, we must make this program to output different characters according to the user's language, such as output "Chengngu" to English users, output "Chen Gang" for Chinese users, using output "Chen Gang" for Taiwan. The implementation method of this demand is as follows:
1. Create a series of resource files
Create the following files under the cn.com.chengang.spring package:
(1) Messages.properties (default: English), only one sentence, as follows
Chengang = Giles
"Chengang" is the key value, giles is the English string to output.
(2) Messages_zh_cn.properties (Simplified Chinese)
Chengang = / U9648 / U521A
"/ U9648 / u521a" is a Unicode code, the corresponding Chinese is "Chen Gang"
(3) Messages_ _tw.properties (Traditional Chinese)
"Chen Gang" is "Chen Gang" in Chinese.
Note: Since Chinese is to convert into Unicode, there are many inconveniences in editing and reading, if you use Eclipse to make IDE, there is a plugin for editing resource files, the download URL is http: //www.guh-software. DE /, the resource file that is opened is as shown in the following figure, you can see that three resources are reflected in an interface.
If you don't have eclipse, it is programmed by the EditPlugs JDK (now there is such a primitive?), You can also transfer the Chinese string to the Unicode code with the Native2ASCII.exe program that comes with JDK. ANT also provides a corresponding task:
2, modify bean.xml
Sign up the Org.SpringFramework.Context.support.ResourceBundleMessagesSource class comes into bean.xml in Bean.xml, which is to obtain the contents of the resource file, and is to automatically obtain such objects in the bean.xml file of IOC. (Spring has made some simplified programming).
XML Version = "1.0" encoding = "UTF-8"?>
list>
Property>
bean>
beans>
Code description:
l id = "Messagesource" setting is constant, must be.
l ResourceBundleMessageSource is a Message class for Spring. There is also a choice, with the ReloadableResourceBundleMessageSource class, this class can provide features that can reload the resource file without reboot (the former is only loaded only once for the resource file). For the need for thermal modification resource files, the latter is more appropriate, but the latter may have loss in efficiency, because at least some code to check if the resource file is changed (this is just my guess, I don't have it. Read the source code of this section). l "basenames" is constant, must be. It is an attribute of ResourceBundleMessagesource, which is "Private String [] Basenames;", it is a string array.
l "cn.com.chengang.spring.Messages" is incorporated into the baseNames property. Note: Three resource files need to be incorporated in common primary names (red fonts): messages.properties, messages_en_cn.properties, messages_en_tw.properties.
3, use. Modify the MessageTest class, as follows
Package cn.com.chengang.spring;
Import org.springframework.context.ApplicationContext;
Import org.springframework.context.support.filesystemxmlapplicationContext;
Public class messagemetest {
Public static void main (String [] args) {
ApplicationContext CTX = New FileSystemXMLApplicationContext ("bean.xml");
String str = ctx.getMessage ("Chengang", NULL, NULL);
System.out.println (STR);
}
}
Code description:
(1) MAIN method
l The first sentence acquisition information of the bean.xml file.
l The second sentence obtains the string corresponding to the key value chengang from the resource file.
l Print the string in the third sentence, the result is that "Chen Gang" is printed, indicating that the Messages_ZH_CN.Properties resource file is read.
(2) CTX.getMessage ("Chengang", NULL, NULL); there are three parameters:
l The first is the key value of the resource file;
l The second is the parameter of the resource file string. Since this string does not have a parameters, use a NULL (later, an instance of a string parameter);
l The third is a java.util. Locale type of parameters. The parameter is NULL, indicating that according to the user's locale, because I use the Chinese version of Windows, so it automatically selects the Messages_n_cn.properties resource file when taking a string. There is also a control point in the JVM and JVM will be processed according to the locale of the current operating system. We can set the current JVM language type by adding "-duser.language = zh_tw" in the JVM startup parameter. Level settings can also be automatically switched to the resource file type used. So there are three ways to control the language: set from the Locale of the operating system of the lowest layer, to the Locale setting of the last layer of JVM, and then go to the Locale setting of the program level. 3.3 Other ways of using resource files:
Package cn.com.chengang.spring;
Import java.util.locale;
Import org.springframework.context.ApplicationContext;
Import org.springframework.context.support.filesystemxmlapplicationContext;
Import org.springframework.context.support.resourceBundleMessagesource;
Public class messagemetest {
Public static void main (String [] args) {
ApplicationContext CTX = New FileSystemXMLApplicationContext ("bean.xml");
String str = ctx.getMessage ("Chengang", NULL, NULL);
System.out.println (STR); // Output "Chen Gang"
/ *
* Used Messages.properties
* /
Str = ctx.getMessage ("Chengngu", NULL, New Locale (""));
System.out.println (STR); // Output "Giles"
/ *
* Used Messages_ZH_CN.Properties
* /
Str = ctx.getMessage ("Chengngu", NULL, New Locale ("EN", "CN"));
System.out.println (STR); // Output "Chen Gang"
/ *
* Used Messages_ZH_TW.Properties
* /
Str = ctx.getMessage ("Chengang", NULL, New Locale ("EN", "Tw"));
System.out.println (STR); // Output "Chen Gang"
/ *
* Using Messages_ZH_TW.Properties, you can see the name of the resource file from here.
* For example, we build a message_123.properties, you can do this in the mailing parameter:
* New Locale ("123"), or the value in the message_123.properties can also be taken out.
* /
Str = ctx.getMessage ("Chengngu", NULL, New Locale ("zh_tw"));
System.out.println (STR); // Output "Chen Gang" / *
* When the corresponding resource file is not found, use Messages_ZH_CN.Properties
* /
Str = CTX.getMessage ("Chengang", NULL, New Locale ("abcd"));
System.out.println (STR); // Output "Chen Gang"
/ **
* Do not register with IOC, use the RESourceBundleMessageSource class.
* /
ResourceBundleMessagesource s = new resourcebundleMessagesource ();
S.SetBaseName ("cn.com.chengang.spring.messages);
Str = S.GetMessage ("Chengang", NULL, NULL);
System.out.println (STR); // Output "Chen Gang"
}
}
Code description:
Three in the previous control language: set from the Locale of the operating system of the lowest layer, to the Locale setting of the last layer of JVM, and then go to the Locale setting of the program level. I think the best way is to control the program level: Define a unified Locale static variable, and then use this variable throughout the system, you can set the value of this Locale variable through the interface operation, let the user choose him The required software language. And we can also set this static variable into a NULL value, from the selection of the resource file.
In addition, some constants have also been defined in Locale, we can use directly without having to go to a local Locale object, such as "locale.english".
3.4 another example
This instance demonstrates how to use multiple resource files and how to use string parameters
(1) Create a resource file next to the cn.com.chengang.spring package MessageSother_Zh_CN.Properties
Chengngu.info = / U9648 / U521A / UFF0C / U7F51 / U540D / UFF1A {0} / UFF0C / U82F1 / U6587 / U540D / UFF1A {1} / uff0cblog / uff1a {2}
The Chinese in which the Unicode string corresponds to: "Chen Gang, Net Name: {0}, English name: {1}, blog: {2}", this string has three parameters.
(2) Modify the bean.xml file
Because the baseNames property is an array, of course, multiple resource file settings can be received. Specifically modify the red word part below
XML Version = "1.0" encoding = "UTF-8"?>
list>
Property>
bean>
beans>
(3) Modify the MessageTest class, join a few lines of code used
String [] strunt = new string [3];
STRARGS [0] = "Biochemical Migrant Workers";
Strargs [1] = "giles";
Strargs [2] = "http://blog.9cbs.net/glchengang";
Str = ctx.getMessage ("Chengang.INFO", Strargs, NULL);
System.out.println (STR);
The result of printing is: "Chen Gang, Nette: Biugang Migrant, English Name: giles, blog: http://blog.9cbs.net/glchengang"
3.5 Internationalized Practice Suggestions
l Recommendation a package corresponding to a resource file. Don't use a resource file throughout the system to translate, so that the volume of a single document is too big, it is not conducive to maintenance; of course, it is not necessary to correspond to a resource file so that there is too much resource file.
l Recommended resource files and their translation classes / packages in the same directory. However, if you want to push the software into an external JAR package or WAR package, it is recommended to separate the resource files so that the resource file can be modified without having to pack it again.
l Advice the key value of the string item to add its class name. For example: The above chengang and Chengang.info are best named Messagetest.chengang and MessageTest.chengang.info. This will make a lot of classes that look for this key value.
references
L Xia Zhen << Spring Development Guide >> http://www.xiaxin.net/spring_dev_guide.rar