{{ Handlebars.java }} - i18n in Handlebars.java

i18n in Handlebars.java

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:

  • i18n helper

    Thei18nhelper 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".

    Looks at themessagesbundle for the "hello" key.

    {{i18n "hello"}}
    

    Looks at themyMessagesbundle for the "hello" key.

    {{i18n "hello" bundle="myMessages"}}
    

    Looks at themessagesbundle for the "hello" key using thees_ARlocale.

    {{i18n "hello" locale="es_AR"}}
    

    Looks at themessagesbundle for the "hello" key and bind the{0}parameter toHandlebars.java.

    {{i18n "hello" "Handlebars.java"}}
    

    Now let's see how to reuse thebundleat the browser!

  • i18nJs helper

    Thei18nJshelper 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>
    

    Dump the content of themessagesbundle using the default locale.

    {{i18nJs}}
    

    Generate something like:

    <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?

Want to contribute?

  • Fork the project on Github.
  • Create an issue or fix one from the issues list.
  • Share your ideas or ask questions on mailing list - don't hesitate to write a reply - that helps us improve javadocs/FAQ.
  • If you miss a particular feature - browse or ask on the mailing list - don't hesitate to write a reply, show us a sample code and describe the problem.
  • Write a blog post about how you use or extend Handlebars.java.
  • Please suggest changes to javadoc/exception messages when you find something unclear.
  • If you have problems with documentation, find it non intuitive or hard to follow - let us know about it, we'll try to make it better according to your suggestions. Any constructive critique is greatly appreciated. Don't forget that this is an open source project developed and documented in spare time.

Thank you, for reading the Handlebars.java blog.