Note that there are some explanatory texts on larger screens.

plurals
  1. POLocales and ResourceBundles in a plugin-based program
    primarykey
    data
    text
    <p>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.</p> <p>As far as I understand it, Java's <code>ResourceBundle</code> work like this. You create a class that extends <code>ResourceBundle</code>, called something like <code>MyProgramStrings</code>, and also language-specific classes called <code>MyProgramStrings_fr</code>, <code>MyProgramStrings_es</code> 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.</p> <p>You look up values in two stages: first you get the correct bundle, then you query it for the string you want.</p> <pre><code>Locale locale = Locale.getDefault(); // or = new Locale("en", "GB"); ResourceBundle rb = ResourceBundle.getBundle("MyProgramStrings", locale); String wotsitName = rb.getString("wotsit.name"); </code></pre> <p>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.</p> <p>I'm a little lost in all this. Can anybody help?</p> <hr> <p><strong>Update:</strong> David Waters asked:</p> <blockquote> <p>I have put my answer at the bottom but I would be interested in hearing how you solved this problem.</p> </blockquote> <p>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.</p> <p>So an interface <code>UsersAPI</code> may have various different implementations. A method <code>getBundle()</code> on that interface by default returns the equivalent of <code>ResourceBundle.get("...UsersAPI", locale)</code>. That file can be replaced, or implementations of UsersAPI can override the method if they need something more complicated.</p> <p>So far that does what we need, but we're still looking at more flexible solutions based on the plugins.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload