当语言是阿拉伯语时,JSF渲染dir ="rtl"

jos*_*ose 2 jsf localization dir conditional-rendering

当浏览器的Accept-Language在我的JSF应用程序中是阿拉伯语时,我想在inputText中呈现文本方向rtl(从右到左).当Accept-Language是英语时,用户引入的文本将是dir ="ltr"(从左到右).我该怎么做?

Bal*_*usC 6

客户端Accept-Language可通过间接获得UIViewRoot#getLocale().在UIViewRoot又在EL可作为#{view}.所以,这应该做:

<h:inputText ... dir="#{view.locale.language eq 'ar' ? 'rtl' : 'ltr'}" />
Run Code Online (Sandbox Code Playgroud)

请注意,dir所有其他组件和HTML元素也支持,例如<h:form>甚至<html>.

<html ... dir="#{view.locale.language eq 'ar' ? 'rtl' : 'ltr'}">
Run Code Online (Sandbox Code Playgroud)

它将适用于所有孩子,除非被不同的集合覆盖dir.这样可以避免在所有子组件/元素上重复相同的属性.

还要注意的是,JSF将只接受被明确登记在语言环境<locale-config>faces-config.xml.因此,如果您还没有ar,那么无论Accept-Language标题如何,上述内容都无效.

<application>
    <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>ar</supported-locale>
        ...
    </locale-config>
</application>
Run Code Online (Sandbox Code Playgroud)

必要时将逻辑移动到托管bean,以便最终如下所示:

<h:inputText ... dir="#{localeManager.dir}" />
Run Code Online (Sandbox Code Playgroud)

也可以看看: