Spring Internationalization web MVC
1. Add two properties files in /WEB-INF/classes:
1) messages_zh_CN.properties Chinese message file.
2) messages_en_US.properties English message file.
2. Add bean in action-servlet.xml:
[bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"/]
Spring will set UI language locale and message according to the client-side browser locale configuration.
3. JSP files
1. [fmt:message key="key1"/]
2. Error message
[spring:bind path="account.password"]
[input type="password" name="${status.expression}" value="${status.value}"/]
[span class="fieldError"]${status.errorMessage}[/span]
[/spring:bind]
3. button
[input type="submit" class="button" name="prev" value="[fmt:message key="prev"/]" /]
Spring Internationalization for portlet MVC
1. Add two properties files in /WEB-INF/classes:
1) messages_zh_CN.properties -Chinese message file.
The Java properties files are created using a native encoding or in UTF-8. This must be converted to an ascii format for Java. This is done as part of the extraction process on the translation website but if you have a manually created file in UTF-8 that needs to be encoded, then use the native2ascii command that comes with with Java JDK.
Call native2ascii to ascii encoding, specifying the original file has UTF-8 encoding:
Eg: native2ascii.exe -encoding UTF-8 chinese_zh.txt ApplicationResource_zh_CN.properties
2) messages_en_US.properties -English message file.
2. Update applicationContext.xml file as below:
- Replace the static text in JSP files with
[spring:message code=”key1”/] where key1 is the key available in message_xx.properties file
Note :The Java properties files are created using a native encoding or in
UTF-8. This must be converted to an ascii format for Java. This is done
as part of the extraction process on the translation website but if you
have a manually created file in UTF-8 that needs to be encoded, then
use the native2ascii command that comes with with Java JDK.
Call native2ascii to ascii encoding, specifying the original file has UTF-8 encoding:
native2ascii.exe -encoding UTF-8 chinese_zh.txt ApplicationResource_zh_CN.properties
Ref: http://wiki.lamsfoundation.org/display/lams/LAMS+i18n
The below JSP is Useful to troubleshoot locale related issues:
[%
//java.util.Locale locale = (java.util.Locale)request.getSession().getAttribute
("org.apache.struts.action.LOCALE");
Locale locale = renderRequest.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
out.println(language +" "+country);
// Remember, messagesis the fully qualified name of a bundle in the classpath
// e.g. WEB-INF/classes/messages_*.properties
ResourceBundle res = ResourceBundle.getBundle("messages", new java.util.Locale(language, country));
java.util.Enumeration en=res.getKeys();
while(en.hasMoreElements())
{
out.println(en.nextElement());
}
out.print(res.getString("key1"));
%]
No comments:
Post a Comment