In this section you will learn how to apply internationalization techniques in Handlebars.java.
Integration between the front-end and back-end can be done in one of two ways:
Thei18n
helper is a helper built on top of java.util.ResourceBundle.
Syntax is as follows:
{{i18n "key" [arg1, ..., argN] [locale="current"] [bundle="messages"]}}
Where key
is the name of the message.
The locale
option let you switch/change the default locale. Default is the current locale.
The bundle
option let you switch/change the default resource bundle. Default is: "messages".
messages
bundle for the "hello" key.
{{i18n "hello"}}
myMessages
bundle for the "hello" key.
{{i18n "hello" bundle="myMessages"}}
messages
bundle for the "hello" key using thees_AR
locale.
{{i18n "hello" locale="es_AR"}}
messages
bundle for the "hello" key and bind the{0}
parameter toHandlebars.java
.
{{i18n "hello" "Handlebars.java"}}
Now let's see how to reuse thebundle
at the browser!
Thei18nJs
helper is a helper built on top of i18n.js JavaScript library.
Syntax is as follows:
{{i18nJs ["locale"] [bundle="messages"]}}
Example:
<script type="text/javascript" src="i18n.js"></script>
messages
bundle using the default locale.
{{i18nJs}}
<script type="text/javascript">
I18n.defaultLocale = 'es_AR';
I18n.locale = 'es_AR';
I18n.translations = I18n.translations || {};
// Spanish (Argentina)
I18n.translations['es_AR'] = {
"hello": "Hi {{arg0}}!"
}
</script>
Interpolation of variables in i18n.js are possible too, because expressions likeHello {0}!
are translated to Hello {{arg0}}!.
Cool, isn't?
Thank you, for reading the Handlebars.java blog.