handlebars-i18next.js adds the internationalization features of i18next and Intl to handlebars.js
handlebars-i18n adds the internationalization features of i18next and Intl to handlebars.js.
Handlebars.js is a slim and convenient templating language but does not come up with build-in localization / internationalization features and is not in the list of i18next’s supported Frameworks. Handlebars-i18next.js bridges the gap. It is usable as node module as well as in browser.
Copyright © 2020 Florian Walzel,
MIT License
$ npm install handlebars-i18n
Usage within node environment:
const HandlebarsI18n = require("handlebars-i18n");
HandlebarsI18n.init();
Usage in web browser:
<script src="handlebars.js"></script>
<script src="i18next.js"></script>
<script src="handlebars-i18n.js"></script>
<script>
HandlebarsI18n.init()
</script>
Initialize i18next with your language strings and default settings:
i18next.init({
resources : {
"en" : {
translation : {
"phrase1": "What is good?",
"phrase2": "{{what}} is good."
}
},
"de" : {
translation: {
"phrase1": "Was ist gut?",
"phrase2": "{{what}} ist gut."
}
}
},
lng : "en"
});
Set your Handlebars.js data object:
let data = {
myItem: "handlebars-i18n",
myPrice: 1200.99,
myDate: "2020-03-11T03:24:00"
}
Initialize handlebars-i18n:
HandlebarsI18n.init();
Optionally configure your language specific number, currency, and date-time defaults:
HandlebarsI18n.configure([
["en", "PriceFormat", {currency: "USD"}],
["de", "PriceFormat", {currency: "EUR"}]
]);
Finally use in template:
<p> {{__ "phrase1"}} </p>
<p> {{__ "phrase2" what=myItem}} </p>
<p> {{_date myDate}} </p>
<p> {{_price myPrice}} </p>
Also see the examples folder in the repo for more details.
Returns the phrase associated with the given key for the selected language. The key can be passed hard encoded in the template when written in quotes:
{{__ "keyToTranslationPhrase"}}
… or it can be referenced via a handlebars variable:
{{__ keyFromHandlebarsData}}
undefinedVariable Replacementundefined
Template usage:
{{__ "whatIsWhat" a="Everything" b="fine"}}
The i18next resource:
"en" : {
translation : {
"whatIsWhat": "{{a}} is {{b}}."
}
},
undefinedPluralsundefined
{{__ "keyWithCount" count=8}}
'en' : {
translation : {
'keyWithCount': '{{count}} item',
'keyWithCount_plural': '{{count}} items',
}
},
Returns the shortcode of i18next’s currently selected language such as “en”, “de”, “fr”, “fi” … etc.
{{_locale}}
Checks a string against i18next’s currently selected language. Returns true or false.
{{#if (localeIs "en")}} ... {{/if}}
Outputs a formated date according to the language specific conventions.
{{_date}}
If called without argument the current date is returned. Any other input date can be passed as a conventional date string, a number (timestamp in milliseconds), or a javascript date array.
undefinedDate argument given as date string:undefined
{{_date "2020-03-11T03:24:00"}}
{{_date "December 17, 1995 03:24:00"}}
undefinedDate argument given as number (milliseconds since begin of unix epoch):undefined
{{_date 1583922952743}}
undefinedDate argument given as javascript date array:undefined
{{_date "[2012, 11, 20, 3, 0, 0]"}}
undefinedAdditional arguments for formatingundefined
You can add multiple arguments for individual formating. See Intl DateTimeFormat for your option arguments.
{{_date 1583922952743 year="2-digit" day="2-digit" timeZone="America/Los_Angeles"}}
Outputs a formated number according to the language specific conventions of number representation, e.g. 4,100,000.8314 for “en”, but 4.100.000,8314 for “de”.
{{_num 4100000.8314 }}
undefinedAdditional arguments for formatingundefined
You can add multiple arguments for individual formating. See Intl NumberFormat for your option arguments.
{{_num 3.14159 maximumFractionDigits=2}}
Outputs a formated currency string according to the language specific conventions of price representation, e.g. €9,999.99 for “en”, but 9.999,99 € for “de”.
{{_price 9999.99}}
undefinedAdditional arguments for formatingundefined
You can add multiple arguments for individual currency formating. See Intl NumberFormat for your option arguments.
{{_price 1000 currency="JPY" minimumFractionDigits=2}}
Instead of defining the formating options for each date, number or price anew, you can configure global settings for all languages or only for specific languages.
HandlebarsI18n.configure("en", "DateTimeFormat", {timeZone: "America/Los_Angeles"});
First argument is the language shortcode or “all” for all languages. Second is the format option you want to address (DateTimeFormat, NumberFormat, or PriceFormat). Third argument ist the options object with the specific settings.
The lookup cascade is:
1st Priority: The argument given in the template, e.g. {{_date timeZone="America/Los_Angeles"}}2nd Priority: The global setting configured for the current language, such as “en”3rd Priority: The global setting configured for all languagesDefault: The Intl default settingundefinedExample:undefined
This defines that all prices for all languages are represented as Dollar:
HandlebarsI18n.configure("all", "PriceFormat", {currency: "USD"});
This defines that all prices for all languages are represented as Dollar, but that for language French the currency is Euro:
HandlebarsI18n.configure([
["all", "PriceFormat", {currency: "USD"}],
["fr", "PriceFormat", {currency: "EUR"}]
]);