Struts1 internationalization (1)

Source: Internet
Author: User
Tags i18n
The internationalization of software is also called I18nQuestion: Let's look at two cases.
Case 1: Google

First, let's take a look at Google's homepage.


As we all know, Google pages can be switched between Simplified Chinese, traditional Chinese, English, and other languages. How does one implement inter-language switching? Is a Google search engine developed for every language?
Of course not. Google supports so many languages. if you write a set of code for each language, it will be a lot of work, and it will not be easy to maintain. We all know that programming is an art that requires brains. Google has so many talented engineers who cannot use such stupid methods.
Case 2: Software LocalizationFor another example, we all know that the software is in Chinese. A software we download from the official website may not support simplified Chinese, but the great gods will provide us with a soft fake Chinese version and a better Chinese version, in this way, all English on the interface will be replaced with Chinese.
Think about one problem: we cannot obtain the source code of the software, so we cannot implement software localization by modifying the code. How is the software written into Chinese?
Through the above case, we can speculate that software supporting multiple languages, the text displayed on its UI is not written in the source code. It must exist independently of binary files outside the code.

What problems should Software Internationalization solve?In general Solve the hard coding problem, Specifically:
  1. Page string hard Encoding
  2. Hard encoding of exception messages
  3. Hard encoding of prompt information
Tip: Hard encoding means writing all strings to the program code.

So how can we solve these problems?
Simply put, all the strings used for display in the software are placed in the external "resource file". Each language corresponds to a resource file, allowing the software to read the resource file, the Chinese version of the software reads Chinese resource files, and the English version of the software reads English resource files.

The following describes how Java is internationalized.
Java internationalization is simple, and requires "two classes and one set of resource files"
  • Locale class

This class encapsulates the language and corresponding countries, such as zh_cn and en_us. When you get this type of object, you will know which language the resource file is used.


  • Resourcebundle class
This class can read strings in the resource file based on the basename of the resource file and the specified locale object

  • A set of resource files (. properties)
Resource files are simple and easy to use. They are simple plain text files. They consist of a series of key = value pairs, as shown below:
Chinese resource file messagesbundle_zh_cn.propertiesk1 = hellok2 = Good bye
English resource file messagesbundle_en_us.propertiesk1 = Hello k2 = goodbye

The code above demonstrates Java's International Implementation
Resource file


I18nexample class
Public static void main (string [] ARGs) {// create locale object locale = new locale ("ZH", "cn "); // create resourcebundle object resourcebundle RB = resourcebundle. getbundle ("com. tgb. i18n. resources. messagesbundle ", locale); // print the information in the resource file system. out. println (RB. getstring ("K1"); system. out. println (RB. getstring ("K2 "));}

Execution result

If locale specifies the English "en_us"
// Create the locale object locale = new locale ("en", "us ");

The execution result will be changed

How can I automatically identify the language type based on the language configuration of the operating system?
// Create the locale object locale = locale. getdefault ();
In this way, the software displays the language of the operating system.

Notes
  1. Resource file name: basename + Locale information. properties; as shown above: basename = messagesbundle; locale = "en_us" of the English resource file; "zh_cn" in Chinese"
  2. All resource files must use the same basename. The advantage is that you only need to obtain the locale information to determine which resource file to use.
  3. There is a resource file without Locale information. When the specified Locale information does not exist, this resource file will be used, as shown in messagesbundle. properties.

Struts's support for internationalization depends on Java's internationalization. later articles will introduce struts's support for internationalization.

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.