gilbsgilbs / babel-plugin-i18next-extract

Babel plugin that statically extracts i18next and react-i18next translation keys.
https://i18next-extract.netlify.com
MIT License
161 stars 37 forks source link

translation resource files get overwritten #271

Open stoefln opened 9 months ago

stoefln commented 9 months ago

Describe the bug

When I change a string somewhere it seems that the babel plugin regenerates the all translation files. For example I added german translations to ./extractedTranslations/de/common.json, but those were deleted when I edited some strings and the compiler ran again.

How to reproduce

I am not sure if this is only on my end, or if this is expected behavior. Am I missing a config setting somewhere?

  1. I start webpack, and my electron project
  2. Files are generated: extractedTranslations/en/common.json, extractedTranslations/de/common.json
  3. I translate a string in extractedTranslations/de/common.json and place it in the respective json field
  4. I edit some source that contains a trans string (just edit "Validate license" -> "Validate licenses")
  5. previously translated string is reset (file is probably completely regenerated)

Babel configuration:

{
  "presets": ["@babel/preset-env", "@babel/preset-react"],
  "env": {
    "production": {
      "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-syntax-optional-chaining"
      ]
    },
    "development": {
      "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-syntax-optional-chaining",
        "react-refresh/babel",
        [
          "i18next-extract", // https://i18next-extract.netlify.app/#/configuration
          {
            "locales": ["en", "de"],
            "defaultNS": "common",
            "keySeparator": null,
            "nsSeparator": "::",
            "defaultValue": null,
            "discardOldKeys": true,
            "tFunctionNames": ["i18nTrans"]
          }
        ]
      ]
    },
    "test": {
      "plugins": [
        ["@babel/plugin-proposal-decorators", { "legacy": true }],
        "@babel/plugin-proposal-class-properties",
        "@babel/plugin-syntax-optional-chaining"
      ]
    }
  }
}

Expected behavior

I'd expect the plugin to only remove the keys that are not existing anymore and change the ones that I changed in my sources.

What actually happens

Resource files get overwritten.

Your environment

stoefln commented 9 months ago

Just realized, that if I am restarting the webpack dev server process between the edits, that I can work around the issue. It's just really annoying and I have to be super-careful not to accidentally delete stuff that was translated already

jmlavoier commented 5 months ago

It seems to be an issue when using hot reload. The need of restart the server every changes I do on the code is showing that there is something wrong with this library with new webpack version. I'm trying to figure out this, but if anyone here already sorted out this, please update this issue with the solution.