| | | |
| | |
| | | {anchor:top} |
| | 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: |
| | |
| | {code:xml} |
| | <!--V 'L10N:key'-->default value<!--/V--> |
| | {code} |
| | |
| | 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: |
| | |
| | h4. text_nl.properties |
| | {noformat} |
| | hello = Hallo |
| | {noformat} |
| | |
| | h4. text_fr.properties |
| | {noformat} |
| | hello = Bonjour |
| | {noformat} |
| | |
| | and a template |
| | h4. text.html |
| | {code:xml} |
| | Hey mister, I said: "<!--V 'L10N:hello'/-->!" |
| | {code} |
| | |
| | The following Java code: |
| | |
| | {code:java} |
| | Template template_html = TemplateFactory.HTML.get("text"); |
| | ResourceBundle bundle = Localization.getResourceBundle("text", "fr"); |
| | template_html.addResourceBundle(bundle); |
| | System.out.println(template.getContent()); |
| | {code} |
| | |
| | will output: |
| | |
| | {noformat}Hey mister, I said: "Bonjour!"{noformat} |
| | |
| | and just replacing the second line with the following: |
| | |
| | {code:java} |
| | ... |
| | ResourceBundle bundle = Localization.getResourceBundle("text", "nl"); |
| | ... |
| | {code} |
| | |
| | will output: |
| | |
| | {noformat}Hey mister, I said: "Hallo!"{noformat} |
| | |
| | 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: |
| | |
| | {noformat} |
| | L10N_DEFAULT_RESOURCEBUNDLE |
| | L10N_DEFAULT_LANGUAGE |
| | L10N_DEFAULT_COUNTRY |
| | {noformat} |
| | |
| | So consider the following configuration setup: |
| | |
| | {code:xml} |
| | <param name="L10N_DEFAULT_RESOURCEBUNDLE">myapplication</param> |
| | <param name="L10N_DEFAULT_LANGUAGE">nl</param> |
| | {code} |
| | |
| | Java code is then able to just write the following line to obtain the ResourceBundle: |
| | |
| | {code:java} |
| | ... |
| | ResourceBundle bundle = Localization.getResourceBundle(); |
| | ... |
| | {code} |
| | |
| | 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. |