一个视图中的多个区域设置/捆绑包

发布于 2024-12-29 02:35:40 字数 525 浏览 3 评论 0 原文

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.

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

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

发布评论

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

评论(2

小猫一只 2025-01-05 02:35:41

为什么不直接将英文值放入默认资源包(不带 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.

太阳公公是暖光 2025-01-05 02:35:40

通过 方式这是不可能的。您确实可以在整个视图中只有一种区域设置,并且不允许嵌套 标记(从技术上讲,它可以工作,但嵌套标记将被忽略)。

最好的选择是创建一个托管 bean,它扩展 ResourceBundle,将包包装在所需的语言环境中并将调用委托给它。像这样:

@ManagedBean
@RequestScoped
public class BundleBean extends ResourceBundle {

    private ResourceBundle wrapped;

    public BundleBean() {
        // You can get/supply the locale as a parameter or property.
        // Again, just a basic kickoff example.
        wrapped = ResourceBundle.getBundle("com.example.i18n.text", new Locale("fr"));
    }

    @Override
    protected Object handleGetObject(String key) {
        return wrapped.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return wrapped.getKeys();
    }

}

您可以按照通常的方式使用它:

#{bundleBean.someKey}
#{bundleBean['some.key.with.periods']}

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:

@ManagedBean
@RequestScoped
public class BundleBean extends ResourceBundle {

    private ResourceBundle wrapped;

    public BundleBean() {
        // You can get/supply the locale as a parameter or property.
        // Again, just a basic kickoff example.
        wrapped = ResourceBundle.getBundle("com.example.i18n.text", new Locale("fr"));
    }

    @Override
    protected Object handleGetObject(String key) {
        return wrapped.getObject(key);
    }

    @Override
    public Enumeration<String> getKeys() {
        return wrapped.getKeys();
    }

}

You can use it the usual way:

#{bundleBean.someKey}
#{bundleBean['some.key.with.periods']}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文