How to apply i18n on whole website by a single click in Struts 2

落花浮王杯 提交于 2019-12-11 11:06:56

问题


Is it possible to apply Struts 2 Internationalization (i18n) for changing language from English to Hindi on whole web pages of website by single click?

If it is possible then how can I resolve this problem?


回答1:


The framework is internationalized.

You need to add the corresponding to each locale resource bundles for the localized messages that you display via the struts tags. Use text tag or getText() to retrieve a message in the UI.

The browser language is passed with the HTTP request and framework create a locale corresponding to the browser settings. Switching locale performed via passing a special parameter request_locale to i18n interceptor that should be on your stack.

You can also configure this interceptor to accept user-defined parameters.

Normally switching to a locale persist for the session of the user. So, you don't have to pass a parameter each time on every request, but this case is also supported if needed. See how could you achieve all of the above using localization.




回答2:


As mentioned by @RomanC , it is possible to do that, you need to have the i18n interceptor in your package.

After that you need your jsp page submit a form with request_locale in it, or call an action with request_locale in its parameters . For example, if you want to do it with select you can use below:

<s:form id="langselect" action="locale-manager" namespace="/common">
        <s:select name="request_locale" headerKey="-1"
                headerValue="Language" 
                list="#{'en_US':'English', 'fa_IR':'فارسی','ar_SA':'العربية','zh_CN':'中国的'}"
                onchange="forms['langselect'].submit()" />
</s:form>

You form action needs nothing to do about changing the locale at all, as the interceptor will do all the job for you.

Changing the locale will change all your 20 pages at once, of course as soon as you reload your pages. So if you want to do it with tab you need to reload your new pages (for example with ajax) or reload your total site to get localized jsp from struts.



来源:https://stackoverflow.com/questions/36762212/how-to-apply-i18n-on-whole-website-by-a-single-click-in-struts-2

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