Localization technique
I am currently developing a desktop game as part of my CS course .
Although it is not required I want my application to support Localization So I browsed the web and found two techniques - ResourceBundle - properties file However I can not decide which is better suited for me , I simply need localized messages and labels for the GUI. Also I am confused on how such files/classes should be named for example if FooBar extends ResourceBundle should be like FooBar_UNIX_en_US Btw , the assignment is an Entanglement (by gopherwoods studio) clo开发者_如何学JAVAne (Desktop not applet)~Thanks
You use both - from your program's point of view, they are ResourceBundles, from the translators point of view they are property files. A ResourceBundle can load its strings (only strings, though) from a property file.
So you simply put files like Messages.properties
(the default), Messages_eo.properties
(the esperanto translation), Messages_de.properties
(german translation), Messages_de_AT.properties
(special strings for austrian german, overriding the general ones).
In your program you then simply do ResourceBundle bundle = ResourceBundle.getBundle("Messages")
, and the runtime builds your resource bundle for the current locale from the property files.
If you need to translate other things than strings, you would need .class
files instead, subclassing ResourceBundle. You can even mix both, but I would then better split them to different bundles (i.e. one text
bundle and one for the dynamic resources).
You are in the right direction, the convention is typically Name_<language>_<country>
.
ResourceBundle
itself can use properties file to back the localization string, so you can use that. That way you don't have to compile new classes for each localization you would like to support. Just create a new properties file for the language.
Check out ResourceBundle.getBundle() factory method.
I believe using resource bundles would be the best technique.
Java itself, will attempt to reference your resource bundels by most specific to least specific (most generic).
If you have the following resources:
resource
resource_en
resource_en_GB
resource_fr
resource_fr_CA
And the end users local is en_NZ (New Zeland) he would be shown what is in the resource_en.
However, someone from Quebec, Canada would most likely be shown what is in the resource_fr_CA.
Hope that helps.
精彩评论