JSF 2.0 中的 UI 分为两部分:一部分位于用户区域设置中,另一部分位于所选区域设置中。例如,如果用户是一个法国人,决定选择英语,则页面的某些组件是法语,其他一些组件是英语。
有可能实现这一目标吗?
简单的 f:loadBundle
不接受任何语言环境,并使用其父 f:view
中定义的语言环境,因此看来我们只能为每个视图定义一种语言环境。
也许我们可以使用多个f:view
?我们尝试了一下,服务器并没有抱怨太多,但是 JavaDoc 表示 f:view 是“在 a 上使用的所有 JavaServer Faces 核心和自定义组件操作的容器page”,所以我认为这样做并不安全。
We have a UI in JSF 2.0 that is divided in two parts: one is in the user locale, and the other in a chosen locale. By example, if the user is a french guy who decides to choose english, some components of the page are in french, some others are in english.
Is it possible to achieve this?
The simple f:loadBundle
doesn't accept any locale and use the one defined in its parent f:view
, so it seems we can only define one locale per view.
Maybe we can use multiple f:view
? We tried a bit and the server didn't complain that much, but the JavaDoc says that f:view is "Container for all JavaServer Faces core and custom component actions used on a page", so I don't guess it should be safe to do it that way.
发布评论
评论(2)
为什么不直接将英文值放入默认资源包(不带 a 后缀的资源包)中?这样,如果当前区域设置没有匹配的字符串,它将显示出来。
Why not just put the English values in the default resource bundle (the one without the a suffix)? That way, it'll get displayed if there is no matching string for the current locale.
通过
方式这是不可能的。您确实可以在整个视图中只有一种区域设置,并且不允许嵌套
标记(从技术上讲,它可以工作,但嵌套标记将被忽略)。最好的选择是创建一个托管 bean,它扩展
ResourceBundle
,将包包装在所需的语言环境中并将调用委托给它。像这样:您可以按照通常的方式使用它:
That's not possible through the
<f:loadBundle>
means. You can indeed have only one locale throughout the entire view and it's not allowed to nest<f:view>
tags (technically it will work, but the nested tag will just be ignored).Your best bet is to create a managed bean which extends
ResourceBundle
, wraps the bundle in the desired locale and delegates the calls to it. Something like this:You can use it the usual way: