Solving internationalization problems with Java

Source: Internet
Author: User
Tags abstract date character set expression final implement interface locale
Solve the problem if the application system is oriented to many languages, it is necessary to solve the problem of internationalization in programming, including the style of the Operation interface, the version of the prompt and help language, the customization of the interface, and so on.
Because the Java language has the advantages of platform-independent, portability, and provides a powerful class library, the Java language can help us solve these problems. The Java language itself uses Double-byte character encoding and a large character character set, which provides a lot of convenience for solving internationalization problems. From the design point of view, as long as the process of language and culture-related parts of the separation, coupled with special treatment, can partially solve the internationalization problem. In the custom aspect of the interface style, we store the elements that can be parameterized, such as fonts, colors, etc., in a database to provide a friendly interface for users, and if some parts contain elements that cannot be parameterized, then we may have to design them separately to solve specific problems through targeted coding.
Java class Package
In the process of solving internationalization problems with Java, the main classes that may be exploited are provided by the Java.util package. The related classes in this class include Locale, ResourceBundle, ListResourceBundle, propertyResourceBundle, and so on, whose inheritance relationship is shown in the following illustration.

The main functions provided by each of these are as follows:
Locale: This class contains the encapsulation of the geographical features of the major geographical areas. A particular object represents a particular geographical, political, or cultural area. By setting locale, we can provide specific countries or regions with fonts, symbols, icons, and expression formats that conform to local cultural habits. For example, we can display a date that matches a specific expression format by obtaining an instance of the Calendar class under a specific locale.
ResourceBundle: This class is an abstract class that requires a static method Resourcebundle.getbundle () to specify the base name of a specific implementation class or property file. The base name, in conjunction with the specified or default locale class, determines the unique name of the class or property file that is invoked specifically. For example, specifying a base class or property file name is called Testbundle, and the specified locale is Chinese, the class name best suited for matching is testbundle_zh_cn.class, and the best Bet property file name is called Testbundle_zh_ Cn.properties. As required by the Java DOC and related documentation, if the class or property file is not found, the system looks for an approximate match (the primary file name is Testbundle_zh and Testbundle's class or property file). The Getkeys () method provided by this class is used to obtain the key names of all members and provides the Handlegetobject method to obtain the corresponding element of the specified key.
ListResourceBundle: This class inherits the ResourceBundle class, mainly by adding some easy to manipulate components, but still abstract classes. If you want to use a class to implement specific ResourceBundle, it is generally preferable to inherit this class.
propertyResourceBundle: This class also inherits the ResourceBundle class and can be instantiated. The behavior characteristics of this class, like the Java.util.properties class, can obtain specific attribute pairs from the input stream.
If you are dealing with issues such as date and time display, you can use Java.text packages and classes such as TimeZone, SimpleTimeZone, and calendar in Java.util packages for secondary processing.
Parametric Solutions
In the specific application, can be the specific country or region characteristics can be parameterized part of the special named property file, after the determination of specific locale, through the propertyResourceBundle class read the corresponding property files, to achieve internationalization characteristics.
Use the propertyResourceBundle class to get the local version of internationalization information, some of the 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\t Value:" + value);
}
......
The specific contents of the Disp_zh_tw.properties document are as follows:
Key1=\u53ef\u4ee5
key2=\u64a4\u9500
After the equal sign is the use of NATIVE2ASCII program converted traditional Chinese characters, if not to convert, the system may display garbled.
Handling Hints and help
For the hint language and Help files section, you can place the language map in the properties file or in subclasses of the ListResourceBundle class. The following program is a servlet that returns information about a particular language and character version to the client by accepting the client's choice.
......
public class Processservlet extends HttpServlet
{//The default language is Chinese
public static final String default_language = "en";
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 for the specified language and characters received from the client should be consistent with sun company regulations
String lang = Req.getparameter
("language");
String Country = Req.getparameter
("Country");
if (lang = = null)
{
If no parameters are received, try to get the
Lang = (String) session.getattribute
("language");
Country = (String) session.getattribute
("Country")
} else {
Session.setattribute ("language", Lang);
Session.setattribute ("Country", country);
}
if (lang = = null)
{
If you cannot get language and character information from the above methods, 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 ("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 (""Out.println ("<br>");
Out.println ("<b>" + notice +
"</b>");
Out.println ("</body>");
Out.println ("}
}
The properties file used by the servlet above (DISPLAYLIST_ZH_CN.
Properties) reads as follows:
Title= Chinese Version
Welcome= This is the Simplified Chinese page
Notice= Simplified Chinese Test success
Note: This file is used directly in Chinese rather than converted Unicode encoding because most Web servers do not require these transformations.
In practice, if the Web server supports the Servlet 2.3 specification (such as Jakarta-tomcate 4.0), the aforementioned servlet should be slightly changed to use as a processor for other servlet. In addition, if you store a specific version of ResourceBundle in a stateless session bean, you can improve program efficiency to some extent.
Summary
The author found the following problems in the actual test, some of which were solved:
1. For display characters to appear garbled problem, if it is through the property file to achieve internationalization solution, then may be directly in the property file to write non-standard ASCII text. The solution is to use the tools provided by JDK to scan all the Native2ascii.exe files and overwrite the original file contents with the scan results. If we implement the conversion scheme using the class file, we need to recompile the related class file and specify the encoding set at compile time. For example, to compile a class file that uses GB code, the following compile command is used:
Javac-encoding GB2312 Your_java_file
2. Although Sun claims that during the instantiation of the ResourceBundle class, the class looks for classes that absolutely match the specified underlying class and try to match the specified locale property. For example, if we specify that the ResourceBundle base class is Testbundle, and ZH_CN is specified in locale (Chinese Simplified Chinese), then if the system cannot find TESTBUNDLE_ZH_CN, The system should look for Testbundle_zh and testbundle in sequential sequence. But the author discovers in the system development process, this match has not produced any actual effect.
The author's test platform is Windows Server, no service Pack is configured, and the JDK version used is version 1.3.0. The author attempts to find the cause of the problem by looking at the source code attached to the Src.jar in the JDK directory, but finds that the operation is encapsulated in the Sun.misc package, and the Src.jar file does not provide the source code for any of the classes in the package. This paper puts forward this question and hopes to discuss it with the developers concerned.


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.