JSF render dir=“rtl” when language is arabic

旧街凉风 提交于 2019-12-06 09:41:52
BalusC

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'}" />

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.

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>

Move if necessary the logic to a managed bean so that you can end up like below:

<h:inputText ... dir="#{localeManager.dir}" />

See also:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!