大家好,
我想在用 struts 2 编写的网站中添加本地化功能。据我所知,这样做的标准方法是按以下方式使用 get :
http://.../namespace/action_name?request_locale=<区域设置代码>
然而,我的老板不喜欢这种毛茸茸的网址。相反,我需要按以下方式编写:
http://.../namespace/a_param/ code>/another_param。 ..
我尝试将 struts.xml 中的操作映射更改为类似的内容
<动作名称=“*/*...”...>
{2}
...
就不起作用了
我把它改成之后
<动作名称=“*/*...”...>
{2}
...
顺便说一句,它也不起作用
,我知道有一个技巧,可以放置 ActionContext.getContext().setLocale(new Locale(...));在行动中,这基本上改变了该实例的区域设置。然而,似乎效果只是暂时的(相比之下,i18n 将所选区域设置保存在会话中,这基本上使其相当持久。
那么,如何通过在 url 中嵌入区域设置代码来更改区域设置?
您的帮助非常大赞赏=D
hi all,
i want to make localization feature in a website written in struts 2. as far as i know, the standard way of doing so is using get in the following manner:
http://.../namespace/action_name?request_locale=<locale code>
however, my boss doesn't like such hairy url. instead, i'm required to write it in the following manner:
http://.../namespace/a_param/<locale code>/another_param...
i tried to change the action mapping in my struts.xml into something like
<action name="*/*..." ... >
<param name="locale">{2}</param>
...
</action>
it doesn't work
after i changed it into
<action name="*/*..." ...>
<param name="request_locale">{2}</param>
...
</action>
it doesn't work either T_T
by the way, i know there is trick of putting ActionContext.getContext().setLocale(new Locale(...)); in action which basically change the locale for that instance. however, it seems that the effect will only be transient (in contrast, i18n saves the chosen locale in session, which basically makes it quite persistent.
so, how to change the locale by embedding the locale code in the url?
Your help is highly appreciated =D
发布评论
评论(3)
我没有对本地变量做太多事情,但 i18n 应该通过标头自动从浏览器确定正确的本地变量,不需要在 url 中包含任何内容。只要有特定语言环境的语言包,它就会尝试从该文件中提取属性。
这个页面展示了一个使用基本 i18n 的例子(只看了一会儿,我个人总是从 http 开始://struts.apache.org/2.x/ 但是教程/指南有点枯燥,
如果用户确实想要的话,为什么您需要引用 url 中的任何内容?覆盖默认区域设置我将提供某种形式的控制(菜单)来执行此操作然后我将在会话中设置一个变量,然后我将创建一个拦截器,该拦截器将使用会话上的本地参数调用 setLocale 操作(如果有)。当然是一个值集)。这样就不需要将参数嵌入到各个页面中,并且本地的参数都在 url 之外。
有一种方法可以使用 url 执行您想要的操作...如果我记得的话,我会在动作名称中使用约定和斜杠,但我认为上面的方法通常是更好的方法
:考虑到你的身份 。尝试完成我可以看到两种截然不同的解决方案。
1) 可以使用代理,传入的 URL www.example.com/en/ 和 www.example.com/fr/ 可以映射到不同的 Web 应用程序甚至同一个 Web 应用程序,但 url 被重新写入适合您的应用程序的表格。可以做到这一点的工具包括:iptables、apache mod_rewrite、squid...以及许多其他工具。如果您在一台服务器上处理多个 IP 地址/URL/应用程序,这种类型的解决方案更有价值。
2)您可以设置struts2属性struts.enable.SlashesInActionNames,然后使用通配符可以执行以下操作:
您还可以将参数传递给操作名称中找到的每个星号的操作变成 {1}、{2} 等。听起来您可能需要此功能。如果其他人知道我现在不知道如何使用 struts2-conventions-plugin 捕获这样的 url 部分,以便操作可以利用它们,我会觉得很有趣。
I have not done much with locals but i18n should automatically determine the correct local from the browser via the headers, there is no need for anything to be in the url. As long as there is a language bundle for the particular locale it will try to pull properties from that file.
This page shows an example of using basic i18n (only looked at it for a moment, personally I always start at http://struts.apache.org/2.x/ but the tutorial/guides are a bit dry.
Why do you need to refer to anything in the url at all concerning language? Personally if the user did want to override the default locale I would provide some form of control (menu) to do so. Then I would set a variable in session then I would create an interceptor which would call setLocale on the action using the local parameter on the session (if there is a value set of course). This way there would not be any need to embed parameters into individual pages and the local is out of the url all together.
There is a way to do what you want with the url... Something to do with conventions and slashes in allowing slashes in the action name I think. I'll post back if I remember. But I think the above is generally a better approach anyways.
Edit: Taking into consideration what you are trying to accomplish I can see two very different solutions.
1) You can use a proxy, the incoming URL www.example.com/en/ and www.example.com/fr/ can be mapped to different web applications or even the same web application but the url is re-written into a form that suites your application. Tools that can do this include: iptables, apache mod_rewrite, squid... and a multitude of others. This type of solution is more valuable if you handle multiple ip addressses/urls/applications on one server.
2) You can set the struts2 property struts.enable.SlashesInActionNames then using wildcards you can do something like:
You can also pass parameters to actions each asterisk found in the action name becomes {1}, {2}, etc. It sounds like you might need this feature. If someone else knows it escapes me at the moment how you would capture parts of the url like this with struts2-conventions-plugin so the action can make use of them I would find that interesting.
@Quaternion
基本上的意图是,该网站有几个国家“子网站”。根据用户的IP地址,他/她将被重定向到国家“子网站”。就像当您打开 www.google.com 时,您可能会被重定向到 www.google.com.country_domain。
每个国家“子网站”都有多种语言,其中有 1 种默认语言。就像当您打开谷歌以色列网站时,默认情况下您会看到一个用希伯来语编写的网站,尽管您可以通过选择英语来覆盖此默认选择。
在我计划的网站中,按照 isreal 网站和希伯来语示例,应该是这样的:
谢谢您的帮助 =D
@Quaternion
basically the intention is that, the website has several national "sub-website". based on user's ip address, he/she will be redirected to the national "sub-website". it's like when you open www.google.com, you may be redirected to www.google.com.country_domain.
each national "sub-website" has several languages, with 1 default language. it's just like when you open google israel website, by default you will see a website written in hebrew language, although you can override this default choice by choosing it to be in english.
in my planned website, following isreal website and hebrew language example, it is supposed to be like this:
thx fr your help =D
当然,我会将处理区域设置的责任留给拦截器。
这是创建拦截器的教程。
该拦截器可以放置在由所有(或大多数)传入请求共享的公共堆栈中,并且它将
使用适当的逻辑分配区域设置,该逻辑可以考虑:
Definitely I would leave the responsibility to handle the Locale to an interceptor.
Here is a tutorial to Create an Interceptor.
This interceptor can be placed in a common stack shared by all (or most of) incoming requests and it will assign the locale
with the proper logic that could take into account: