Jac*_*ack 5 java enums internationalization
我正在考虑使用enum类型来管理我正在开发的Java游戏中的i18n,但我对使用具有大量元素的枚举(我认为数千个)时可能出现的性能问题感到好奇.
其实我正在尝试这样的事情:
public enum Text {
STRING1,
STRING2,
STRING3;
public String text() {
return text;
}
public String setText() {
this.text = text;
}
}
Run Code Online (Sandbox Code Playgroud)
然后加载它们我可以填写字段:
static
{
Text.STRING1.setText("My localized string1");
Text.STRING2.setText("My localized string2");
Text.STRING3.setText("My localized string3");
}
Run Code Online (Sandbox Code Playgroud)
当然,当我必须管理多种语言时,我会从文件中加载它们.
我问的是
Text.STRING1.text()).所以它应该是恒定的复杂性,或者它们可能只是在编译阶段被替换.谢谢
And*_*s_D 14
找到并调整了很好的enum和ResourceBundle组合:
public enum Text {
YELL, SWEAR, BEG, GREET /* and more */ ;
/** Resources for the default locale */
private static final ResourceBundle res =
ResourceBundle.getBundle("com.example.Messages");
/** @return the locale-dependent message */
public String toString() {
return res.getString(name() + ".string");
}
}
Run Code Online (Sandbox Code Playgroud)
# File com/example/Messages.properties
# default language (english) resources
YELL.string=HEY!
SWEAR.string=§$%&
BEG.string=Pleeeeeease!
GREET.string=Hello player!
Run Code Online (Sandbox Code Playgroud)
# File com/example/Messages_de.properties
# german language resources
YELL.string=HEY!
SWEAR.string=%&$§
BEG.string=Biiiiiitte!
GREET.string=Hallo Spieler!
Run Code Online (Sandbox Code Playgroud)
Cam*_*ner 11
你可能最好使用这java.util.ResourceBundle门课.它旨在解决这个问题.
回答你的问题: