Search code examples
javajsfrichfacesejb-3.0seam2

Resource bundle at server-side for internationale


I faced a problem regarding loading ResourceBundle at server-side on Seam 2.2 application.

In client side (JSF), it's OK to use the resource bundle:

<f:facet name="header">#{message['addTest.header.add']}</f:facet>

but on server-side, there's no way to use Resource bundle.

I tried following methodology but no solution works.

  • add to StatusMessage: StatusMessage.addFromResourceBundle(serverity.ERROR, "key_string");
  • Use injection as in this question: Use message bundle in Java class with Seam
  • Load resource bundle:

    private java.util.ResourceBundle getResourceBundle() { org.jboss.seam.core.ResourceLoader resourceLoader = org.jboss.seam.core.ResourceLoader .instance(); java.util.ResourceBundle resourceBundle = resourceLoader.loadBundle("message"); return resourceBundle; }

but result of this method is null.

Any idea or guidance will be highly appreciated.


Solution

  • Found the problem, the 3rd way works.

    It's because I use 2 resource bundles in my project, one is messages (common) and one is myMessage.

    At first, when defining resource bundle in file components.xml, I only define myMessage resource bundle.

    <core:resource-loader bundle-names="my">
            <core:bundle-names>
                <value>myMessage</value>
            </core:bundle-names>
    </core:resource-loader>
    

    In file faces-config.xml, I defined language support.

    <application>
            <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
            <locale-config>
                <default-locale>en</default-locale>
                <supported-locale>bg</supported-locale>
                <supported-locale>de</supported-locale>
                <supported-locale>en</supported-locale>
                <supported-locale>fr</supported-locale>
                <supported-locale>tr</supported-locale>
                <supported-locale>ja</supported-locale>
            </locale-config>
    
            <message-bundle>messages</message-bundle>
    
        </application>
    

    And when using 3rd way, it only return messages bundle only.

    To fix this, only need to add messages bundle into file components.xml and remove name in core:resource-loader.

    <core:resource-loader>
            <core:bundle-names>
                <value>messages</value>
                <value>myMessage</value>
            </core:bundle-names>
    </core:resource-loader>
    

    That's it.