David Moreno García
David Moreno García

Reputation: 4523

Internationalize nodejs jade templates

I'm trying to internationalize my nodejs express app using i18n-2 module. All is working but I have a question. Is there a way to translate string from my jade templates. Imagine I have 100 strings in my website. Do I have to send 100 translations to the template through the res.render invocation?

res.render('profile', {
  title: 'My cool title',
  user: req.user,
  hello1: req.i18n.__("hello1"),
  hello2: req.i18n.__("hello2"),
  hello3: req.i18n.__("hello3"),
  ...
  helloN: req.i18n.__("helloN")
});

Is there another way to do this? Somethin like the next code:

res.render('profile', {
  title: 'My cool title',
  user: req.user,
  i18n: req.i18n // to be used inside jade
});

Upvotes: 2

Views: 1445

Answers (1)

JME
JME

Reputation: 3642

i18n-2 already registers helper objects in your Express locals, which are accessible form your Jade template. These helper methods are registered automatically: "__", "__n", "getLocale", and "isPreferredLocale". Without any additional configuration, should be able to do the following in your Jade template:

a(href="/") #{ __('home') }

Upvotes: 1

Related Questions