Open bsdis opened 4 years ago
Easiest way to do this (so far as I know) is to run tsc first, then run your scanner on its output. I have this in my package.json.
scripts: {
"i18n": "rm -rf tmp/i18n; npx tsc --jsx preserve --outDir tmp/i18n && npx i18next-scanner",
...
}
why compiling ts/tsx is required to extract translation ?
Because i18next-scanner doesn't parse typescript files, so it'll choke when it hits syntax it doesn't recognize. Compiling removes all that syntax.
Is there any change that i18next-scanner
will get TS support?
I haven't really went to far with this, but seems like Parser actually run over Ts files:
const fs = require('fs');
const { Parser } = require('i18next-scanner');
const parser = new Parser();
let content = '';
content = fs.readFileSync('src/components/Home/Home.tsx', 'utf-8');
parser
.parseFuncFromString(content, {
list: ['t', 'i18n.t'],
extensions: ['.ts', '.tsx'],
});
console.log(parser.get());
@ImADrafter : where I have to put above code inside where and how to run it ? `const gulp = require('gulp'); const sort = require('gulp-sort'); const scanner = require('i18next-scanner');
gulp.task('extract', function () { return gulp //.src(['src/*/.{js,ts,tsx,jsx}']) // add path of files to get scanned for extraction .src(['../../.{tsx}']) .pipe(sort()) // Sort files in stream by path .pipe( scanner({ quiet: true, lngs: ['en'], // add supported languages interpolation: { prefix: '{{', suffix: '}}', }, resource: { loadPath: '../.json', // add your folder path to save the extracted files savePath: 'i18n/{{lng}}/{{ns}}.json', }, }), ) .pipe(gulp.dest('assets')); });
running above script `
Is there any support or solution or workaround on tsx file...
I haven't really went to far with this, but seems like Parser actually run over Ts files:
const fs = require('fs'); const { Parser } = require('i18next-scanner'); const parser = new Parser(); let content = ''; content = fs.readFileSync('src/components/Home/Home.tsx', 'utf-8'); parser .parseFuncFromString(content, { list: ['t', 'i18n.t'], extensions: ['.ts', '.tsx'], }); console.log(parser.get());
how and where I have to run this file
how to run this scanner over tsx file any solution for same, will helpful
Can I extract language from a tsx file (react component) as shown below? If yes, what config file do I need to make this happen?