struts2 本地化通过在操作名称中嵌入区域设置代码而不是使用 ...?request_locale=;

发布于 2024-10-12 14:09:47 字数 919 浏览 3 评论 0 原文

大家好,

我想在用 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥琐帝 2024-10-19 14:09:47

我没有对本地变量做太多事情,但 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,然后使用通配符可以执行以下操作:

<action name="*/*">
  <result>/WEB-INF/content/{1}/{2}.jsp</result>
<action>

您还可以将参数传递给操作名称中找到的每个星号的操作变成 {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:

<action name="*/*">
  <result>/WEB-INF/content/{1}/{2}.jsp</result>
<action>

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.

神妖 2024-10-19 14:09:47

@Quaternion

基本上的意图是,该网站有几个国家“子网站”。根据用户的IP地址,他/她将被重定向到国家“子网站”。就像当您打开 www.google.com 时,您可能会被重定向到 www.google.com.country_domain。

每个国家“子网站”都有多种语言,其中有 1 种默认语言。就像当您打开谷歌以色列网站时,默认情况下您会看到一个用希伯来语编写的网站,尽管您可以通过选择英语来覆盖此默认选择。

在我计划的网站中,按照 isreal 网站和希伯来语示例,应该是这样的:

  1. 用户在以色列,
  2. 他正在打开 www.abcdef.com
  3. 服务器正在识别客户端在以色列。以色列“子网站”有3种语言可供选择:希伯来语、阿拉伯语、英语。默认是希伯来语,但客户端可以覆盖此选择,
  4. 然后用户将被重定向到 www.abcdef.com/il/he(“il”代表以色列国家,“he”代表希伯来语)
  5. ,但用户显然是英国游客不懂希伯来语或阿拉伯语。因此,他/她选择了英语,
  6. 下次客户在以色列再次打开 www.abcdef.com 时,他/她将被重定向到 www.abcdef.com/il/en(“en”代表英语)
  7. (假设 cookie & 会议仍在进行),他/她将被重定向到 www.abcdef.com/il/en

谢谢您的帮助 =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:

  1. the user is in israel
  2. he is opening www.abcdef.com
  3. the server is recognizing that the client is in israel. there are 3 languages can be chosen for the israel "sub-website": hebrew, arabian, english. the default one is hebrew, but client can override this choice
  4. the user is then redirected to www.abcdef.com/il/he ("il" stands for israel country and "he" stands for hebrew language)
  5. but the user is apparently a british tourist with no knowledge on hebrew or arabian language. so he/she chose english language
  6. he/she will be redirected to www.abcdef.com/il/en ("en" stands for english language)
  7. the next time that client opens www.abcdef.com in israel again (assuming the cookies & sessions are still around), he/she will be redirected to www.abcdef.com/il/en

thx fr your help =D

巴黎盛开的樱花 2024-10-19 14:09:47

当然,我会将处理区域设置的责任留给拦截器

这是创建拦截器的教程。

该拦截器可以放置在由所有(或大多数)传入请求共享的公共堆栈中,并且它将

ActionContext.getContext().setLocale(locale);

使用适当的逻辑分配区域设置,该逻辑可以考虑:

  • 查询字符串参数
  • 存储的用户首选项
  • cookie
  • 会话
  • 浏览器首选项(位于要求)

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

ActionContext.getContext().setLocale(locale);

with the proper logic that could take into account:

  • query-string parameters
  • stored user preferences
  • cookies
  • session
  • browser preferences (are in the request)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文