Template helpers / usage

with jade

Basic usage

Template:
// set encoded content (text only)
p
=t("my.key1")
p
=t("myNamespace:my.key2", { count: 5, replaceMe: "something" })
 
// set without encoding (eg. html content)
.myClass!=t("my.key3")
!=t("my.key4", { context: "loggedIn" })
|
// setting arguments
input
(type="text", placeholder!=t('my.key5'))

You can have html content written with multilines in JSON File

JSON:
{
 
"myhtml": [
     
"<p>",
     
"my paragraph",
     
"</p>"
 
]  
}

i18next will join the array with '\n'.

Hint: functions like nesting or variable replacing are still full functional.

for mixins

given JSON (eg. locales/en-US/translation.json):
{
   
"listWithHeader": {
     
"title": "my title",
     
"items": [
       
"item 1 ...",
       
"item 2 ..."
   
]
 
},
}
Template:
mixin ul(items)
 
- each item in items
    li
= item
 
mixin h4AndUl
(entry)
  h4
= entry.title
  ul
      mixin ul
(enty.items)
|
// use mixin
mixin h4AndUl
(t('listWithHeader', {returnObjectTrees: true}))

By using option returnObjectTrees i18next will return an object usefull in mixins.

When using returnObjectTrees i18next won't join arrays so arrays can be used for lists.

Hint: functions like nesting or variable replacing are still full functional.

Using markdown

JSON:
// given resources with array
{          
 
'en-US': {
    translation
: {  
     
"markdownKey": [
       
"### title",
       
"a paragraph",
       
"",
       
"    // some code"
     
]
   
}
 
}
};
registered post processor:
// given registered post processor
i18n
.addPostProcessor("markdown", function(val, key, opts) {
   
return require("markdown").markdown.toHTML(val);
});
Template:
!=t("markdownKey", { postProcess: "markdown" }) // -> will output parsed markdown

Post Processor will be called after regular translation.

Hint: functions like nesting or variable replacing are still full functional.

Using jade

JSON:
// given resources with array
{          
 
'en-US': {
    translation
: {  
     
"jadeKey": [
       
"h1 title",
       
"p a paragraph",
       
"p another paragraph",
       
"    button with nested button"
     
]
   
}
 
}
};
registered post processor:
// given registered post processor
i18n
.addPostProcessor("jade", function(val, key, opts) {
   
return require("jade").compile(val, opts)();
});
Template:
!=t("jadeKey", { postProcess: "jade" }) // -> will output parsed jade

Post Processor will be called after regular translation.

Hint: functions like nesting or variable replacing are still full functional.