2

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
jose
  • 303
  • 4
  • 17

1 Answers1

6

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555