如何在JSF中以编程方式设置语言环境 [英] How to set the locale programmatically in JSF

查看:127
本文介绍了如何在JSF中以编程方式设置语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试以编程方式设置语言环境,但是找不到一个好的解决方案.用例是我有另一个网站,该网站将数据发布到具有locale参数的网站上,并且基于此语言环境,我必须呈现我的页面.

I'm currently trying to set the locale programatically, but can't find a good solution. The use case is I have another website that post data to my site that has a locale parameter, and base on this locale, I have to render my page.

我已经尝试过在preRenderView,构造函数和PostConstruct上设置语言环境,但似乎已经很晚了.

I've already tried setting the locale on preRenderView, constructor and PostConstruct but it seems it's already to late.

有什么建议吗?谢谢.

推荐答案

我已经通过使用普通的javascript加载页面解决了该问题.

I've resolved the issue by using plain javascript to load the page.

在我的PostConstruct中,我从上下文的请求参数映射中读取参数. //设置语言环境

In my PostConstruct, I read the parameter from context's request parameter map. // set locale

try {
    languageBean.setCurrentLocale(LocaleUtils.toLocale(temp.getLocale()));
} catch (IllegalArgumentException | NullPointerException e) {
    languageBean.setCurrentLocale(Locale.JAPAN);
}

然后使用普通的javascript重新加载页面:

And then reload the page using plain javascript:

<script type="text/javascript">
    window.onload = function() {
        if (!window.location.hash) {
            window.location = window.location + '#loaded';
            window.location.reload();
        }
    }
</script>

我在这里写了更详细的代码: http://czetsuya-tech.blogspot.com /2013/01/loading-locale-from-postget-parameter.html

I've wrote a more detailed codes here: http://czetsuya-tech.blogspot.com/2013/01/loading-locale-from-postget-parameter.html

这篇关于如何在JSF中以编程方式设置语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆