We need to start adding internationalisation to our program. Thankfully not the whole thing yet, just a few bits, but I want the way we do it to scale up to potentially cover the whole program. The thing is, our program is based on plugins, so not all strings belong in the same place.
As far as I understand it, Java's ResourceBundle
work like this. You create a class that extends ResourceBundle
, called something like MyProgramStrings
, and also language-specific classes called MyProgramStrings_fr
, MyProgramStrings_es
etc. Each of these classes maps keys (strings) to values (any object). It's up to each of these classes where to get its data from, but a common place for them is a properties file.
You look up values in two stages: first you get the correct bundle, then you query it for the string you want.
Locale locale = Locale.getDefault(); // or = new Locale("en", "GB");
ResourceBundle rb = ResourceBundle.getBundle("MyProgramStrings", locale);
String wotsitName = rb.getString("wotsit.name");
However, what we need is to combine the results of several locales into a single resource space. For example, a plugin needs to be able to override a string that's already defined, and have that new value returned whenever code looks up the string.
I'm a little lost in all this. Can anybody help?
Update: David Waters asked:
I have put my answer at the bottom but I would be interested in hearing how you solved this problem.
Well, we haven't got very far yet - long term WIBNIs always fall victim to the latest crisis - but we're basing it on the interface that a plugin implements, with the convention that resources have the same fully qualified name as the interface.
So an interface UsersAPI
may have various different implementations. A method getBundle()
on that interface by default returns the equivalent of ResourceBundle.get("...UsersAPI", locale)
. That file can be replaced, or implementations of UsersAPI can override the method if they need something more complicated.
So far that does what we need, but we're still looking at more flexible solutions based on the plugins.