gpt4 book ai didi

java - 资源包

转载 作者:行者123 更新时间:2023-12-01 05:53:12 25 4
gpt4 key购买 nike

我刚刚开始使用此代码:

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());
}

}

我没有收到这些sysout。在类路径中有这些文件:
消息.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>

对于每种语言:

Messages_es.properties

cancel=Cancelar
submit=Enviar
Search=Buscar

最佳答案

我只需枚举所有键,这样我就可以读取一个。这段代码:

Enumeration bundleKeys = labels.getKeys();

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

我得到这个输出:

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

Complete tutorial here

关于java - 资源包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3931050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com