i18next-express-middleware: Importing t() from i18next (instead of from request) is resulting in error "Cannot read property 'translator' of undefined"

I’ve setup i18next with the express-middleware package. Everything works fine with translating strings with the lng and t() function from the request object. However if I import it from the i18next instance itself, I get the error "Cannot read property 'translator' of undefined" when calling t() with my own language string (not importing that from i18next, just passing it in hardcoded).

A console.log(i18next) learns that the import works, I also have a t(), it only not works. The use case for this: i’m having a business layer that data fetching and transformations (like translations) and when doing a business layer to business layer call, I not always have the request object available.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 15 (7 by maintainers)

Most upvoted comments

You are right 😃

Setting the t() function like this does work correctly; return { lng: 'en', t: i18next.t.bind(i18next) };

@jamuhl ok, thanks I will try to figure out how it’s better to organise my code then

idk…never used the middleware or i18next with imports on node.js -> so not sure how that works out in the end.

just guess import { t } from 'i18next' might work not as expected - but like said…idk

but using i18next.t from some var i18next = require('i18next') should work

try the use not importing t but using i18next.t