例如,我有一个 Language
类,它将包含 String
类型的常量属性,该属性将在整个程序中用于打印 x
或 y
Strings
基于启动程序时的设置。
我怎样才能做到这一点?我曾经使用过Enum
,但是我缺乏使用它们的经验并且未能正确应用它们;尝试了继承Language
并且必须指定每个String
的值的不同类,但这看起来像一个自制的Enum
。
作为一个优点,我希望有一个包含每个 String
内容的 XML 文件,但我完全不知道如何在 java 中获取 XML 文件的值(我可能知道如何做他们分开)。如果可能的话,提供一些示例或文档。
谢谢。
最佳答案
您可以使用属性文件,即将它们命名为 en-GB.properties、en-US.properties 等,文件如下所示:
en-GB.properties
file=File
edit=Edit
it-IT.properties
file=File
edit=Modifica
“=”符号之前的字符串是属性名称,其后面的字符串是属性值,这是访问它的代码:
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream("it-IT.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("file")); //prints "File"
System.out.println(prop.getProperty("edit"));//prints "Modifica"
} catch (IOException ex) {
ex.printStackTrace();
}
希望这有帮助
关于java - 如何根据设置本地化程序文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15205415/