Pregunta

Currently my project has three resource bundles in my faces-config.xml as follows:

<resource-bundle>
  <base-name>com.example.text.common</base-name>
<var>common</var>
</resource-bundle>
<resource-bundle>
  <base-name>com.example.text.option1</base-name>
  <var>option1</var>
</resource-bundle>
<resource-bundle>
  <base-name>com.example.text.option2</base-name>
  <var>option2</var>
</resource-bundle>

I need to use two resource bundles, the common bundle will always be used and is hard coded in the view pages of my site. However, I wish to only use one of the two option bundles, either option1 or option2. Is there a way to specify which of the two option bundles to use based on a condition. The desired outcome is that is this case:

<h:outputText value="#{oneOfTheOptions['contact.explanation']}"/>

the oneOfTheOptions would be dynamically set to one of the appropriate bundle name. Is there a mechanism in JSF that can accomplish this ? If not, is any other way to dynamically set the Resource Bundle for view components ?

Thanks,

¿Fue útil?

Solución

They are stored as request attributes (like request scoped managed beans). So, you should be able to obtain them via the request scope map #{requestScope} using a dynamic key in brace notation #{map[key]}.

Imagine that the EL variable #{bundleKey} can hold one of the string values "common", "option1" or "option2", then this should do:

#{requestScope[bundleKey]['contact.explanation']}

It can even be a bean property:

#{requestScope[bean.bundleKey]['contact.explanation']}

Note that you don't need a whole <h:outputText> around it, provided that you're using JSF2/Facelets. See also Is it suggested to use h:outputText for everything?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top