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

process the key before it is written in the json file #141

Open
andreidiaconescu opened this issue Mar 22, 2019 · 0 comments
Open

process the key before it is written in the json file #141

andreidiaconescu opened this issue Mar 22, 2019 · 0 comments

Comments

@andreidiaconescu
Copy link

andreidiaconescu commented Mar 22, 2019

Hello,
I have an issue: i need to process a little the key before saving it is written into the json file:

  • some of my keys are surrounded by single quotes;
  • the reason for this is that my project is in angular5, and in templates i translate using a component like: <mtranslate [mKey]="'Save your file'"> (notice the double quotes and inside them the single quotes, as necessary in angular)
  • i need to trim the single quotes, and i try to do that in the transform key, but this only duplicates the keys, keeping the quotes ones, and adding the quotes free ones
    How could i do this ?
    Thank you !

Version

  • i18next: 10.0.0
  • i18next-scanner: 2.10.1

Configuration

{ // i18next-scanner options
    input: [
        'src/app/**/*.{ts,html,htm}',
        '!**/node_modules/**',
    ],
    output: './tmp',
    options: {
        debug: true,
        removeUnusedKeys: true,
        func: {
            list: ['i18nService.translate', 'mi18nService.translate'],
            extensions: ['.ts', '.html', '.htm']
        },
        attr: {
            list: ['[attx]', '[qwe]', '[mkey]', '[akey]'],
            extensions: ['.html', '.htm']
        },
        trans: false,
        lngs: ['fr', 'nl', 'en'],
        ns: [
            'translation',
        ],
        defaultLng: 'fr',
        defaultNs: 'translation',
        defaultValue: '__STRING_NOT_TRANSLATED__',
        resource: {
            loadPath: 'tmp/i18n/translations.{{lng}}.json',
            savePath: 'tmp/i18n/translations.{{lng}}.json',
            jsonIndent: 2,
            lineEnding: '\n'
        },
        nsSeparator: false, // namespace separator
        keySeparator: false, // key separator
        interpolation: {
            prefix: '{{',
            suffix: '}}'
        },
    },
    transform: function customTransform(file, enc, done) {
        "use strict";

        console.log(`Parsing file: file=${JSON.stringify(file.relative)}`);

        const parser = this.parser;
        const content = fs.readFileSync(file.path, enc);
        let count = 0;

        parser.parseAttrFromString(content, (key, options) => {
          console.log(`Found attribute value to be translated translation: ${key}`);
            key = key.replace(/^'/, '').replace(/'$/, '');
            console.log(`Key after trimming single quotes: ${key}`);
            parser.set(key, Object.assign({}, options, {
                nsSeparator: false,
                keySeparator: false
            }));

            // parser.set(key, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
              count;
        });

        if (count > 0) {
            console.log(`i18next-scanner: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`);
        }
        done();
    },
}
@andreidiaconescu andreidiaconescu changed the title prosess the key before it is written in the json file process the key before it is written in the json file Mar 22, 2019
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

1 participant