International Java applications

Source: Internet
Author: User

Prepared by: Xu jianxiang (netpirate@gmail.com)

Time:

From: http://www.anymobile.org

 

<Term>
Internationalization: internationalize (i18n for short) is a solution where applications can adapt to changes in different languages and regions.
Localization: localization, l10n check, refers to the process in which the application adapts to a specific language and region.

Software Internationalization is the inevitable outcome of software development. Almost all public software supports multiple languages. The most common is the Chinese version of English software.

The Java language provides unicode-based internationalization support. Use the Locale object to define the region, language, operating system, and other information, call the getBundle method of ResourceBundle to bind the data of resource files, and easily localize different language versions.

Through the definition of ResourceBundle and Locale, we can see that the java resource reading process is: language, country, variant-n-> language, country-n-> language-n-> Locale. getDefault ().

ResourceBundle. getBundle (String baseName)
ResourceBundle. getBundle (String baseName, Locale locale)

Locale (String language)
Locale (String language, String country)
Locale (String language, String country, String variant)

ResourceBundle stores local special objects in the form of key-value pairs, such as String, which can be divided into ListResourceBundle and PropertyResourceBundle.

The former is implemented through the. class program, and the latter is localized through the. properties configuration file, as shown below:

1. ListResourceBundle Example

1.1 default (Chinese in the test environment) Resource class

/**
* $ File: Rb. java $
* $ Revision: 1.0.0 $
* $ Date: 2:12:11 $
*/
Package org. anymobile. test;

Import java. util. ListResourceBundle;

/**
*
* @ Author Xu Jianxiang
*/
Public class Rb extends ListResourceBundle
{
Static final Object [] [] contents = {
{"CHG", "modify "},
{"MSG", "message "},
{"BYE", "goodbye "}
};

Public Object [] [] getContents ()
{
Return contents;
}
}

1.2 English Resources

/**
* $ File: rb_en.java $
* $ Revision: 1.0.0 $
* $ Date: 2:14:37 $
*/
Package org. anymobile. test;

Import java. util. listresourcebundle;

/**
*
* @ Author Xu jianxiang
*/
Public class rb_en extends listresourcebundle
{
Static final object [] [] Contents = {
{"Chg", "change "},
{"MSG", "message "},
{"Bye", "bye "}
};

Public object [] [] getcontents ()
{
Return contents;
}
}

1.3 Test class

/**
* $ File: mylistresourcebundle. Java $
* $ Revision: 1.0.0 $
* $ Date: 2:24:30 $
*/
Package org. anymobile. test;

Import java. util. Locale;
Import java. util. MissingResourceException;
Import java. util. ResourceBundle;

/**
* Test java. util. ListResourceBundle
*
* @ Author Xu Jianxiang
*/
Public class MyListResourceBundle
{
Public static void main (String [] args)
{
String baseName = "org. anymobile. test. Rb ";
String key = "BYE ";
Try
{
ResourceBundle res = ResourceBundle. getBundle (baseName );
System. out. println (res. getString (key ));

Res = ResourceBundle. getBundle (baseName, Locale. ENGLISH );
System. out. println (res. getString (key ));
}
Catch (MissingResourceException exp)
{
Exp. printstacktrace ();
}
}
}

Running result:

Goodbye
Bye

2. PropertyResourceBundle Example

2.1 English configuration file ({project}/classes/resources/rb_en.properties)

# Rb_en.properties

Chg = change
MSG = message
Bye = bye

2.2 Chinese configuration file ({project}/classes/resources/rb_zn_CN.properties)

# Rb_zn_cn.properties

Chg = Modify
MSG = message
Bye = goodbye

2.3 Test class

/**
* $ File: myresourceboundle. Java $
* $ Revision: 1.0.0 $
* $ Date: 1:19:06 $
*/
Package org. anymobile. test;

Import java. util. locale;
Import java. util. missingresourceexception;
Import java. util. resourcebundle;

/**
* Test java. util. propertyresourcebundle
*
* @ Author Xu jianxiang
*/
Public class mypropresourcebundle
{
Public static void main (string [] ARGs)
{
String basename = "resources. RB ";
String key = "chg ";

Try
{
// ResourceBundle res = ResourceBundle. getBundle (baseName, Locale
//. GetDefault ());
ResourceBundle res = ResourceBundle. getBundle (baseName, new Locale ("zh", "CN", "WINDOWS "));
System. out. println (key + ":" + res. getString (key ));

Res = ResourceBundle. getBundle (baseName, Locale. ENGLISH );
System. out. println (key + ":" + res. getString (key ));
}
Catch (MissingResourceException exp)
{
Exp. printStackTrace ();
}
}
}

Running result:

CHG: Change
CHG: change

Then run the command native2ascii src.txt DeST. properties. After conversion, for example, the Unicode escape code of "China" is "/u4e2d/u56fd ".
To restore Unicode to the original encoding, run the command native2ascii-reverse DeST. File SRC. File.

The native2ascii method must be run each time. In actual development, you can use the native2ascii task of Apache ant for batch transcoding. For example: <native2ascii encoding = "GBK" src = "$ {SRC}" DEST = "$ {build}"/>

3. Internationalization of spring messages

Org. springframework. Context. applicationcontext inherits from the org. springframework. Context. messgeresource interface and reads the resource files of text messages from various versions of getmessage () to internationalize messages.

Public interface messagesource
{
String getmessage (string code, object [] ARGs, string defaultmessage, locale );
}

References:

Java 2 SDK document
How to internationalize Java applications (Sun) http://java.sun.com/docs/books/tutorial/i18n/index.html
ISO Language Code http://www.ics.uci.edu/pub/ietf/http/related/iso639.txt
ISO Country Code http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

 

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.