Tuesday, 25 April 2017

i18n-node + express4, how to change language from template?

I use express4 with pug, I want to change language in webpage, But it doesn't work

index.js

i18n.configure({
  defaultLocale: 'en',
  locales: ['cn', 'en'],
  directory: './locales',
  extension: '.js',
  cookie: 'lang',
});

app.use(cookieParser());
app.use(i18n.init);

app.locals._ = i18n.__;

router.js

  router.get('/lang/:lang', (req, res) => {
    let {lang} = req.params;
    i18n.setLocale(lang);
    console.log(i18n.__("你好"));
    res.redirect('back');
  });

index.pug

h2= __('你好')

en.js {"你好": "hello"}

cn.js
{"你好": "你好"}

When I click to /lang/en, console.log(i18n.__("你好")) show me hello, this is Ok, But the template still show me 你好
how can I do it?



via nataila

No comments:

Post a Comment