Перейти до основного вмісту

Handlebars

To enable handlebars support make use of the viewEngine option inside your I18nModule.

src/app.module.ts
  I18nModule.forRoot({
fallbackLanguage: 'en',
loaders: [
new I18nJsonLoader({
path: path.join(__dirname, '/i18n/'),
}),
],
+ viewEngine: 'hbs'
})
обережно

Handlebars is imported dynamically, so make sure to install it (npm i hbs). Otherwise @softkit/i18n can't register the helper function.

Example usage

Let's try to do some translations with handlebar templates.

src/i18n/en/test.json
{
"HELLO": "Hello {username}"
}
src/app.controller.ts
@Controller('Test')
export class TestController {
@Get('/')
@Render('page')
index(): any {
return { helloArgs: { username: 'John' } };
}
}
src/view/page.hbs
<html>
<body>
<h1>{{t 'test.HELLO' helloArgs}}</h1>
</body>
</html>
порада

The third argument helloArgs is optional. This is only needed if you want to pass along arguments to your translations.

Result


  

Hello John