JSF中的语言切换器实现 [英] Language switcher implementation in JSF

查看:44
本文介绍了JSF中的语言切换器实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的jsf页面,其中包含在PrimeFaces中开发的一些小部件.到现在为止,该应用程序已被完全Ajax化,这意味着还没有提交,但是所有事件和更新都是通过Ajax行为来处理的(这不是必须的,而是一个很好的功能).我还做了SelectOneMenu切换语言:

I have a complex jsf page with some widgets developed in PrimeFaces. Up to now, the application is completely ajaxified, meaning that there aren't submits, but all the events and updates are handled through Ajax behavior (this is not a must, but a nice-to-have feature). I have also done a SelectOneMenu for switching the language:

<h:form>
    <p:panelGrid columns="2" >
        <h:outputText value="#{msgs.SelectLanguage}" />
        <p:selectOneMenu value="#{languageSwitcher.selectedLanguage}" >
            <f:selectItems value="#{languageSwitcher.languages}" ></f:selectItems>
        </p:selectOneMenu> 
    </p:panelGrid>
</h:form>

切换台工作正常. 我的问题是选择一种语言后如何重新加载翻译后的消息.

The switcher works well. My problem is how to reload the translated messages when a language is selected.

我尝试了以下选项:

选项1

在selectOneMenu中进行ajax更新:

With an ajax update inside the selectOneMenu:

<p:ajax update="myFormsTobeUpdated" ></p:ajax>

这很好用,并且是首选解决方案,因为它是Ajax,但是

This works great, and is the preferred solution because it's Ajax, but

已编辑

此解决方案不会ajax更新PrimeFaces TabView中的Tab标题,并且此问题被阻止,因为需要翻译Tab标题.

this solution doesn't ajax-update the Tab titles in the PrimeFaces TabView, and this is problem is blocking because the Tab titles need to be translated.

选项2

使用Javascript完全重新加载页面.我已经尝试过了,但是不起作用(无法说出原因):

With a complete reload of the page in Javascript. I have tried this but doesn't work (can't tell why):

<p:selectOneMenu value="#{languageSwitcher.selectedLanguage}" onchange="window.location.reload()" >

选项3

使用Java完全重载页面,如此处所述: https://stackoverflow.com/a/1821708/870122 (说实话,我还没有尝试过,但是很快就会成功!)

With a complete reload of the page in Java like explained here: https://stackoverflow.com/a/1821708/870122 (to be honest I haven't tried it yet, but will do soon!)

欢迎提出任何建议.

推荐答案

由于使用选项1 ,因此无法ajax更新PrimeFaces TabView中的Tab标题,我已切换为选项2 ,使用JavaScript完全重新加载页面.

Since with Option 1 it's not possible to ajax-update the Tab titles in the PrimeFaces TabView, I have switched to Option 2, a complete reload of the page using JavaScript.

使其生效的代码是:

<h:form id="selectLanguage">
    <p:panelGrid columns="2">
        <h:outputText value="#{msgs.SelectLanguage}" />
        <p:selectOneMenu value="#{languageSwitcher.selectedLanguage}" onchange="document.getElementById('selectLanguage').submit();" >
            <f:selectItems value="#{languageSwitcher.languages}" ></f:selectItems>
        </p:selectOneMenu> 
    </p:panelGrid>
</h:form>

这篇关于JSF中的语言切换器实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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