Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control over translations being added #208

Open
Poky85 opened this issue Jan 24, 2021 · 4 comments
Open

Control over translations being added #208

Poky85 opened this issue Jan 24, 2021 · 4 comments

Comments

@Poky85
Copy link

Poky85 commented Jan 24, 2021

🚀 Feature Proposal

Currently keys that doesn't exist in resource files are added for all languages defined in options.lngs. E.g. when your application has dictionaries in 5 languages and my defaultValue is empty string, then new keys are added in all languages.

en.json

{
  newTranslationFound: '',
}
de.json

{
  newTranslationFound: '',
}
fr.json

{
  newTranslationFound: '',
}

In my i18next app i use fallback language. So translations which doesn't exist in user's language are loaded from fallback language:

i18next.init({
    fallbackLng: 'en',
    lngs: ['en', 'de', 'fr'],
    returnEmptyString: true, // Translations do not fallback because we have empty strings
});

Empty string translations doesn't fallback by default, but I would have to set:

i18next.init({
    fallbackLng: 'en',
    lngs: ['en', 'de', 'fr'],
    returnEmptyString: false, // <-- doesn't take an empty string as valid translation and rather loads translation from fallback language
});

With config above, it fallbacks even when having empty strings in resource files.

I would like to have keys in resource file only when they are translated. Empty strings are unnecessary.

I think we could use defaultValue option for this, see an example.

Motivation

  1. For me empty strings in dictionary files are completely unnecessary and I would like to add new translations to fallbackLng only. In our project developer usually creates message in fallbackLng only, then translations for other languages are added in translation service. Having empty strings unnecessarily increases file size of dictionaries when loaded in browser.

  2. From my experience some translation services have a problem with empty strings. They don't return empty strings in export which means that empty translations are added by scanner and then removed by translation service. And this happens over and over again until key gets translated.

Example

i18next.init({
    fallbackLng: 'en',
    lngs: ['en', 'de', 'fr'],
    defaultValue: (lng) => {
        if (lng !== 'en') {
            return // Undefined means "do not add a key"
        }

        return  ''
    },
});

What do you think the idea?

@Toilal
Copy link

Toilal commented Sep 2, 2021

@Poky85 It seems to already support this behavior.

@Poky85
Copy link
Author

Poky85 commented Sep 2, 2021

@Toilal Which version? Could you post the code showing how to achieve this behavior?

@Toilal
Copy link

Toilal commented Sep 3, 2021

I run v3.0.0, here's my configuration file. My main language is French.

module.exports = {
  options: {
    ns: ['app'],
    defaultNs: 'app',
    removeUnusedKeys: true,
    sort: true,
    func: {
      list: ['i18next.t', 'i18n.t', 'i18nKey', 't', 'tl', '\\$t', '\\$tl'],
      extensions: ['.ts', '.vue']
    },
    trans: {
      component: 'Trans',
      extensions: []
    },
    lngs: ['fr-FR', 'en-GB'],
    fallbackLng: 'fr-FR',
    defaultLng: 'fr-FR',
    defaultValue: (lng, ns, key, entry) => {
      if (lng !== 'fr-FR') {
        return
      }

      return entry.defaultValue
    },
    resource: {
      loadPath: 'src/i18n/{{lng}}/{{ns}}.json',
      savePath: 'src/i18n/{{lng}}/{{ns}}.json',
      jsonIndent: 2,
      lineEnding: '\n'
    }
  }
}

@Toilal
Copy link

Toilal commented Sep 3, 2021

@Poky85 I still have issues in some cases (plurals). It seems https://github.com/i18next/i18next-parser works better, you should have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants