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

分类:Java 时间:2011-05-05 人气:113
分享到:
blog comments powered by Disqus

相关文章

  • JSP development of Chinese language garbage problem 2010-03-29

    This paper mainly in the JSP for Tomcat development of Chinese garbled prone to problems. 1, JSP garbage problem in the output of Chinese JSP output of Chinese, that is, the output directly in the JSP in Chinese, or assign to a variable the value of

  • Myeclipse8.0/8.5 loss and default workspace configuration issues 2010-04-17

    A few days ago newly installed Myeclipse8.5, the first open, select the workspace is no longer prompt the option I chose. Spent many days without any problems, today's projects do not remove the previous close Myeclipse, Again opened Myeclipse someho

  • java.sql.SQLException: Incorrect key file for table 2010-08-28

    Previously to be a movie site on the captured movies need to imdb, actor and other relevant data. Swing sets made outside the network machine while ripping. Lead to excessive access to imdb. Beginning as a browser only the first visit, the results to

  • java集合map取key使用示例 java遍历map 2013-11-04

    这篇文章主要介绍了java集合map取key使用示例,需要的朋友可以参考下 for (Iterator i = keys.iterator(); i.hasNext() { String key = (String) i.next(); String value = (String) map.get(key); text+=key + " = " + value; } 复制代码 代码如下: <span style="border-collapse: collapse;

  • Java / JSP Chinese garbage problem-solving experience (change) 2010-06-09

    From: http://www.xici.net/u9206704/d56632455.htm Since access to Java and JSP has been with the Java on the issue of Chinese garbage deal, and now finally been completely resolved, we now experience and share solutions. 1, Java the Chinese origin of

  • [Change] Java Traditional Chinese processing (1) 2010-03-29

    Many people use Java processing to Chinese data, often garbled. With regard to Java and Chinese compatibility issues, it makes many programmers were racking their brains, the relevant issues will appear every few days on the Internet. In order to eas

  • Java / JSP Chinese garbled problem solving experience 2010-11-25

    Since the exposure to Java and JSP has to constantly deal with Java's garbage problem in Chinese, and now finally been completely resolved, we now experience and share solutions. One, Java the Chinese origin of the problem Java class files and the ke

  • windows system environment variable configuration under JDK1.6 2010-04-08

    windows system environment variable configuration under JDK1.6 1, JDK1.6 download Currently the latest version of JDK is JDK1.6, to http://java.sun.com/javase/downloads/index.jsp can download JDK1.6. Second, JDK1.6 installation JDK installation is ve

  • windows system environment variable configuration JDK1.6 2010-04-08

    windows system environment variable configuration JDK1.6 A, JDK1.6 download Currently the latest version of JDK is JDK1.6, to http://java.sun.com/javase/downloads/index.jsp can download JDK1.6. Two, JDK1.6 installation JDK installation is very simple

  • Linux system installation and configuration of Apache Subversion 2010-12-23

    Linux system installation and configuration of Subversion First, the first related to the installation package is ready, I'm using the following several packages Apr: apr-1.3.8.tar.gz and apr-util-1.3.9.tar.gz Apache: httpd-2.2.14.tar.gz Subversion:

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.810 (s). 12 q(s)