Solve internationalization problems with Java

Source: Internet
Author: User

If the application system is oriented to multiple languages, you have to solve the internationalization problems during programming, including style issues on the operating interface, versions of prompt and help languages, and customization issues.
The Java language has advantages such as platform independence and portability, and provides powerful class libraries. Therefore, the Java language can help us solve the above problems. The Java language uses double-byte encoding and the character set of big Chinese characters, which provides a lot of convenience to solve the internationalization problem. From the design point of view, as long as the part of the program related to language and culture is separated and added with special processing, the problem of internationalization can be partially solved. In terms of interface style customization, we store parameterized elements such as fonts and colors in the database to provide a friendly interface for users; if some parts contain elements that cannot be parameterized, we may have to design them separately and solve specific problems through targeted encoding.
Java class package
When Java is used to solve international problems, the main classes that may be used are provided by the java. util package. The classes related to this package include locale, resourcebundle, listresourcebundle, and propertyresourcebundle. The Inheritance relationships are shown in.
The main functions provided by various types are as follows:
Locale: This class contains the encapsulation of the geographical features of the main geographic areas. A specific object represents a specific geographic, political, or cultural region. By setting locale, we can provide fonts, symbols, icons, and expressions that conform to local cultural habits for specific countries or regions. For example, we can get the calendar ar class instance under a specific locale to display the date that conforms to the specific expression format.
Resourcebundle: this class is an abstract class. You must use the static method resourcebundle. getbundle () to specify the basic name of the specific implementation class or attribute file. The basic name is the same as the specified or default locale class to determine the unique name of the called class or attribute file. For example, if you specify the basic class or attribute file name as testbundle and the specified locale is Chinese, the most suitable class name for matching is testbundle_zh_cn.class, and the optimal matching attribute file name is testbundle_zh_cn.properties. According to the requirements of Java Doc and relevant documents, if the class or attribute file is not found, the system will look for an approximate match (the main file name is the class or attribute file testbundle_zh and testbundle in sequence ). The getkeys () method provided by this class is used to obtain the key names of all Members, and the handlegetobject method is provided to obtain the corresponding elements of the specified key.
Listresourcebundle: This class inherits the resourcebundle class, mainly to add some easy-to-operate components, but it is still an abstract class. If you want to use a class to implement a specific resourcebundle, it is generally better to inherit this class.
Propertyresourcebundle: This class also inherits the resourcebundle class and can be instantiated. The behavior characteristics of this class are similar to the java. util. properties class. You can obtain specific attribute pairs from the input stream.
If issues such as date and time display are involved, you can use the timezone, simpletimezone, and calendar classes in the Java. Text package and Java. util package for auxiliary processing.
Parameterization Solution
In a specific application, you can place the parameterized part of a specific country or region feature in a special named attribute file. After determining the specific locale, you can use the propertyresourcebundle class to read the corresponding attribute files and implement internationalized features.
Use the propertyresourcebundle class to obtain international information of the local version. Some code is as follows:
......
Public static final string base_prop_file =
"Disp ";
Public static final string suffix =
". Properties ";
Locale = locale. getdefault ();
String propfile = base_prop_file + "_" + locale. tostring () + suffix;
Resourcebundle RB;
Try {
File file = new file (propfile );
If (file. exists ()){
Is = new fileinputstream (File );
RB = new propertyresourcebundle (is );
If (rb = NULL) system. Out. println ("no resource ");
}
} Catch (ioexception IOE ){
System. Out. println ("error open file named" + propfile );
}
Enumeration E = RB. getkeys ();
While (E. hasmoreelements ()){
Key = (string) E. nextelement ();
Value = (string) Rb. handlegetobject (key );
System. Out. println ("Key:" + key +
"/T value:" + value );
}
......
The details of the disp_zh_tw.properties file are as follows:
Key1 =/u53ef/u4ee5
Key2 =/u64a4/u9500
The equal sign is followed by the traditional Chinese characters converted using the native2ascii program. If the conversion is not performed, the system may display garbled characters.
Handling tips and help
For the prompt language and help file, you can place the language ing in the attribute file or a subclass of the listresourcebundle class. The following program is a servlet. By accepting the client selection, it returns the information of the specific language and character version to the client.
......
Public class processservlet extends httpservlet
{// The default language is Chinese.
Public static final string default_language = "ZH ";
// The default character set is simplified Chinese.
Public static final string default_country = "cn ";
Public void Service (httpservletrequest req,
Httpservletresponse res) throws ioexception, servletexception {
Httpsession session = Req. getsession (true );
// Parameters of the specified language and character received from the client shall be consistent with relevant regulations of sun
String lang = Req. getparameter
("Language ");
String Country = Req. getparameter
("Country ");
If (lang = NULL)
{
// If the parameter is not received, try to get it from the session.
Lang = (string) Session. getattribute
("Language ");
Country = (string) Session. getattribute
("Country ")
} Else {
Session. setattribute ("language", Lang );
Session. setattribute ("country", country );
}
If (lang = NULL)
{
// If the language and character information cannot be obtained from the above method, use the default value
Lang = default_language;
Country = default_country
Session. setattribute ("language", Lang );
Session. setattribute ("country", country );
}
Locale locale = NULL;
Resourcebundle bundle = NULL;
Try {
Locale = new locale (Lang, country );
} Catch (exception e ){
System. Out. println ("No locale with" +
Country + "_" + Lang );
Locale = locale. getdefault ();
}
Try {
Bundle = resourcebundle. getbundle (
"Displaylist", locale );
} Catch (missingresourceexception e ){
System. Out. println ("no resources available for locale" + locale );
Bundle = resourcebundle. getbundle
("Displaylist", locale. US );
}
Res. setcontenttype ("text/html ");
Printwriter out = res. getwriter ();
Out. println ("<HTML> ");
Out. println ("
String title = bundle. getstring ("title ");
String welcome = bundle. getstring
("Welcome ");
String notice = bundle. getstring ("notice ");
Out. println ("<title>" + title +
"</Title> ");
Out. println ("
Out. println ("<body bgcolor = /"
White/"> ");
Out. println ("
"</H3> ");
Out. println ("<br> ");
Out. println ("<B>" + notice +
"</B> ");
Out. println ("</body> ");
Out. println ("
}
}
The property file (displaylist_zh_cn.
Properties:
Title = Chinese Version
Welcome = This is a Simplified Chinese version
Notice = Simplified Chinese
Note: This file uses Chinese characters rather than the converted Unicode code. This is because most Web servers do not need the preceding conversion.
In actual use, if the web server supports the servlet 2.3 specification (such as Jakarta-receivate 4.0), the servlet mentioned above should be slightly changed for use as the processor of other servlets. In addition, if you store a specific version of resourcebundle in a stateless Session Bean, you can improve program efficiency to a certain extent.
Knot
In actual tests, I found the following problems, some of which have been solved:
1. For garbled characters displayed, if an international solution is implemented through the attribute file, non-standard ASCII text may be directly written into the attribute file. Resolving this issue is to use native2ascii.exe to scan all attribute files and overwrite the original file content with scan results. If we use class files to implement the conversion scheme, we need to re-compile the relevant class files and specify the set of classes during compilation. For example, to compile a class file that uses the country code, use the following command:
Javac-encoding gb2312 your_java_file
2. Although Sun claims that during the resourcebundle class instantiation, the class will find the class that absolutely matches the specified basic class and matches the specified locale attribute as much as possible. For example, if we specify the resourcebundle base class as testbundle and the zh_cn (Simplified Chinese in mainland China) in locale, if the system cannot find testbundle_zh_cn, the system should search for testbundle_zh and testbundle in sequence. However, during system development, I found that this matching did not produce any practical results.
The author's test platform is Windows 2000 Server, and no service pack is configured. The JDK version used is 1.3.0. The author tries to view the SRC. find the cause of the problem, but find that the related operations are encapsulated in Sun. misc package, while SRC. the jar file does not provide the source code for any classes in the package. This article raises this question and hopes to discuss it with relevant developers.

Author's blog:Http://blog.csdn.net/zaowei21/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.