babel-plugin-i18next-extract icon indicating copy to clipboard operation
babel-plugin-i18next-extract copied to clipboard

BUG: When an extraction error is found, other keys found in same file are all ignored

Open rrlevy opened this issue 5 years ago • 4 comments

When extraction the keys, if an extraction error happens (for example, because of a dynamic key that can't be solved like t(myVariable)), all the remaining valid keys in the entire file will be ignored.

This is extra annoying when using the "discardOldKeys": true configuration, because in this case the existing translations will all be "erased".

rrlevy avatar Oct 31 '19 18:10 rrlevy

Not sure it really can be considered as a bug since my original intention was to crash in such case. But yeah, I see your point and having the plugin continuing no matter what is probably desirable.

gilbsgilbs avatar Oct 31 '19 21:10 gilbsgilbs

Mmh, actually, I think the description of the problem is inaccurate. I wrote a test case and this:

t('key0');
t(broken);
t('key1');

does output:

{"key0": "", "key1": ""}

Can you provide a file that can reproduce the issue? It may happen with other files extractors than t() though.

gilbsgilbs avatar Oct 31 '19 21:10 gilbsgilbs

Here is how to replicate the error.

I'm using a create-react-app project

/src/pages/test.jsx

import React from 'react';
import { useTranslation } from 'react-i18next';

export default function Test(props) {
    const { t } = useTranslation('test-context');

    return (
        <div id="Test">
            <p>{t('test1.label1')}</p>
            <p>{t(param)}</p> {/* This is the error line */}
            <p>{t('test1.label2')}</p>
        </div>
    );
}

/src/pages/test2.jsx

import React from 'react';
import { useTranslation } from 'react-i18next';

export default function Test2(props) {
    const { t } = useTranslation('test-context');

    return (
        <div id="Test">
            <p>{t('test2.label1')}</p>
            <p>{t('test2.label2')}</p>
        </div>
    );
}

packages.json babel configuration:

    "babel": {
        "presets": [
            "react-app"
        ],
        "plugins": [
            [
                "i18next-extract",
                {
                    "locales": [
                        "en"
                    ],
                    "outputPath": "src/translations/{{locale}}/{{ns}}.json",
                    "jsonSpace": 2,
                    "keyAsDefaultValue": [
                        "en"
                    ],
                    "discardOldKeys": true
                }
            ]
        ]
    }

When you run NODE_ENV=development babel 'src/**/*.{js,jsx,ts,tsx} the extracted file will be:

test-context.json

{
  "test2": {
    "label1": "label1",
    "label2": "label2"
  }
}

If you remove the error line and run again NODE_ENV=development babel 'src/**/*.{js,jsx,ts,tsx}:

test-context.json

{
  "test1": {
    "label1": "label1",
    "label2": "label2"
  },
  "test2": {
    "label1": "label1",
    "label2": "label2"
  }
}

If you reintroduce the error line and run again NODE_ENV=development babel 'src/**/*.{js,jsx,ts,tsx}:

test-context.json

{
  "test2": {
    "label1": "label1",
    "label2": "label2"
  }
}

Even if you don't have the second file test2.jsx, the first time you run the extractor with test.jsx with and error, it won't create any file.

rrlevy avatar Nov 04 '19 23:11 rrlevy

Thanks for the reproduction steps. I see, this happens with extractors that extract multiple keys because they raise as soon as they encounter an error. I definitely understand how this is annoying, especially when the error is well known. I'll try to revise how these exceptions work sometimes.

gilbsgilbs avatar Nov 04 '19 23:11 gilbsgilbs