i18next: saveMissingTo only saves current language

๐Ÿ› Bug Report

when fallbackLng:false, saveMissing: true and saveMIssingTo: 'all' is set, missing keys only being added to current language instead of all supported languages.

To Reproduce

i18n.js

import i18n from 'i18next';
import Backend from 'i18next-fs-backend';
import {resolve, join} from 'path';
import {lstatSync, readdirSync} from 'fs';

const localePath = resolve('./locales');

i18n
    .use(Backend)
    .init({
        backend: {
            allowMultiLoading: true,
            addPath: `${localePath}/{{lng}}/{{ns}}.json`,
            loadPath: `${localePath}/{{lng}}/{{ns}}.json`
        },
        preload: readdirSync(localePath).filter((fileName) => {
            const joinedPath = join(localePath, fileName);
            const isDirectory = lstatSync(joinedPath).isDirectory();
            return isDirectory;
        }),
        ns: readdirSync(`${localePath}/en`).filter((fileName) => {
            const joinedPath = join(`${localePath}/en`, fileName);
            const isDirectory = lstatSync(joinedPath).isDirectory();
            return !isDirectory;
        }).map(file => file.replace(/\.json/, '')),
        load: 'all',
        updateMissing: true,
        lowerCaseLng: true,
        cleanCode: true,
        defaultNS: 'common',
        nonExplicitSupportedLngs: true,
        keySeparator: false,
        fallbackLng: false,
        debug: true,
        saveMissingTo: 'all',
        saveMissing: true,
        interpolation: {
            escapeValue: false
        },
        supportedLngs: ['en', 'tr', 'de']
    });

export default i18n;

with above configuration, I delete all exceptions.js files, ( also tested with empty exceptions.js), if I call for i18n.changeLanguage('de'); i18n.t('exceptions:user-not-found'), I can see exceptions.js file gets generated in /locales/de/ folder. but even though it is set to all it does not create same exceptions file in other supported languages. The only way to generate them is to change language to missing one and re-trigger.

Expected behavior

when saveMissingTo is set to all it should create/update missing keys in all supported languages array. This will avoid developer to miss keys in different languages.

Your Environment

  • runtime version: node v14, server-side-node + express
  • i18next version: 21.6.16
  • os: Linux

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

which might not make sense as:

  • in case you got nonExplicitSupportedLngs set that list wonโ€™t contain โ€œallโ€ languages you provide
  • saveMissingTo: โ€˜supportedโ€™ would not check in all supported languages if the key exists as it only uses i18next.languages for searching that key existing -> so you will eventual overwrite content

so in most cases i18next has neither all information about your languages nor is 100% sure if keys exist in any of the languages it currently did not load or not use to search that key

The main question is why do you need to write the missings to all your languages? Seems like I miss something there - as I think that is totally unneeded.