Dashboard > RIFE > ... > Templates > Localization through ResourceBundles
RIFE Log In   View a printable version of the current page.
Localization through ResourceBundles


Added by Geert Bevin, last edited by Emmanuel Okyere II on Nov 07, 2005  (view change)
Labels: 
(None)


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:

<!--V 'L10N:key'-->default value<!--/V-->

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

hello = Hallo

text_fr.properties

hello = Bonjour

and a template

text.html

Hey mister, I said: "<!--V 'L10N:hello'/-->!"

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:

Hey mister, I said: "Bonjour!"

and just replacing the second line with the following:

...
ResourceBundle bundle = Localization.getResourceBundle("text", "nl");
...

will output:

Hey mister, I said: "Hallo!"

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:

L10N_DEFAULT_RESOURCEBUNDLE
L10N_DEFAULT_LANGUAGE
L10N_DEFAULT_COUNTRY

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.



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators