在 JSF 中为完整的 HTML 页面进行国际化的正确方法? [英] correct way to do internationalization in JSF for full HTML pages?

查看:35
本文介绍了在 JSF 中为完整的 HTML 页面进行国际化的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JSF2 应用程序,到目前为止,我正在以一种典型的方式使用消息资源包进行国际化;示例:

I have a JSF2 application and so far I'm doing the internationalization in a typical way with a message resource bundle; example:

<f:view contentType="text/html" locale="#{loginHandler.currentLocale}">

<f:loadBundle basename="MessageResource" var="msg" />

<h:outputText value="#{msg.user_firstNameLabel}" />

我的问题是我有一个信息页面,其中包含两种语言的内容段落.我不希望将页面上的每一行文本都作为消息属性文件中的键|值.

My problem is I have an information page with paragraphs of content in two languages. I prefer not to take every line of text on the page and make them key|values in the message properties file.

如果内容在两个html文件中;比如contactus_en.html和contactus_fr.html,根据当前语言环境显示正确语言的正确方法是什么?

If the content is in two html files; say contactus_en.html and contactus_fr.html, what would be the right way to display the right language based on the current locale?

谢谢!抢

推荐答案

最简单的方法是创建一个控制器来读取 HTML 文件的内容并按需返回它们.在这种情况下,您可以通过非常简单的声明从您的 JSF 页面中写出:

The easiest way to do what you are asking for is to create a controller that would read the contents of your HTML files and return them on demand. In such case you can write out it from your JSF page with very simple declaration:

<h:outputText escape="false" value="#{yourController.contactus}" />

由于您要读取 HTML 文件的内容,您需要告诉 JSF 不要转义它们(因此 escape="false").
当然,您的控制器需要提供名为 getContactus() 的方法,该方法应该读取您感兴趣的 HTML 文件的内容并将它们作为字符串返回.我相信你可以轻松处理它:)

Since you are going to read the contents of HTML file(s), you need to tell JSF not to escape them (thus escape="false").
Of course your controller need to provide method named getContactus() which should read the contents of HTML files of your interest and return them as String. I believe you can easily handle it :)

编辑 - 添加有关如何选择文件的信息.

如果您的 HTML 文件依赖于区域设置,因此它们已经是双语但英语和法语不同,您可以轻松地从 UIViewRoot 获取当前视图区域设置:

If your HTML files are locale-dependent so these are already bi-lingual but different for English and French, you can easily get current view Locale from UIViewRoot:

Locale currentLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
String fileName = "contactus_" + currentLocale.toString() + ".html";

这篇关于在 JSF 中为完整的 HTML 页面进行国际化的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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