It's possible to easily localize templates through the standard ResourceBundle mechanism that's available in Java. The strings that are localized are automatically filtered and replaced values tags, just as for config value replacement. The format for localized values is:
Of course, before replacement, the template instance has to know where to look for the key. An additional setResourceBundle(ResourceBundle bundle) method has been added to the Template class, just for this purpose.
For example, consider the following resource bundles:
text_nl.properties
text_fr.properties
and a template
text.html
The following Java code:
Template template_html = TemplateFactory.HTML.get("text");
ResourceBundle bundle = Localization.getResourceBundle("text", "fr");
template_html.addResourceBundle(bundle);
System.out.println(template.getContent());
will output:
and just replacing the second line with the following:
...
ResourceBundle bundle = Localization.getResourceBundle("text", "nl");
...
will output:
The examples use the convenience class Localization that's available in com.uwyn.rife.tools, but any other means of obtaining a ResourceBundle can be used. The advantage of the Localization class is that it closely integrates with RIFE's configuration and it thus picks up the default settings for the resource bundle basename, language and country if they haven't been specified through method arguments. The corresponding configuration parameter names are:
So consider the following configuration setup:
<param name="L10N_DEFAULT_RESOURCEBUNDLE">myapplication</param>
<param name="L10N_DEFAULT_LANGUAGE">nl</param>
Java code is then able to just write the following line to obtain the ResourceBundle:
...
ResourceBundle bundle = Localization.getResourceBundle();
...
This makes it extremely easily to ship a localized application and allow the users to set up a fixed language beforehand, while still allowing a lot of flexibility to allow dynamic localization behaviour.