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

Missing script i18next-scanner #152

Closed
sindhuanamal opened this issue Jun 5, 2019 · 6 comments
Closed

Missing script i18next-scanner #152

sindhuanamal opened this issue Jun 5, 2019 · 6 comments

Comments

@sindhuanamal
Copy link

Hi, i have recently added react-i18next for translating react application. In order to extract translations using i18next-scanner. To do so,
i installed the i18next-scanner and add the configuration to the i18next-scanner.config.js file like below,

/* eslint no-console: 0 /
/
eslint strict: 0 */
const fs = require('fs');
const chalk = require('chalk');
const languages = [
'en', // English (default)
];

module.exports = {
src: [
'app//*.{html,js,jsx}',
// Use ! to filter out files or directories
'!app/
/*.spec.{js,jsx}',
'!app/i18n/',
'!test/
',
'!/node_modules/'
],
dest: './',
options: {
debug: false,
removeUnusedKeys: true,
sort: false,
lngs: languages,
func: {
list: [], // Use an empty array to bypass the default list: i18n.t, i18next.t
extensions: ['.js', '.jsx']
},
trans: {
component: 'I18n',
i18nKey: 'i18nKey',
extensions: ['.js', '.jsx'],
fallbackKey: function(ns, value) {
return value;
}
},
defaultValue: (lng, ns, key) => {
if (lng === 'en') {
return key; // Use key as value for base language
}
return ''; // Return empty string for other languages
},
ns: [
'resource' // default
],
defaultNs: 'resource',
resource: {
loadPath: 'app/i18n/{{lng}}/{{ns}}.json',
savePath: 'app/i18n/{{lng}}/{{ns}}.json', // or 'app/i18n/${lng}/${ns}.saveAll.json'
jsonIndent: 4
},
nsSeparator: ':', // namespace separator
keySeparator: '.', // key separator
plural: false, // No plural form keys
interpolation: {
prefix: '{{',
suffix: '}}'
}
},
transform: function(file, enc, done) {
'use strict';

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

    parser.parseFuncFromString(content, { list: ['i18n._', 'i18n.__'] }, (key, options) => {
        parser.set(key, Object.assign({}, options, {
            nsSeparator: false,
            keySeparator: false
        }));
          count;
    });

    if (count > 0) {
        console.log(`[i18next-scanner] transform: count=${chalk.cyan(count)}, file=${chalk.yellow(JSON.stringify(file.relative))}`);
    }

    done();
}

};

And from command line executed npm run i18next-scanner --config i18next-scanner.config.js 'src/**/*.{js,jsx}' but it throws error missing script i18next-scanner.

Not sure why i get this error. Could you point me to some tutorial that explains how to configure react- i18next-scanner to react-i18next.

Thanks.

@daliusd
Copy link
Contributor

daliusd commented Jun 5, 2019

npm run runs script from packages.json script section.

@sindhuanamal
Copy link
Author

okay i tried using i18next-scanner --config i18next-scanner.config.js 'src/**/*.{js,jsx} doesn't work as well. what is the command to run it. thanks

@sindhuanamal
Copy link
Author

got it working. path to the file was wrong.

@emb-sindgan
Copy link

I have similar problem. Even my path to config file is correct. Can someone help to redirect me to a tutorial which contains end to end usage of i18next-scanner

@vituchon
Copy link

Hi all, i don't what happen with "chalk" it seems to be a library that provides you methods for displaying text in colour in a terminal interface alike, I don't know.. but I do remove it from the config file and then remove the references (it was used for looging issues, so no core functionally is beign touched), and it works.

I leave you the config file that works for me

const fs = require('fs');

module.exports = {
    input: [
        'app/**/*.{js,jsx}',
        // Use ! to filter out files or directories
        '!app/**/*.spec.{js,jsx}',
        '!app/i18n/**',
        '!**/node_modules/**',
    ],
    output: './',
    options: {
        debug: true,
        func: {
            list: ['i18next.t', 'i18n.t'],
            extensions: ['.js', '.jsx']
        },
        trans: {
            component: 'Trans',
            i18nKey: 'i18nKey',
            defaultsKey: 'defaults',
            extensions: ['.js', '.jsx'],
            fallbackKey: function(ns, value) {
                return value;
            },

            // https://react.i18next.com/latest/trans-component#usage-with-simple-html-elements-like-less-than-br-greater-than-and-others-v10.4.0
            supportBasicHtmlNodes: true, // Enables keeping the name of simple nodes (e.g. <br/>) in translations instead of indexed keys.
            keepBasicHtmlNodesFor: ['br', 'strong', 'i', 'p'], // Which nodes are allowed to be kept in translations during defaultValue generation of <Trans>.

            // https://github.com/acornjs/acorn/tree/master/acorn#interface
            acorn: {
                ecmaVersion: 2020,
                sourceType: 'module', // defaults to 'module'
            }
        },
        lngs: ['en','de'],
        ns: [
            'locale',
            'resource'
        ],
        defaultLng: 'en',
        defaultNs: 'resource',
        defaultValue: '__STRING_NOT_TRANSLATED__',
        resource: {
            loadPath: 'i18n/{{lng}}/{{ns}}.json',
            savePath: 'i18n/{{lng}}/{{ns}}.json',
            jsonIndent: 2,
            lineEnding: '\n'
        },
        nsSeparator: false, // namespace separator
        keySeparator: false, // key separator
        interpolation: {
            prefix: '{{',
            suffix: '}}'
        },
        metadata: {},
        allowDynamicKeys: false,
    },
    transform: function customTransform(file, enc, done) {
        "use strict";
        const parser = this.parser;
        const content = fs.readFileSync(file.path, enc);
        let count = 0;

        parser.parseFuncFromString(content, { list: ['i18next._', 'i18next.__'] }, (key, options) => {
            parser.set(key, Object.assign({}, options, {
                nsSeparator: false,
                keySeparator: false
            }));
              count;
        });

        if (count > 0) {
            console.log(`i18next-scanner: count=${count}, file=${JSON.stringify(file.relative)}`);
        }

        done();
    }
};

@vituchon
Copy link

this also provides another config file that works: https://lightrun.com/answers/i18next-i18next-scanner-support-for-react-i18next-namespaces.

I will start to wonder if this project is actually manned by someone.

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

4 participants