Handlebars Phrase helper
{{phrase key}}
Internationalisation helper for Handlebars
handlebars.phrase is handlebars all the way down so its phrase strings can be templates containing other phrases, variables or helpers.
Version
1.0.4
Installation
npm install handlebars.phrase
Registering the helper
var Handlebars = require("handlebars");
var Phrase = require("handlebars.phrase");
Phrase.registerHelpers(Handlebars);
Setting the languages
Phrase.setLanguages({
en: {
"foo": "Foo",
"bar": "Bar {{x}}",
"wibble": "{{phrase 'wobble'}}",
"wobble": "Wibble Wobble",
"baz.qux": "Baz Qux"
},
fr: {
"foo": "Un Foo",
...
}
});
Setting the default locale
Phrase.locale("en");
Using the helper
The following examples assume the above languages and locale have been set and a context of { x: "X" }
Output a phrase using a key
{{phrase "foo"}} → «Foo»
Output a phrase, interpolating a variable from the context
{{phrase "bar"}} → «Bar X»
Explicitly interpolate a variable
{{phrase "bar" x="Y"}} → «Bar Y»
Output a phrase that contains a nested phrase
{{phrase "wibble"}} → «{{phrase 'wobble'}}» → «Wibble Wobble»
Output a phrase for a particular locale
{{phrase "foo" "fr"}}
{{phrase "foo" locale="fr"}} → «Mon foo»
Output a phrase for a key appended with a suffix
{{phrase "baz" _append="qux"}} → «Baz Qux»
Output a phrase for a key prepended with a prefix
{{phrase "qux" _prepend="baz"}} → «Baz Qux»
Output a phrase wrapped in html for debugging purposes
{{phrase "foo" _debug=true}} → «<phrase data-phrase-key="foo">Foo</phrase>»
Alternative shorter syntax
((foo)) → «Foo»
((bar x="Y")) → «Bar Y»
See phrase helper for more details
Additional methods
Phrase.get
var str = Phrase.get(key, params);
Returns a Handlebars SafeString for key
Phrase.getString
var str = Phrase.getString(key, params);
Returns string for key
See handlebars.phrase.getString
Phrase.locale
Phrase.locale([loc]);
Get (or set) Phrase’s current locale
Phrase.setLanguages
Phrase.setLanguages(langs, options);
Sets languages to be used
See handlebars.phrase.setLanguages
Phrase.setLanguage
Phrase.setLanguages(lang, phrases);
Sets individual language
Phrase.addLanguages
Phrase.addLanguages(langs);
Adds to languages already set
See handlebars.phrase.addLanguages
Phrase.addLanguage
Phrase.addLanguage(lang, phrases);
Adds to a previously set language
See handlebars.phrase.addLanguage
Tests
npm test
To see output generated by tests
npm run test:output