Access to i18next Translation into functions not in App.js

Im using ExpressJS and i18next.

in app.js

 var express = require('express') , i18n = require('i18next') , user = require('./routes/user') ... //internationalization i18n.init({ lng: 'en-US', saveMissing: true, debug: true }); ... app.use(i18n.handle); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); ... i18n.registerAppHelper(app); ... app.post('/users/save', user.save); 

I can access the translation in jade:

 t('app.title') 

How can I access translation in routes.user.js

 exports.save = function(req, res){ //t('app.title') } 
+4
source share
2 answers

t is available in your route handlers as res.locals.t . This should also work in Express 3 and 2.

+2
source

i18next-express-middleware -> the translation function can be accessed via req.i18n.t or just req.t but, obviously, under res.locals.t - for access to templates.

0
source

Source: https://habr.com/ru/post/1487412/


All Articles