1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/Tools/build-translation.js

252 lines
8.6 KiB
JavaScript
Raw Normal View History

"use strict"
require('app-module-path').addPath(__dirname + '/../ReactNativeClient');
2017-12-04 19:55:06 +02:00
const rootDir = __dirname + '/..';
2017-07-18 20:04:47 +02:00
const processArgs = process.argv.splice(2, process.argv.length);
const silentLog = processArgs.indexOf('--silent') >= 0;
2017-12-04 19:55:06 +02:00
const { basename, dirname, filename, fileExtension } = require(rootDir + '/ReactNativeClient/lib/path-utils.js');
const fs = require('fs-extra');
const gettextParser = require('gettext-parser');
2017-07-18 20:04:47 +02:00
const cliDir = rootDir + '/CliClient';
const cliLocalesDir = cliDir + '/locales';
const rnDir = rootDir + '/ReactNativeClient';
2017-11-12 02:44:26 +02:00
const electronDir = rootDir + '/ElectronClient/app';
2017-07-18 20:04:47 +02:00
2017-12-04 20:16:14 +02:00
const { execCommand } = require('./tool-utils.js');
2018-02-01 22:21:54 +02:00
const { countryDisplayName, countryCodeOnly } = require('lib/locale.js');
function parsePoFile(filePath) {
const content = fs.readFileSync(filePath);
return gettextParser.po.parse(content);
}
function serializeTranslation(translation) {
let output = {};
const translations = translation.translations[''];
for (let n in translations) {
if (!translations.hasOwnProperty(n)) continue;
if (n == '') continue;
2017-07-19 00:14:20 +02:00
const t = translations[n];
if (t.comments && t.comments.flag && t.comments.flag.indexOf('fuzzy') >= 0) {
output[n] = t['msgid'];
} else {
output[n] = t['msgstr'][0];
}
}
return JSON.stringify(output);
}
function saveToFile(filePath, data) {
fs.writeFileSync(filePath, data);
}
function buildLocale(inputFile, outputFile) {
const r = parsePoFile(inputFile);
const translation = serializeTranslation(r);
saveToFile(outputFile, translation);
}
2017-07-19 22:39:48 +02:00
async function removePoHeaderDate(filePath) {
2017-12-01 22:06:20 +02:00
// Note: on macOS this will fail because it needs to be 'sed -i ""'
// Solution would be to install gsed, detect it here, and use it in place of sed in macOS
// https://stackoverflow.com/questions/30003570/how-to-use-gnu-sed-on-mac-os-x#34815955
await execCommand('sed -i -e\'/POT-Creation-Date:/d\' "' + filePath + '"');
await execCommand('sed -i -e\'/PO-Revision-Date:/d\' "' + filePath + '"');
2017-07-19 22:39:48 +02:00
}
2017-07-18 20:04:47 +02:00
async function createPotFile(potFilePath, sources) {
let baseArgs = [];
baseArgs.push('--from-code=utf-8');
baseArgs.push('--output="' + potFilePath + '"');
baseArgs.push('--language=JavaScript');
baseArgs.push('--copyright-holder="Laurent Cozic"');
baseArgs.push('--package-name=Joplin-CLI');
baseArgs.push('--package-version=1.0.0');
2017-07-24 20:08:01 +02:00
baseArgs.push('--no-location');
2017-07-18 20:04:47 +02:00
for (let i = 0; i < sources.length; i++) {
let args = baseArgs.slice();
if (i > 0) args.push('--join-existing');
args.push(sources[i]);
const result = await execCommand('xgettext ' + args.join(' '));
if (result) console.error(result);
2017-07-19 22:39:48 +02:00
await removePoHeaderDate(potFilePath);
2017-07-18 20:04:47 +02:00
}
}
async function mergePotToPo(potFilePath, poFilePath) {
const command = 'msgmerge -U "' + poFilePath + '" "' + potFilePath + '"';
const result = await execCommand(command);
if (result) console.error(result);
2017-07-19 22:39:48 +02:00
await removePoHeaderDate(poFilePath);
2017-07-18 20:04:47 +02:00
}
2017-07-19 23:26:30 +02:00
function buildIndex(locales) {
let output = [];
output.push('var locales = {};');
for (let i = 0; i < locales.length; i++) {
const locale = locales[i];
output.push("locales['" + locale + "'] = require('./" + locale + ".json');");
}
output.push('module.exports = { locales: locales };');
return output.join("\n");
}
2017-11-30 20:29:10 +02:00
function availableLocales(defaultLocale) {
const output = [defaultLocale];
fs.readdirSync(cliLocalesDir).forEach((path) => {
if (fileExtension(path) !== 'po') return;
const locale = filename(path);
if (locale === defaultLocale) return;
output.push(locale);
});
return output;
}
2018-02-10 15:03:01 +02:00
function extractTranslator(regex, poContent) {
const translatorMatch = poContent.match(regex);
let translatorName = '';
if (translatorMatch && translatorMatch.length >= 1) {
translatorName = translatorMatch[1];
translatorName = translatorName.replace(/["\s]+$/, '');
translatorName = translatorName.replace(/\\n$/, '');
translatorName = translatorName.replace(/^\s*/, '');
}
if (translatorName.indexOf('FULL NAME') >= 0) return '';
if (translatorName.indexOf('LL@li.org') >= 0) return '';
return translatorName;
}
async function translationStatus(isDefault, poFile) {
// "apt install translate-toolkit" to have pocount
const command = 'pocount "' + poFile + '"';
const result = await execCommand(command);
const matches = result.match(/translated:\s*?(\d+)\s*\((.+?)%\)/);
if (matches.length < 3) throw new Error('Cannot extract status: ' + command + ':\n' + result);
const percentDone = Number(matches[2]);
if (isNaN(percentDone)) throw new Error('Cannot extract percent translated: ' + command + ':\n' + result);
let translatorName = '';
const content = await fs.readFile(poFile, 'utf-8');
2018-02-10 15:03:01 +02:00
translatorName = extractTranslator(/Last-Translator:\s*?(.*)/, content);
if (!translatorName) {
translatorName = extractTranslator(/Language-Team:\s*?(.*)/, content);
}
// Remove <> around email otherwise it's converted to HTML with (apparently) non-deterministic
// encoding, so it changes on every update.
2018-05-09 18:06:02 +02:00
translatorName = translatorName.replace(/ </, ' (');
translatorName = translatorName.replace(/>/, ')');
return {
percentDone: isDefault ? 100 : percentDone,
translatorName: translatorName,
};
}
2018-02-10 14:52:57 +02:00
function flagImageUrl(locale) {
2018-09-03 13:13:59 +02:00
const baseUrl = 'https://joplin.cozic.net/images/flags';
if (locale === 'eu') return baseUrl + '/es/basque_country.png';
if (locale === 'gl_ES') return baseUrl + '/es/galicia.png';
if (locale === 'ca') return baseUrl + '/es/catalonia.png';
if (locale === 'ko') return baseUrl + '/country-4x3/kr.png';
2018-09-03 13:16:35 +02:00
if (locale === 'sv') return baseUrl + '/country-4x3/se.png';
2018-09-09 21:05:36 +02:00
if (locale === 'no') return baseUrl + '/country-4x3/no.png';
2018-09-16 20:53:06 +02:00
if (locale === 'ro') return baseUrl + '/country-4x3/ro.png';
2018-09-03 13:13:59 +02:00
return baseUrl + '/country-4x3/' + countryCodeOnly(locale).toLowerCase() + '.png'
2018-02-10 14:52:57 +02:00
}
function poFileUrl(locale) {
return 'https://github.com/laurent22/joplin/blob/master/CliClient/locales/' + locale + '.po';
}
function translationStatusToMdTable(status) {
let output = [];
output.push(['&nbsp;', 'Language', 'Po File', 'Last translator', 'Percent done'].join(' | '));
2018-02-01 22:21:54 +02:00
output.push(['---', '---', '---', '---', '---'].join('|'));
for (let i = 0; i < status.length; i++) {
const stat = status[i];
const flagUrl = flagImageUrl(stat.locale);
output.push(['![](' + flagUrl + ')', stat.languageName, '[' + stat.locale + '](' + poFileUrl(stat.locale) + ')', stat.translatorName, stat.percentDone + '%'].join(' | '));
}
return output.join('\n');
}
async function updateReadmeWithStats(stats) {
const mdTableMarkerOpen = '<!-- LOCALE-TABLE-AUTO-GENERATED -->\n';
const mdTableMarkerClose = '\n<!-- LOCALE-TABLE-AUTO-GENERATED -->';
let mdTable = translationStatusToMdTable(stats);
mdTable = mdTableMarkerOpen + mdTable + mdTableMarkerClose;
let content = await fs.readFile(rootDir + '/README.md', 'utf-8');
2018-02-01 22:21:54 +02:00
// [^]* matches any character including new lines
const regex = new RegExp(mdTableMarkerOpen + '[^]*?' + mdTableMarkerClose);
content = content.replace(regex, mdTable);
await fs.writeFile(rootDir + '/README.md', content);
}
2017-07-18 20:04:47 +02:00
async function main() {
let potFilePath = cliLocalesDir + '/joplin.pot';
let jsonLocalesDir = cliDir + '/build/locales';
const defaultLocale = 'en_GB';
await createPotFile(potFilePath, [
cliDir + '/app/*.js',
2017-11-12 02:44:26 +02:00
cliDir + '/app/gui/*.js',
electronDir + '/*.js',
electronDir + '/gui/*.js',
2017-07-18 20:04:47 +02:00
rnDir + '/lib/*.js',
rnDir + '/lib/models/*.js',
rnDir + '/lib/services/*.js',
2017-07-19 23:26:30 +02:00
rnDir + '/lib/components/*.js',
rnDir + '/lib/components/shared/*.js',
2017-07-19 23:26:30 +02:00
rnDir + '/lib/components/screens/*.js',
2017-07-18 20:04:47 +02:00
]);
await execCommand('cp "' + potFilePath + '" ' + '"' + cliLocalesDir + '/' + defaultLocale + '.po"');
fs.mkdirpSync(jsonLocalesDir, 0o755);
let stats = [];
2017-11-30 20:29:10 +02:00
let locales = availableLocales(defaultLocale);
2017-07-18 20:04:47 +02:00
for (let i = 0; i < locales.length; i++) {
const locale = locales[i];
const poFilePäth = cliLocalesDir + '/' + locale + '.po';
const jsonFilePath = jsonLocalesDir + '/' + locale + '.json';
if (locale != defaultLocale) await mergePotToPo(potFilePath, poFilePäth);
buildLocale(poFilePäth, jsonFilePath);
const stat = await translationStatus(defaultLocale === locale, poFilePäth);
stat.locale = locale;
stat.languageName = countryDisplayName(locale);
stats.push(stat);
2017-07-18 20:04:47 +02:00
}
stats.sort((a, b) => a.languageName < b.languageName ? -1 : +1);
2017-07-19 23:26:30 +02:00
saveToFile(jsonLocalesDir + '/index.js', buildIndex(locales));
const rnJsonLocaleDir = rnDir + '/locales';
await execCommand('rsync -a "' + jsonLocalesDir + '/" "' + rnJsonLocaleDir + '"');
2017-11-04 13:46:06 +02:00
2017-11-12 02:44:26 +02:00
const electronJsonLocaleDir = electronDir + '/locales';
2017-11-04 13:46:06 +02:00
await execCommand('rsync -a "' + jsonLocalesDir + '/" "' + electronJsonLocaleDir + '"');
await updateReadmeWithStats(stats);
2017-07-18 20:04:47 +02:00
}
main().catch((error) => {
console.error(error);
});