1
0
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:
Laurent Cozic 2021-11-09 12:20:07 +00:00
parent 0ab235273b
commit 3782255c27
3 changed files with 19132 additions and 19016 deletions

View File

@ -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);
} }

View File

@ -6,7 +6,7 @@
"packages": { "packages": {
"": { "": {
"name": "@joplin/tools", "name": "@joplin/tools",
"version": "2.5.0", "version": "2.6.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"execa": "^4.1.0", "execa": "^4.1.0",
@ -31,6 +31,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",
@ -1348,6 +1349,16 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/@types/glob": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz",
"integrity": "sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==",
"dev": true,
"dependencies": {
"@types/minimatch": "*",
"@types/node": "*"
}
},
"node_modules/@types/graceful-fs": { "node_modules/@types/graceful-fs": {
"version": "4.1.5", "version": "4.1.5",
"resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz",
@ -1391,6 +1402,12 @@
"pretty-format": "^26.0.0" "pretty-format": "^26.0.0"
} }
}, },
"node_modules/@types/minimatch": {
"version": "3.0.5",
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz",
"integrity": "sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==",
"dev": true
},
"node_modules/@types/mustache": { "node_modules/@types/mustache": {
"version": "0.8.32", "version": "0.8.32",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-0.8.32.tgz", "resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-0.8.32.tgz",
@ -1409,6 +1426,12 @@
"integrity": "sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==", "integrity": "sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==",
"dev": true "dev": true
}, },
"node_modules/@types/parse5": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/@types/parse5/-/parse5-5.0.3.tgz",
"integrity": "sha512-kUNnecmtkunAoQ3CnjmMkzNU/gtxG8guhi+Fk2U/kOpIKjIMKnXGp4IJCgQJrXSgMsWYimYG4TGjz/UzbGEBTw==",
"dev": true
},
"node_modules/@types/prettier": { "node_modules/@types/prettier": {
"version": "2.3.2", "version": "2.3.2",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz",
@ -2726,6 +2749,12 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/css-selector-parser": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/css-selector-parser/-/css-selector-parser-1.4.1.tgz",
"integrity": "sha512-HYPSb7y/Z7BNDCOrakL4raGO2zltZkbeXyAd6Tg9obzix6QhzxCotdBl6VT0Dv4vZfJGVz3WL/xaEI9Ly3ul0g==",
"dev": true
},
"node_modules/cssom": { "node_modules/cssom": {
"version": "0.4.4", "version": "0.4.4",
"resolved": "https://registry.npmjs.org/cssom/-/cssom-0.4.4.tgz", "resolved": "https://registry.npmjs.org/cssom/-/cssom-0.4.4.tgz",
@ -3899,6 +3928,24 @@
"assert-plus": "^1.0.0" "assert-plus": "^1.0.0"
} }
}, },
"node_modules/gettext-extractor": {
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/gettext-extractor/-/gettext-extractor-3.5.3.tgz",
"integrity": "sha512-9EgJ+hmbtAbATdMIvCj4WnrkeDWH6fv1z+IJJ1XCxdcUMGx6JQdVVFTdzJkSyIHh4td53ngoB5EQbavbKJU9Og==",
"dev": true,
"dependencies": {
"@types/glob": "5 - 7",
"@types/parse5": "^5",
"css-selector-parser": "^1.3",
"glob": "5 - 7",
"parse5": "5 - 6",
"pofile": "1.0.x",
"typescript": "2 - 4"
},
"engines": {
"node": ">=6"
}
},
"node_modules/gettext-parser": { "node_modules/gettext-parser": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/gettext-parser/-/gettext-parser-1.3.0.tgz", "resolved": "https://registry.npmjs.org/gettext-parser/-/gettext-parser-1.3.0.tgz",
@ -8187,6 +8234,12 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/pofile": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/pofile/-/pofile-1.0.11.tgz",
"integrity": "sha512-Vy9eH1dRD9wHjYt/QqXcTz+RnX/zg53xK+KljFSX30PvdDMb2z+c6uDUeblUGqqJgz3QFsdlA0IJvHziPmWtQg==",
"dev": true
},
"node_modules/posix-character-classes": { "node_modules/posix-character-classes": {
"version": "0.1.1", "version": "0.1.1",
"resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz", "resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz",
@ -11631,6 +11684,16 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"@types/glob": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.2.0.tgz",
"integrity": "sha512-ZUxbzKl0IfJILTS6t7ip5fQQM/J3TJYubDm3nMbgubNNYS62eXeUpoLUC8/7fJNiFYHTrGPQn7hspDUzIHX3UA==",
"dev": true,
"requires": {
"@types/minimatch": "*",
"@types/node": "*"
}
},
"@types/graceful-fs": { "@types/graceful-fs": {
"version": "4.1.5", "version": "4.1.5",
"resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz", "resolved": "https://registry.npmjs.org/@types/graceful-fs/-/graceful-fs-4.1.5.tgz",
@ -11674,6 +11737,12 @@
"pretty-format": "^26.0.0" "pretty-format": "^26.0.0"
} }
}, },
"@types/minimatch": {
"version": "3.0.5",
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz",
"integrity": "sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==",
"dev": true
},
"@types/mustache": { "@types/mustache": {
"version": "0.8.32", "version": "0.8.32",
"resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-0.8.32.tgz", "resolved": "https://registry.npmjs.org/@types/mustache/-/mustache-0.8.32.tgz",
@ -11692,6 +11761,12 @@
"integrity": "sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==", "integrity": "sha512-Gj7cI7z+98M282Tqmp2K5EIsoouUEzbBJhQQzDE3jSIRk6r9gsz0oUokqIUR4u1R3dMHo0pDHM7sNOHyhulypw==",
"dev": true "dev": true
}, },
"@types/parse5": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/@types/parse5/-/parse5-5.0.3.tgz",
"integrity": "sha512-kUNnecmtkunAoQ3CnjmMkzNU/gtxG8guhi+Fk2U/kOpIKjIMKnXGp4IJCgQJrXSgMsWYimYG4TGjz/UzbGEBTw==",
"dev": true
},
"@types/prettier": { "@types/prettier": {
"version": "2.3.2", "version": "2.3.2",
"resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz", "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz",
@ -12758,6 +12833,12 @@
"which": "^2.0.1" "which": "^2.0.1"
} }
}, },
"css-selector-parser": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/css-selector-parser/-/css-selector-parser-1.4.1.tgz",
"integrity": "sha512-HYPSb7y/Z7BNDCOrakL4raGO2zltZkbeXyAd6Tg9obzix6QhzxCotdBl6VT0Dv4vZfJGVz3WL/xaEI9Ly3ul0g==",
"dev": true
},
"cssom": { "cssom": {
"version": "0.4.4", "version": "0.4.4",
"resolved": "https://registry.npmjs.org/cssom/-/cssom-0.4.4.tgz", "resolved": "https://registry.npmjs.org/cssom/-/cssom-0.4.4.tgz",
@ -13703,6 +13784,21 @@
"assert-plus": "^1.0.0" "assert-plus": "^1.0.0"
} }
}, },
"gettext-extractor": {
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/gettext-extractor/-/gettext-extractor-3.5.3.tgz",
"integrity": "sha512-9EgJ+hmbtAbATdMIvCj4WnrkeDWH6fv1z+IJJ1XCxdcUMGx6JQdVVFTdzJkSyIHh4td53ngoB5EQbavbKJU9Og==",
"dev": true,
"requires": {
"@types/glob": "5 - 7",
"@types/parse5": "^5",
"css-selector-parser": "^1.3",
"glob": "5 - 7",
"parse5": "5 - 6",
"pofile": "1.0.x",
"typescript": "2 - 4"
}
},
"gettext-parser": { "gettext-parser": {
"version": "1.3.0", "version": "1.3.0",
"resolved": "https://registry.npmjs.org/gettext-parser/-/gettext-parser-1.3.0.tgz", "resolved": "https://registry.npmjs.org/gettext-parser/-/gettext-parser-1.3.0.tgz",
@ -17055,6 +17151,12 @@
} }
} }
}, },
"pofile": {
"version": "1.0.11",
"resolved": "https://registry.npmjs.org/pofile/-/pofile-1.0.11.tgz",
"integrity": "sha512-Vy9eH1dRD9wHjYt/QqXcTz+RnX/zg53xK+KljFSX30PvdDMb2z+c6uDUeblUGqqJgz3QFsdlA0IJvHziPmWtQg==",
"dev": true
},
"posix-character-classes": { "posix-character-classes": {
"version": "0.1.1", "version": "0.1.1",
"resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz", "resolved": "https://registry.npmjs.org/posix-character-classes/-/posix-character-classes-0.1.1.tgz",

View File

@ -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",