项目中涉及到多语言版本时,往往需要做到能自由切换不同语言,比如中英文切换。Struts2框架已经封装了
国际化
相关流程,使用起来极其简单,Struts2需要用到
国际化
环境时具体请看Struts2
国际化
流程步骤。而要做到自由切换语言时,其实也很简单。
1.只要将request_locale这个参数传递到testAction即可,参数值为:xx_XX(语言_区域,如zh_CN)。
<ahref="/testAction.do?request_locale=zh_CN">中文简体</a>
<ahref="/testAction.do?request_locale=en_US">English</a>
2.Action无需任何操作,只要跳转到相应页面即可。
<span style="color:#0800; font-style:normal; font-weight:bold">/**
* FileName:TestAction.java
* @author ShelWee
*/</span>
<span style="color:#7f055; font-weight:bold">public</span> <span style="color:#7f055; font-weight:bold">class</span> TestAction <span style="color:#7f055; font-weight:bold">extends</span> ActionSupport <span style="color:#0990">{</span>
<span style="color:#7f055; font-weight:bold">public</span> <span style="color:#03399">String</span> execute<span style="color:#0990">(</span><span style="color:#0990">)</span> <span style="color:#7f055; font-weight:bold">throws</span> <span style="color:#03399">Exception</span><span style="color:#0990">{</span>
<span style="color:#7f055; font-weight:bold">return</span> <span style="color:#00ff">"success"</span><span style="color:#339933">;</span>
<span style="color:#0990">}</span>
<span style="color:#0990">}</span>
<spanstyle="color:#808080; font-style:italic"><!--struts.xml--></span>
<spanstyle="color:#0990"><spanstyle="color:#000000; font-weight:bold"><package</span><spanstyle="color:#0066">name</span>=<spanstyle="color:#ff00">"test"</span><spanstyle="color:#0066">extends</span>=<spanstyle="color:#ff00">"struts-default"</span><spanstyle="color:#0066">namespace</span>=<spanstyle="color:#ff00">"/"</span><spanstyle="color:#000000; font-weight:bold">></span></span>
<spanstyle="color:#0990"><spanstyle="color:#000000; font-weight:bold"><action</span><spanstyle="color:#0066">name</span>=<spanstyle="color:#ff00">"testAction"</span><spanstyle="color:#0066">class</span>=<spanstyle="color:#ff00">"testAction"</span><spanstyle="color:#000000; font-weight:bold">></span></span>
<spanstyle="color:#0990"><spanstyle="color:#000000; font-weight:bold"><result<spanstyle="color:#000000; font-weight:bold">></span></span></span>/test.jsp<spanstyle="color:#0990"><spanstyle="color:#000000; font-weight:bold"></result<spanstyle="color:#000000; font-weight:bold">></span></span></span>
<spanstyle="color:#0990"><spanstyle="color:#000000; font-weight:bold"></action<spanstyle="color:#000000; font-weight:bold">></span></span></span>
<spanstyle="color:#0990"><spanstyle="color:#000000; font-weight:bold"></package<spanstyle="color:#000000; font-weight:bold">></span
3.jsp页面根据当前语言显示文字,比如当前是中文,显示中文,当前是英文显示English。
<span style="color:#339933"><!--</span>test.<span style="color:#06633">jsp</span><span style="color:#339933">--></span>
<span style="color:#339933"><</span>s<span style="color:#339933">:</span><span style="color:#7f055; font-weight:bold">if</span> test<span style="color:#339933">=</span><span style="color:#00ff">
"%{getText('lang')=='中文' || #session.WW_TRANS_I18N_LOCALE.toString()=='zh_CN'}"
</span><span style="color:#339933">></span>
中文
<span style="color:#339933"></</span>s<span style="color:#339933">:</span>if<span style="color:#339933">></span>
<span style="color:#339933"><</span>s<span style="color:#339933">:</span>else<span style="color:#339933">></span>
English
<span style="color:#339933"></</span>s<span style="color:#339933">:</span>else<span style="color:#339933">></span>
PS:如果仅仅只有#session.WW_TRANS_I18N_LOCALE.toString()==’zh_CN’这个条件是不够的,因为用户第一次访问时,WW_TRANS_I18N_LOCALE并不存在于SESSION中。因此这里采取一个较为投机的方法,就是在国际化
资源文件中存放一个name为lang的值,然后在JSP页面中获取lang的值匹配。
--转自
该贴由koei123转至本版2015-7-14 11:05:45