i18next / i18next-express-middleware

[deprecated] can be replaced with i18next-http-middleware
https://github.com/i18next/i18next-http-middleware
MIT License
206 stars 52 forks source link

How to get translates from json file ? #204

Closed golubvladimir closed 4 years ago

golubvladimir commented 4 years ago

My config

i18next
  .use(i18nextMiddleware.LanguageDetector)
  .use(FilesystemBackend)
  .init({
    preload: ["en", "de"],
    detection: {
      order: ['path'],
      lookupPath: 'lng',
      caches: false
    },
    backend: {
      loadPath: '/lang/json/{{lng}}.json',
      jsonIndent: 2,
    }
  });

app.use(
  i18nextMiddleware.handle(i18next, {
    removeLngFromUrl: false
  })
);

app.get('/:lng/test-lang', (req, res) => {
  console.log(req);
  console.log(req.language);
  res.json({status: 'ok'});
});

How to get translates from json file ? What is req option ? How does preload option work ?

golubvladimir commented 4 years ago

It does not work

app.get('/:lng/test-lang', (req, res) => {
  console.log(req);
  console.log(req.language);
  console.log(i18next.t('test'));
});
golubvladimir commented 4 years ago

GET

http://localhost:3001/ru/test-lang
console.log(i18next.language);
undefined
golubvladimir commented 4 years ago

I fix my config

i18next
  .use(i18nextMiddleware.LanguageDetector)
  .use(FilesystemBackend)
  .init({
    debug: true,
    fallbackLng: ['en'],
    preload: ["en", "ru"],
    detection: {
      order: ['path'],
      lookupPath: 'lng',
      caches: false
    },
    backend: {
      loadPath: 'lang/json/{{lng}}.json',
      jsonIndent: 2,
    }
  });

app.use(
  i18nextMiddleware.handle(i18next, {
    removeLngFromUrl: false
  })
);

app.get('/:lng/test-lang', (req, res) => {
  console.log(i18next.language);
  console.log(i18next.t('test'));
});

For start, I have messages:

i18next::backendConnector: loaded namespace translation for language en {
  test: 'Test',
...

i18next::backendConnector: loaded namespace translation for language ru {
  test: 'Тест',
...

i18next: languageChanged ru
undefined
i18next: hasLoadedNamespace: i18n.languages were undefined or empty undefined
i18next::translator: key "test" for namespace "translation" for languages "en" won't get resolved as namespace was not yet loaded This means something IS WRONG in your application setup. You access the t function before i18next.init
 / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!
Test

How to fix it ?

golubvladimir commented 4 years ago

It works with warnings.

console.log(req.t('test'));
i18next: languageChanged ru
i18next: hasLoadedNamespace: i18n.languages were undefined or empty undefined
i18next::translator: key "test" for namespace "translation" for languages "ru, en" won't get resolved as namespace was not yet loaded This means something IS WRONG in your application setup. You access the t function before i18next.
init / i18next.loadNamespace / i18next.changeLanguage was done. Wait for the callback or Promise to resolve before accessing it!!!
Тест
jamuhl commented 4 years ago

forget about:

app.get('/:lng/test-lang', (req, res) => {
  console.log(i18next.language);
  console.log(i18next.t('test'));
});

i18next.language is the main instance of i18next (singleton) which is useless in an async world were requests from different users with different languages are processed in an event loop...(using that miain instance would lead to mixed language output as request B overwrites language of request A)...

always access req.language and req.i18n and req.t