Struts1.x Series Tutorials (13): Internationalization

Source: Internet
Author: User
Tags http request locale

For a global Web application, it is particularly important to display relevant information (called internationalization) according to the requirements of different countries. The internationalization of the work is very complex and cumbersome. Because to translate a lot of web interface, information format and so on. However, struts provides us with a much easier way to do international work. In this article, I'll explain how to use struts to simplify internationalization work.

First, processing the Client interface coding problem

Because Web browsers can use different encoding formats to resolve client code, this depends largely on the user's default settings or preferences. Because of this situation, when the server sends data to the client, it must use a consistent encoding format with the client.

The common practice is to use the more commonly used UTF-8 encoding on the internet, mainly because the characters described in the UTF-8 encoding format include all the world's language encodings in the known country and region. This way, the server does not have to constantly adjust the encoding format of the service-side response information according to the client's encoding format. For JSP pages, you can use page designation to set the encoding format for the response information, as shown in the following code:

<%@ page contenttype= "text/html; Charset=utf-8 "%>

Of course, if we use the Struts framework in a Web program, we can also use the following code in the Struts-config.xml file to set the encoding format:

<controller contenttype= "text/html; Charset=utf-8 "/>

If you use the above setting, all pages accessed through the. Do or <forward> elements inherit this encoding setting. However, if you visit the JSP page individually, you will bypass this setting of struts.

For static pages, such as HTML, you need to use the <meta> element to set the encoding format, as shown in the following code:

    <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">

In addition to using the generic UTF-8 encoding format, you can obtain the encoding format of the client from the HTTP request header field Accept-charset. The encoding format of the response information is then set according to this encoding format. However, although the Web browser can set the Accept-charset field according to the HTML4.0 specification, there are many browsers that can ignore the field, so use this field only to determine that the encoding format used by the Web browser is unreliable. So the best international coding scheme solution is still UTF-8.

Second, localization attribute file

The most important part of localization is to display the response language interface according to the user's locale, for example, the English interface will be displayed in the English operating system, and the Chinese interface will be displayed if the Chinese operating system is in place. Ideally, the switching of this display effect is completely transparent.

This technology is provided in struts, making it easier for us to achieve transparent interface switching. There is a <bean:message> tag in the Struts Bean tag library that automatically selects the corresponding property file to read the interface display information according to the locale in which the client browser is in. A program that demonstrates how to use <bean:message> and different property files to achieve internationalization. Let's take a look at an example first.

In this example, the locales of three countries (Chinese, English, and French) are selected to demonstrate internationalization implementations. Since the reader may not be able to facilitate the input text, the French information is still replaced in Chinese, with the following "(French)" as the logo. We need the following four steps to complete this example:

"Step 1th" to create a default property file

Create a application.properties file in the >\src\struts directory of the <samples Engineering directory, which reads as follows:

Title= This is an internationalized Web program (default value)

Name= name (default value)

submit= commit (default value)

The Application.properties file is the default property file, and if the locale in which the client browser is located does not find the corresponding property file on the server,<bean:message> The label reads the string information from the Application.properties file.

When editing characters in non-ISO 8859-1 encoded format in eclipse, you should use the two Eclipse Plug-ins described in the workaround for the property (Resource) file garbled problem, or use the Native2ascii.exe command to convert. Otherwise, the information in the property file will appear garbled.

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.