mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Tools: Fixed build-translation script to handle .ts and .tsx files directly
This commit is contained in:
parent
0ab235273b
commit
3782255c27
@ -19,6 +19,9 @@ const libDir = `${rootDir}/packages/lib`;
|
|||||||
const { execCommand, isMac, insertContentIntoFile, filename, dirname, fileExtension } = require('./tool-utils.js');
|
const { execCommand, isMac, insertContentIntoFile, filename, dirname, fileExtension } = require('./tool-utils.js');
|
||||||
const { countryDisplayName, countryCodeOnly } = require('@joplin/lib/locale');
|
const { countryDisplayName, countryCodeOnly } = require('@joplin/lib/locale');
|
||||||
|
|
||||||
|
const { GettextExtractor, JsExtractors } = require('gettext-extractor');
|
||||||
|
|
||||||
|
|
||||||
function parsePoFile(filePath) {
|
function parsePoFile(filePath) {
|
||||||
const content = fs.readFileSync(filePath);
|
const content = fs.readFileSync(filePath);
|
||||||
return gettextParser.po.parse(content);
|
return gettextParser.po.parse(content);
|
||||||
@ -113,14 +116,7 @@ async function createPotFile(potFilePath) {
|
|||||||
'./readme/*',
|
'./readme/*',
|
||||||
];
|
];
|
||||||
|
|
||||||
// We get all the .ts and .js files, preferring the .ts file when it's
|
const findCommand = `find . -type f \\( -iname \\*.js -o -iname \\*.ts -o -iname \\*.tsx \\) -not -path '${excludedDirs.join('\' -not -path \'')}'`;
|
||||||
// available (because the .js file is a minified version and gettext might
|
|
||||||
// fail on it).
|
|
||||||
//
|
|
||||||
// As of 2021-11, gettext doesn't process .tsx files so we still need to use
|
|
||||||
// the .js for this.
|
|
||||||
|
|
||||||
const findCommand = `find . -type f \\( -iname \\*.js -o -iname \\*.ts \\) -not -path '${excludedDirs.join('\' -not -path \'')}'`;
|
|
||||||
process.chdir(rootDir);
|
process.chdir(rootDir);
|
||||||
let files = (await execCommand(findCommand)).split('\n');
|
let files = (await execCommand(findCommand)).split('\n');
|
||||||
|
|
||||||
@ -152,26 +148,43 @@ async function createPotFile(potFilePath) {
|
|||||||
|
|
||||||
files.sort();
|
files.sort();
|
||||||
|
|
||||||
// Use this to get the list of files that are going to be processed. Useful
|
// Note: we previously used the xgettext utility, but it only partially
|
||||||
// to debug issues with files that shouldn't be in the list.
|
// supports TypeScript and doesn't support .tsx files at all. Besides; the
|
||||||
// console.info(files.join('\n'));
|
// TypeScript compiler now converts some `_('some string')` calls to
|
||||||
|
// `(0,locale1._)('some string')`, which cannot be detected by xgettext.
|
||||||
|
//
|
||||||
|
// So now we use this gettext-extractor utility, which seems to do the job.
|
||||||
|
// It supports .ts and .tsx files and appears to find the same strings as
|
||||||
|
// xgettext.
|
||||||
|
|
||||||
const baseArgs = [];
|
const extractor = new GettextExtractor();
|
||||||
baseArgs.push('--from-code=utf-8');
|
|
||||||
baseArgs.push(`--output="${potFilePath}"`);
|
const parser = extractor
|
||||||
baseArgs.push('--language=JavaScript');
|
.createJsParser([
|
||||||
baseArgs.push('--copyright-holder="Laurent Cozic"');
|
JsExtractors.callExpression('_', {
|
||||||
baseArgs.push('--package-name=Joplin');
|
arguments: {
|
||||||
baseArgs.push('--package-version=1.0.0');
|
text: 0,
|
||||||
baseArgs.push('--keyword=_n:1,2');
|
context: 1,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
JsExtractors.callExpression('_n', {
|
||||||
|
arguments: {
|
||||||
|
text: 0,
|
||||||
|
textPlural: 1,
|
||||||
|
context: 2,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
parser.parseFile(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
extractor.savePotFile(potFilePath, {
|
||||||
|
'Project-Id-Version': 'Joplin',
|
||||||
|
'Content-Type': 'text/plain; charset=UTF-8',
|
||||||
|
});
|
||||||
|
|
||||||
let args = baseArgs.slice();
|
|
||||||
args = args.concat(files);
|
|
||||||
let xgettextPath = 'xgettext';
|
|
||||||
if (isMac()) xgettextPath = executablePath('xgettext'); // Needs to have been installed with `brew install gettext`
|
|
||||||
const cmd = `${xgettextPath} ${args.join(' ')}`;
|
|
||||||
const result = await execCommand(cmd);
|
|
||||||
if (result && result.trim()) console.error(result.trim());
|
|
||||||
await removePoHeaderDate(potFilePath);
|
await removePoHeaderDate(potFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38082
packages/tools/package-lock.json
generated
38082
packages/tools/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -44,6 +44,7 @@
|
|||||||
"@types/jest": "^26.0.15",
|
"@types/jest": "^26.0.15",
|
||||||
"@types/mustache": "^0.8.32",
|
"@types/mustache": "^0.8.32",
|
||||||
"@types/node": "^14.14.6",
|
"@types/node": "^14.14.6",
|
||||||
|
"gettext-extractor": "^3.5.3",
|
||||||
"gulp": "^4.0.2",
|
"gulp": "^4.0.2",
|
||||||
"jest": "^26.6.3",
|
"jest": "^26.6.3",
|
||||||
"sass": "^1.39.2",
|
"sass": "^1.39.2",
|
||||||
|
Loading…
Reference in New Issue
Block a user