JSF render dir=“rtl” when language is arabic

心已入冬 提交于 2019-12-10 11:26:02

问题


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?


回答1:


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:

  • Localization in JSF, how to remember selected locale per session instead of per request/view


来源:https://stackoverflow.com/questions/30629814/jsf-render-dir-rtl-when-language-is-arabic

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