问题
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