I used internationalization in my previous project. It may not be the best method, but I 'd like to summarize it for my reference!
It mainly includes struts2 Internationalization
To set struts2 internationalization:
1. Add the configuration in the Struts. xml file:
<Constant name ="Struts. Custom. i18n. Resources"Value ="Message"> </Constant>
Here, message is the main file name of the International file.
2. Add an international file:
Message_en_us.properties;
Message_zh_cn.properties acts on the global;
Use package (replace message) as the file name to act on the package where the file is located;
If the name of a specific action is the file name, the action class is applied.
3. obtain the corresponding resource:
Obtain from common classes:
Java. util. resourcebundle RB = java. util. resourcebundle. getbundle ("anyname", locale); RB. getstring ("K1"); // obtain the corresponding key information
Get in action:
Gettext ("username. Invalid ")
Get on the page:
<s:text name="hello"></s:text><message key="username.xml.invalid"></message> <s:textfield name="username" key="username.name"></s:textfield> <s:i18n name="temp"></s:i18n>
Normal page (non-struts2 returned page) obtained (using jstl ):
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix ="fmt"%><fmt:setBundle basename= "message" var ="msg"/><fmt:message key ="pageerror.info" bundle ="${msg} "></fmt:message >
4. Add a manual language change:
First, add a button on the page to switch between Chinese and English
Then add js to respond to the click event:
$(".i18n_english").click( function() { $.ajax({ type: "POST", url: "i18n_ajax", data:{flag: "english"}, dataType: "json", async: true, success: function(json){ window.location.reload(); }, error: function(json){ alert( "ERROR!!!..." ); } }); });
$( ".i18n_chinese").click(function () { $.ajax({ type: "POST", url: "i18n_ajax", data:{flag: "chinese"}, dataType: "json", async: false, success: function(json){ window.location.reload(); }, error: function(json){ alert( "ERROR!!!" ); } }); });
Add action response JS:
Public String transform () {locale = (locale) actioncontext. getcontext (). getsession (). get ("ww_trans_i18n_locale"); If (null = This. flag) {locale = locale. US;} else if ("Chinese ". equals (this. flag) {locale = locale. china; // locale. simplified_chinese} else if ("English ". equals (this. flag) {locale = locale. US;} // It is very important to add the international information for setting the action. If this information is not added, You need to click the button on the page twice to convert the actioncontext normally. getcontext (). getsession (). put ("ww_trans_i18n_locale", locale); Return success;} In struts. configure in XML: <action name = "i18n_ajax" class = "i18naction" method = "transform"> <result type = "JSON"> <Param name = "includeproperties"> flag </param> </result> </Action>
You also need to write a filter to set the locale information to the currently saved information when responding to the access request.:
public class I18nFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //Locale locale = MyLocale.getMylocale(); Locale locale = (Locale)((HttpServletRequest)request).getSession().getAttribute("WW_TRANS_I18N_LOCALE"); if(locale!=null){ ((HttpServletRequest)request).getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale); }else{ ((HttpServletRequest)request).getSession().setAttribute("WW_TRANS_I18N_LOCALE", Locale.US); } chain.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { }}
Do not forget to configure this filter in Web. xml.:
<!-- i18n filter --> <filter> <filter-name> i18n</ filter-name> <filter-class> com.proton.filter.I18nFilter</filter-class > </filter> <filter-mapping> <filter-name> i18n</ filter-name> <url-pattern> /*</ url-pattern> </filter-mapping>
OK ~!
This article is from the "demon7c technology blog" blog, please be sure to keep this source http://zhanghongchao.blog.51cto.com/3777224/1566023
Summary of internationalization-about struts2