JSF 语言环境问题
我遇到一种情况,当用户从下拉列表中选择一种语言时,我希望应用程序区域设置相应更改。捕获语言环境并不困难,但是如何为之后的所有页面设置语言环境。
我在资源包和 faces-config.xml 中设置了配置
I have a situation where when a user selects a language from a drop down, I want the the application locale to change accordingly. Capturing locale is not difficult, but how to set the locale for all pages there after.
I have configuration set in resource bundles and faces-config.xml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注册您自己的 ViewHandler 并覆盖其 calculateLocale() 方法是一种方法。
自定义
ViewHandler
需要在faces-config.xml
文件中指出,例如:并且实现将仅覆盖
calculateLocale()
并委托其他代理ViewHandler
的方法:Registering your own
ViewHandler
and overriding its calculateLocale() method would be one way to go.Custom
ViewHandler
needs to be indicated infaces-config.xml
file, e.g.:And the implementation would override the
calculateLocale()
only and delegate other methods to proxiedViewHandler
:我相信这就是您正在寻找的:
I believe this is what you are looking for:
我刚刚经历过这个,编码并不难,但需要花一些时间来弄清楚如何以面向未来和自动化的方式有效地进行工作,但我认为我有一个合理的方法,可以自动检测 JSF 中配置的语言,并自动将菜单项本地化为当前区域设置。
首先,确保您使用某种 Facelets 模板,否则您将必须在您使用的每个页面上复制第一部分。您使用 f:view locale= 参数设置区域设置
您的页面应如下所示:
这是我的 LocaleBean:(使用 @ManagedBean 注册它,或通过具有会话范围的 faces-config.xml 注册)
和 LocaleConverter (同样,通过注释或 faces-config.xml 注册)
I just went through this, and it's not difficult to code, but takes a bit to figure out how to do effectively in a future-proof and automated fashion, but I think I have a reasonable approach that automatically detects what languages are configured in JSF, and that localizes the menu items to the current Locale automatically.
First, make sure you're using a facelets template of some sort, or you're going to have to replicate this first part on every page you use. You set the Locale using the f:view locale= parameter
Your page(s) should look something like this:
Here's my LocaleBean: (Register it with either @ManagedBean, or via faces-config.xml with a session scope)
And the LocaleConverter (again, register via annotations or faces-config.xml)