Struts1 international processing (summary order)

A, java in the international

a) jave to implement the principles of international

java Locale object can be specified by the current system or get the default configuration of the international language code and country code, then get the corresponding Lacale object ResourceBundle class international profile that Properties file, the value realized by reading the key international.

b) java in the class needed to achieve international

locale categories:

You can specify the language to be displayed, the corresponding international documents

Locale class methods:

/ / Get the current system default encoding

Locale l = Locale.getDefault ();

/ / Custom Locale

/ / Parameter 1: The language code en (English) zh (Chinese) ja (Japanese)

/ / Parameter 2; country code US (UK) CN (China) JP (Japan)

Locale l1 = new Locale ("en", "US");

/ / Get the country code

l.getCountry ();

/ / Get the language code

l.getLanguage ();

ResourceBundle class:

Locale can read the corresponding property file corresponding

c) change the system default encoding international

Control Panel - Regional Language

d) change the default encoding in IE

IE Preferences - Language

e) property file name written rules

Basename Name _ _ country code language code. Properties

Description: BaseName name, but you can customize the name behind the above rules must be written

f) to read the corresponding Properties file

/ / Read the file corresponding to the Properties

/ / The first parameter corresponds to the appropriate basename

/ / The second parameter corresponds to the appropriate Locale

ResourceBundle rb = ResourceBundle.getBundle ("Message", l);

rb.getString ("key value");

g) Properties file garbage problem in Chinese

Files in the Properties button to read the corresponding key will appear garbled in Chinese jdk's native2ascii.exe tools should be used to convert the Chinese unicode code file and then write Properties

h) Native2ascii. exe to use

Jdk's bin directory in the double-native2ascii.exe copy will need to convert the Chinese to enter the command prompt to see the conversion under the unicode encoding to replace the value of the corresponding key to

Eg:

Hello to a =

Replaced by a = \ uf60 \ u597d

Batch Replace

Native2ascii.exe source file paths with file path

The system will automatically set according to the control panel to convert the international code.

i) Properties file placeholder problem

Properties file {0} can be used for placeholder {1} set note start from 0

Placeholder

MessageFormat mf = new MessageFormat (rb.getString ("key name"));

String result = mf.format (new Object [] {"a", "b"});

j) the issue of internationalization path to the file

ResourceBundle rb = ResourceBundle.getBundle ("Message", l);

The default is to read the files under src, if the path should be marked paths of different

Methods: msg +. + Properties file BaseName name

Eg: msg.Message

k) Note:

ResourceBundle default Locale based on parameters of the country code and language code automatically match the corresponding Properties file for reading (so named properties file must be legal).

If the file is not found the appropriate international load the system default configuration file (read the system settings of the country code and language code) if the system does not set the corresponding properties file is loaded Basename name. Properties files. If the files do not correspond to the error.

l) Note 2: New test java project source src folder must be established or an error.

Second, Struts hard-coded international

a) Stuts that is hard-coded international text on the same page for international treatment.

b) use

i. in the struts-config.xml: add file

<message-resources parameter="MessageResources" />

The first parameter and the name of the Properties file BaseName

ii. in the properties file to configure the relevant field, note the Chinese want to convert to unicode code

iii. page using Struts tags in replacement of the contents of the international

<bean:message key=" ">

c) Note: Load internationalization find a file if no relevant documents (IE the international configuration) is the language of the operating system configuration would look if it is not the default file that is loaded Basename.properties file.

d) achieved by modifying the session manually modify the language

request.getSession (). setAttribute (Globals.LOCALE_KEY, currentLocale);

The second parameter of the Locale object for the custom.

Or:

this.setLocale (request, currentLocale);

The second parameter of the Locale object for the custom

Third, the dynamic text international

1, the method

Action is defined in the general information and error messages

ActionMessage m = new ActionMessage ("upass", new Object [] {"pppp"});

In the IE tools -> Internet Options -> Language of the place, to select, define the language of IE.
Third, the form of data processing.
Form data for processing, we are by adding a Filter to achieve. All submitted requests will need to do character processing. Then in the web.xml which defines the Filter. So we do not do anything in the program which the character processing.
3.1 The definition of Filter. Here is an example.
package com.webapps.commons;
import java.io. *;
import javax.servlet .*;
public class CharsetEncodingFilter implements Filter {
private FilterConfig config = null;
private String defaultEncode = "UTF-8";
public void init (FilterConfig config) throws ServletException {
this.config = config;
if (config.getInitParameter ("Charset")! = null) {
defaultEncode = config.getInitParameter ("Charset");
}
}
public void destroy () {
this.config = null;
}
public void doFilter (ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest srequest = request;
srequest.setCharacterEncoding (defaultEncode);
chain.doFilter (srequest, response);
}
}
3.2 web.xml which declared the use of the Filter
<filter>
<filter-name> Character Encoding </ filter-name>
<filter-class> com.webapps.commons.CharsetEncodingFilter </ filter-class>
</ Filter>
<filter-mapping>
<filter-name> Character Encoding </ filter-name>
<url-pattern> / * </ url-pattern>
</ Filter-mapping>
Fourth, continued expansion is part of the application of international issues, and the DB's internationalization.

For the international handling of the Struts users, a support international programs have the following several forms of WEB

1. According to the user's browser automatically set the display language (without user intervention)
2. To provide the user to select the user according to their need to decide what language to use
3. With the first two. The system automatically selects a language, but also provides the user need to select

Development of an international program with Struts is a very convenient thing, we look at how to achieve the first two

1. This is the easiest way, you do not modify any program, you must file in accordance with the various language translation resources, and these files again in accordance with international requirements of the program the same directory name can be a good place. For example ApplicationResource_en_US.properties ApplicationResource_zh_TW . properties. Although this method automatically set to be displayed to the user's language, but sometimes the browser itself, or the user you want to see other languages ​​when there is no way.

2. This is a more common way, by the user to select the language to display. General situation is in the home page or login page to add language options, the user select one of the registry after the user selected language display, but users must be logged in the language selected by the user and let the program saved is <bean: message tags which use the language of choice load configuration information, so the user logs on the implementation of the Action class to add the following code

Locale locale = new Locale (request.getParameter ("locale"));
/ / TODO: determine the effectiveness of locale, null and void if not stored in the sesssion
req.getSession (). setAttribute (Globals.LOCALE_KEY, locale);

Well, now <bean:message/> the tag library you can now save the Locale object in the session to determine what kind of loaded language, by looking at the source tag library <bean:message/> we can clear that the tag library to load resources in the session before the start or the Locale object, if empty, use request.getLocale () to load this value corresponds to the language resources, which in practice is to achieve the previously mentioned the third form. Then there is the translation of resource files, like the first mentioned.

Struts framework to achieve when the international application or for the very thoughtful we want, so you need to do is translate.

Finally, remember two things:
1. Page character set must be UTF-8, for example:
<% @ Page language = "java" contentType = "text / html; charset = UTF-8"%>
2. In addition to all the tips page should be defined in the resource file
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Struts1 international processing (summary order)

  • 6. Configuration JBoss data source

    M in the main SQL as an example: (1): add driver package Put MySQL driver mysql-connector-java-5.0.8-bin.jar copied to the% JBOSS_HOME% / server / default / lib directory (the default configuration is the default, can be altered according to the actu ...

  • JavaEye chat client [ruby]

    Ruby recent study at the GUI tool Shoes, want to do with a ruby and JE Talker similar instrument, so the first to write a JavaEye the Client. Certification has been completed, get_all (access to site-wide chat), get_list (to obtain their own subscriptions

  • python vs ruby

    Object Language 1.python are weak, and ruby are pure object language python can not object to run, but the ruby is the foundation of Object. And python packages bound for the relatively weak, while the relative strength of ruby. Such as a class example of

  • python vs javascript

    python structural body (including class, function and class instances) and the javascript of the structure (Object and function) to achieve somewhat similar, but also somewhat different. python the class of data and functions around the store are Victoria

  • ajax garbled solution summary

    First, javascript use java the way the characters deal with, the internal use are to handle all unicode characters, Second, utf-8 characters are of each (unicode characters) to use 3 bytes to store. Third, using utf-8 to send data is not garbled, and ...

  • bdb: to improve the Berkeley DB for Ruby Bindings

    Berkeley DB Are a high-performance database system, which in the 20th century, early 90's development, he is not a SQL database engine driver, but used to store key / value pairs of data. BDB very fast, can be at most of the operating system to u ...

  • javascript object-oriented technology foundation (1)

    Read a lot of javascript introduction of object-oriented technology article, I am very dizzy. Why? Not because of poorly written, but because too esoteric. javascript objects not explain how matter, went one up on the subject, class / inheritance / p ...

  • Ext JS FormPanel summary of the submission of data ....

    Returns the fields in this form as an object with key / value pairs as they would be submitted using a standard form submit. If multiple fields exist with the same name they are returned as an array. Parameters: * AsString: Boolean (optional) false t ...

  • Javascript in the browser environment (seven) XMLHttpRequest

    XMLHttpRequest XMLHttpRequest is a function used to create a http request. XHR initial IE through ActiveX objects are realized. After the beginning of the various browsers support. Now AJAX is the popular XMLHttpRequest object to the adoption of the imple

  • Imitation Java type HashMap implementation of JavaScript

    Imitation Java type HashMap implementation of JavaScript

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries