2

The resource-bundle is configured as-

    <resource-bundle>                        
        <base-name>views.msgs.labels</base-name>
        <var>msg</var>
    </resource-bundle>

in faces-config.xml.

with 2 properties files in resources/views/msgs/ -

labels_en.properties
labels_fr.properties

There are 2 sections:

In the above section, the text/ labels need to be shown in English, whereas the lower half, the text has to appear in French.

To put it simple,

  <f:view locale="en" >            
            #{msg['hello']}
            #{msg['name']}
  </f:view>
  <f:view locale="fr" >            
            #{msg['HELLO']}
            #{msg['name']}
  </f:view>

The two keys hello & name appears in both property files.

I see that locale="fr" in the lower half f:view overrides the above one.

And the whole output appears in French.

How can I fix this?

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105

1 Answers1

4

This construct is not supported. The <f:view> represents the UIViewRoot. Whilst there can be multiple <f:view> tags, there can be only one UIViewRoot instance in the component tree. The attributes of the latter <f:view> declaration would override any previously declared one.

Your best bet is manually loading the resource bundle via ResourceBundle API the usual way and assigning it a different variable name in EL.

Easiest would be to use a managed bean for that. E.g.

@ManagedBean
@RequestScoped
public class Labels {

    private ResourceBundle french;

    @PostConstruct
    public void init() {
        french = ResourceBundle.getBundle("views.msgs.labels", Locale.FRENCH);
    }

    public ResourceBundle getFrench() {
        return french;
    }

}

(hint: you can get the <default-locale> and all <supported-locale>s via Application)

Then use it as below:

Current locale:
#{msg['hello']}
#{msg['name']}

French locale:
#{labels.french['HELLO']}
#{labels.french['name']}

See also:

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