JSF render dir =“rtl”当语言是阿拉伯语时 [英] JSF render dir="rtl" when language is arabic
问题描述
当我的JSF应用程序中的浏览器的Accept-Language是阿拉伯语时,我想在inputText中呈现文本方向rtl(从右到左)。当接受语言是英文时,用户引入的文本将是dir =ltr(从左到右)。如何做?
I want to render the text direction rtl (Right-To-Left) in an inputText when the browser's Accept-Language is arabic in my JSF application. When Accept-Language is english the text introduced by the user would be dir="ltr" (Left-To-Right). How can I do it?
推荐答案
客户端的 Accept-Language
可以通过 UIViewRoot#的getLocale()
。 UIViewRoot
依次为EL,可用作#{view}
。所以,这应该是:
The client's Accept-Language
is indirectly available via UIViewRoot#getLocale()
. The UIViewRoot
is in turn in EL available as #{view}
. So, this should do:
<h:inputText ... dir="#{view.locale.language eq 'ar' ? 'rtl' : 'ltr'}" />
请注意,还支持 dir
所有其他组件和HTML元素,例如< h:form>
甚至< html>
p>
Note that the dir
is also supported on all other components and HTML elements, such as <h:form>
and even <html>
.
<html ... dir="#{view.locale.language eq 'ar' ? 'rtl' : 'ltr'}">
它将应用于其所有子项,除非被不同的设置目录覆盖
。这将节省您对所有子组件/元素重复相同的属性。
It would be applied on all its children unless overridden by a differently set dir
. That would save you from repeating the very same attribute over all child components/elements.
另请注意,JSF将仅接受在<$ c $中明确注册的语言环境 faces-config.xml
中的c>< locale-config> 。所以如果你还没有 ar
,那么上述不行,不管 Accept-Language
标题。
Also note that the JSF will only accept locales which are explicitly registered in <locale-config>
in faces-config.xml
. So if you don't already have ar
in there, then the above wouldn't work irrespective of Accept-Language
header.
<application>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>ar</supported-locale>
...
</locale-config>
</application>
如果需要,将逻辑移动到托管bean,以便您可以如下所示结束:
Move if necessary the logic to a managed bean so that you can end up like below:
<h:inputText ... dir="#{localeManager.dir}" />
另请参见:
- JSF中的本地化,如何记住选择每个会话的区域设置而不是每个请求/视图
- Localization in JSF, how to remember selected locale per session instead of per request/view
See also:
这篇关于JSF render dir =“rtl”当语言是阿拉伯语时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!