i18next / i18next-express-middleware

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

saveMissing is not honored #210

Closed xthezealot closed 4 years ago

xthezealot commented 4 years ago

I tried the most basic example /examples/basic/index.js and figured out that the saveMissing: true option is not honored, so that the backend.addPath seems useless.

backend: {
  loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json',
  addPath: __dirname + '/locales/{{lng}}/{{ns}}.missing.json' // Useless
},
fallbackLng: 'en',
preload: ['en', 'de'],
saveMissing: true, // Not honored
adrai commented 4 years ago

Can you share some logs and more information?

xthezealot commented 4 years ago

The /examples/basic produces this kind of log for each request:

i18next: init: no languageDetector is used and no lng is defined
i18next: initialized {
  debug: true,
  initImmediate: false,
  ns: [ 'translation' ],
  defaultNS: [ 'translation' ],
  fallbackLng: [ 'en' ],
  fallbackNS: false,
  whitelist: false,
  nonExplicitWhitelist: false,
  load: 'all',
  preload: [ 'en', 'de' ],
  simplifyPluralSuffix: true,
  keySeparator: '.',
  nsSeparator: ':',
  pluralSeparator: '_',
  contextSeparator: '_',
  partialBundledLanguages: false,
  saveMissing: true,
  updateMissing: false,
  saveMissingTo: 'fallback',
  saveMissingPlurals: true,
  missingKeyHandler: false,
  missingInterpolationHandler: false,
  postProcess: false,
  postProcessPassResolved: false,
  returnNull: true,
  returnEmptyString: true,
  returnObjects: false,
  joinArrays: false,
  returnedObjectHandler: false,
  parseMissingKeyHandler: false,
  appendNamespaceToMissingKey: false,
  appendNamespaceToCIMode: false,
  overloadTranslationOptionHandler: [Function: handle],
  interpolation: {
    escapeValue: true,
    format: [Function: format],
    prefix: '{{',
    suffix: '}}',
    formatSeparator: ',',
    unescapePrefix: '-',
    nestingPrefix: '$t(',
    nestingSuffix: ')',
    nestingOptionsSeparator: ',',
    maxReplaces: 1000
  },
  backend: {
    loadPath: '/Users/arthur/Downloads/express-i18next-test/locales/{{lng}}/{{ns}}.json',
    addPath: '/Users/arthur/Downloads/express-i18next-test/locales/{{lng}}/{{ns}}.missing.json'
  },
  isClone: true
}
i18next: languageChanged en
i18next: hasLoadedNamespace: i18n.languages were undefined or empty undefined
i18next::translator: key "home.xxx" 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!!!
i18next::translator: missingKey en translation home.xxx home.xxx
i18next: hasLoadedNamespace: i18n.languages were undefined or empty undefined
i18next::backendConnector: did not save key "home.xxx" for namespace "translation" as the 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!!!

I don't see how I can "load a namespace" in this configuration.

adrai commented 4 years ago

I just cloned a fresh copy of this example... changed the home.title key and it works: image image

Can you share your code?

jamuhl commented 4 years ago

i18next: init: no languageDetector is used and no lng is defined is a false positive (just to not search for that problem: https://github.com/i18next/i18next/issues/1413

but does not break the code from running (just a false warning)

dietergeerts commented 4 years ago

I have seen this too, as mentioned in the referenced ticket, though did not found exactly what is happening. What I can do tell is that when using it will the node-file-backend and by setting the language manually, it does work, but by using the node-file-backend and the express-middleware, it doesn't work. So it might be because how express-middleware interacts with the rest?

I'm trying to debug and will raise a PR for that false positive, so debugging will be easier. After that, I will further investigate and report back if I find the root cause of this.

xthezealot commented 4 years ago

I can already tell this bug has appeared after i18next version 17.0.18.

That's why the basic example in this repo still works, it uses outdated packages.

adrai commented 4 years ago

@arthurwhite can confirm

adrai commented 4 years ago

should be fixed with v19.3.4

dietergeerts commented 4 years ago

Thx for fixing this that fast!