How to change a normal struts2 project to support multi-language

You can do it step by step as follow.

Step 1, Detect if the encoding of the project is UTF-8. If not, change its encoding to UTF-8.
Step 2, Add configuration in struts' config file[src/struts.xml].
    <constant name="struts.custom.i18n.resources" value="messageResource" />
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.locale" value="en_US"> </constant>
Step 3, Add propertites files to src. the file name's format is messageResource_lang_county.properties. Take chinese and english for example:
    messageResource_zh_CN.properties, messageResource_en_US.properties...
The content's format in property file is key/value, and one pair per line:
    Login.Login=Login
    Login.Language=Language
    HelloWord={0},Hello World
    ...
Step 4, Replace labels you want to change in jsp page with the struts tag as follow methods.
   1, <s:property value="%{getText('Login.Login')}"/>
   2, <s:text name="Login.Language"/>
   3, <s:text name="HelloWord">
           <s:param>Mike</s:param>
       </s:text>
Step 4, Replace strings you want to change in action as follow methods.
    1, getText("Login.Language")
    2, getText("HelloWord", {"Mike"})
At this time, The page's language will be same as your browser's language. And if you want to change the language manually, you should do the next step.
Step 5, Add a action to change the current Locale.
     request.getSession().setAttribute("WW_TRANS_I18N_LOCALE", currentlocale);


  Wish it can help you.

 

你可能感兴趣的:(language)