views:

55

answers:

1
+1  Q: 

Resource Bundle

Hello, I am just starting with this, and with this code:

public static void main(String[] args) {
 Locale[] supportedLocales = {
      new Locale("en", "CA"),
      new Locale("es", "ES")

  };

 ResourceBundle labels = ResourceBundle.getBundle("Messages", supportedLocales[0]);
 System.out.println(supportedLocales[0].getDisplayVariant());
 System.out.println(supportedLocales[0].getVariant().toString());
}

}

I am not getting neither of those sysout. In the classpath there are these files:
Messages.bundle

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%&gt;
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%&gt;
<f:loadBundle basename="Messages" var="msg"/>
<f:view> <html>
    <body>
        <f:view>
            <h:form>
                <h:commandButton value="#{msg.cancel}" action="fail"/>
                <h:commandButton value="#{msg.submit}" action="success"/>
                <h:outputText value="#{myBundle[myBean.msgKey]}"/>
            </h:form>  
        </f:view>
    </body>
</html> </f:view>

And for each language:

Messages_es.properties

cancel=Cancelar
submit=Enviar
Search=Buscar
A: 

I just had to enumerate all the keys, in this way I was reading one. Whit this code:

Enumeration bundleKeys = labels.getKeys();

    while (bundleKeys.hasMoreElements()) {
        String key = (String)bundleKeys.nextElement();
        String value = labels.getString(key);
        System.out.println("key = " + key + ", " + 
                   "value = " + value);
    }  

I get this output:

key = Search, value = Buscar
key = submit, value = Enviar
key = cancel, value = Cancelar 

Complete tutorial here .

mujer esponja