简体   繁体   English

资源包

[英]Resource Bundle

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 . 我没有得到那些系统sysout In the classpath there are these files: 在类路径中有以下文件:
Messages.bundle Messages.bundle

<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<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 Messages_es.properties

cancel=Cancelar
submit=Enviar
Search=Buscar

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 . 在此处完成教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM