If the application system is facing multiple languages, it has to be trunked to solve international problems, including the style of operation interface, prompts, and version issues of the help language, and the interface is customized.
Since the Java language has the advantages of platform-independent, portability, and provides a powerful class library, Java language can assist us to solve the above problems. The Java language itself uses double-character character encoding, using a large Chinese character set, which provides a lot of convenience for solving internationalization. From the design perspective, as long as the program is separated from the language and cultural part, add special processing, it can be partially resolved. In terms of the customization of the interface, we can use the elements of the parameters such as font, color, etc. to store in the database to provide users with a friendly interface; if some parts contain elements that cannot be parametric, then we may have to Designed separately, specific issues are solved by targeted coding.
Java package
In the process of solving internationalization with Java, the main classes that may be utilized are provided by the java.util package. The class related to the class is Locale, ResourceBundle, ListResourceBundle, PropertyResourceBundle, etc., its inheritance relationship is shown below.
The main functions provided by all kinds are as follows:
Locale: This class contains a package of geographical feature of the main geographic area. Its specific object represents a particular geographic, political or cultural area. By setting Locale, we can provide fonts, symbols, icons, and expression formats that meet local cultural habits for specific countries or regions. For example, we can display the date that meets a specific expression format by getting an instance of a Calendar class under certain Locale.
ResourceBundle: This class is an abstract class that requires the basic name of the specific implementation class or attribute file by static method resourcebundle.getBundle (). Basic names will collaborately specify or default Locale classes to determine the unique names of the specific call or attribute file. For example: Specifying a base class or attribute file name TestBundle, and the specified local is Chinese, then the most suitable matching class name is testbundle_en_cn.class, and the best matching property file name is TestBundle_zh_cn.properties. According to Java DOC and related documents, if the class or attribute file is not found, the system will find approximation match (the main file name is the class or attribute file of Testbundle_ZH and TestBundle). The GetKeys () method provided by this class is used to get the key names of all members and provide the HandlegetObject method to get the corresponding element of the specified key.
ListResourceBundle: This class inherits the ResourceBundle class, mainly adding some components that are easy to operate, but still abstract classes. If you want to use classes to implement specific ResourceBundle, it is best to inherit this class.
PropertyResourceBundle: This class also inherits the ResourceBundle class, which can be instantiated. The behavior characteristics of this class are like the Java.util.Properties class, which can obtain specific properties from the input stream.
If there is a problem such as date and time display, you can use the Java.Text package and the Timezone, SimpleTimezone and Calendar class in the java.util package.
Parameterization solution
In the specific application, the part of the specific country or regional feature can be parametric can be placed in a specially named attribute file. After determining the specific Locale, the corresponding attribute file is read through the PropertyResourceBundle class to achieve international characteristics.
Use the PropertyResourceBundle class to get the internationalized information of the local version, part of the code as follows: ......
Public static final string base_prop_file =
"DISP";
Public Static Final String Suffix =
".Properties";
Locale = locale.getDefault ();
String propfile = base_prop_file "_" local.toTString () SUFFIX;
ResourceBundle RB;
Try {
File file = new file (propFile);
IF (file.exiss ()) {
IS = new fileinputstream (file);
RB = New PropertyResourceBundle (IS);
IF (rb == null) System.out.Println ("no resource");
}
} catch (ioexception ie) {
System.out.println ("Error Open File Named" PropFile;
}
ENUMERATION E = rb.getKeys ();
While (E.haASMoreElements ()) {
Key = (string) E.NEXTELEMENT ();
Value = (string) rb.handlegetObject (key);
System.out.println ("Key:" Key
"/ T / t value:" value);
}
......
The specific content of the DISP_ZH_TW.PROPERTIES file is as follows:
Key1 = / u53ef / u4ee5
Key2 = / U64A4 / U9500
The equal number is used to transform the traditional Chinese characters after the Native2ASCII program, and if not conversion, the system may display garbled.
Handle tips and help
For the prompt language and help file part, you can map the language map in the subclass of the property file or the ListResourceBundle class. The following program is a servlet, which returns the information of the particular language and the character version to the client by accepting the selection of the client.
......
Public Class ProcessServlet Extends httpservlet
{// Default language is Chinese
Public static final string default_language = "zh";
// 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.getations (TRUE);
// The parameters of the specified language and characters received from the client should be consistent with the relevant regulations of Sun.
String lang = Req.getParameter
("Language");
String country = Req.getParameter
("Country");
IF (lang == null)
{
// If you don't receive a parameter, try to get from the session.
LANG = (string) session.getattribute
("Language"); country = (string) session.getattribute
("Country")
} else {
Session.SetaTRibute ("Language", LANG);
Session.SetaTRibute ("Country", Country;
}
IF (lang == null)
{
// If you cannot get the language and character information from the above means, you will use the default value.
LANG = default_language;
Country = default_country
Session.SetaTRibute ("Language", LANG);
Session.SetaTRibute ("Country", Country;
}
Locale local = 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" local);
Bundle = ResourceBundle.getBundle
("DisplayList", Locale.us;
}
Res.SetContentType ("text / html");
PrintWriter out = res. maxwriter ();
Out.println ("");
Out.println ("
");String title = bundle.getstring ("Title");
String welcome = bundle.getstring
("Welcome");
String notice = bundle.getstring ("notice");
Out.println ("
" Title>");
Out.println (" HEAD>");
Out.println ("
White / ">");
Out.println ("
" H3>");
Out.println ("
");
Out.println ("" NOTICE
" B>");
Out.println (" Body>");
Out.println (" html>");
}
}
The attribute file used in the above servlet (DisplayList_ZH_CN.
Properties) The content is as follows:
Title = Chinese version
Welcome = This is a Simplified Chinese version
Notice = Simplified Chinese test success
Note: This file uses Chinese directly, not transformed Unicode encodings, because most web servers do not require the above conversion. In actual use, if the web server supports servlet 2.3 specification (such as Jakarta-Tomcate 4.0), then the servlet mentioned above should change slightly to use the processor of other servlets. Also, if you put the specific version of ResourceBundle in a stateless session bean, you can improve program efficiency to some extent.
Small knot
The author discovered the following questions in the actual test, some of which have been resolved:
1. For the problem of garbled code, if an international solution is implemented through the attribute file, it is possible to write non-standard ASCII text directly in the properties file. The solution is to use the tools provided by JDK native2ascii.exe to scan all attribute files and overwrite the original file content with the scan results. If we use the class file to implement the conversion scheme, you need to recompile the relevant class file and specify the encoding set when compiling. For example, compiling class files using national codes, the compile commands employed are as follows:
Javac -ENCODING GB2312 Your_Java_File
2. Although Sun claims, in the instantiation of the ResourceBundle class, this class finds a class that matches the specified base class and try to match the specified Locale property. For example: If we specify the ResourceBundle base class for TestBundle, and local is specified in ZH_CN (China Simplified Chinese), then if the system can't find TestBundle_zh_cn, the system should look up TestBundle_zh, TestBundle. However, the author found during system development, and the match did not produce any actual effect.
The author's test platform is Windows 2000 Server. No service pack is configured, the JDK version used is 1.3.0. The author attempts to find a problem caused by viewing the source code included in the JDK directory, but found that the relevant operation is encapsulated in the sun.misc package, and the src.jar file does not provide any class of the source code in the package. This article briefed this problem and hopes to discuss with relevant developers.