Tiles2 Struts Switch Locale

只谈情不闲聊 提交于 2019-12-18 09:49:09

问题


I am newbee to struts2 and tiles2, I am having an application where i m using struts2 as mvc framework and tiles2 as a view component. In my application I am using 2 languages English and Marathi the respective locales are en_US and mr_IN I have tiles.xml and tiles_mr_IN.xml when i switch locale from browser the code works fine but i want to toggle between English and Marathi and the user should be redirected to same action with same request parameters. I have tried satting request_locale parameter but it didnt work. I could find that if i could switch locale in the request header or pass locale from struts2 to tiles lisner or some thing like that please help


回答1:


This issue should be resolvable with a couple s2 jsp tags.

This link shows part of what you need: http://struts.apache.org/2.0.14/docs/how-do-we-change-locales.html

That is it shows how to take a s2 url add a parameter and then use that to set the locale.

If there is a parameter called "request_locale" coming in the i18n interceptor will use that over the user agents language preference.

To make the example better suited for a template change

<s:url id="en" action="Welcome">

to

<s:url includeParams="get">

Notice the action name has been omitted, this will default to the current action (which is ideal for your template) next the includeParams="get" will add all the parameters back into the url so they'll be passed into the action when you use the anchor. The includeParams may be "none", "get" or "all" (doing what you would expect). For more information on the s2 url tag see: http://struts.apache.org/2.2.1.1/docs/url.html

Further note that the s2 a tag shares most of the same funcationality as the s2 url tag taking advantage of this will produce:

<s:a includeParams="get">
  <s:param name="request_locale" value="en_US"/>
  English
</s:a>
&nbsp;
<s:a includeParams="get">
  <s:param name="request_locale" value="mr_IN"/>
  Marathi
</s:a>


来源:https://stackoverflow.com/questions/4773063/tiles2-struts-switch-locale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!