From 0fd94e7a44db946b66269bbed6d0c27ecf78d708 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 22 Feb 2024 12:43:41 +0100 Subject: [PATCH] Slow Mode: infobox translation. --- conversejs/build-conversejs-patch-i18n.js | 97 +++++++++++++++++++++++ conversejs/build-conversejs.sh | 3 + 2 files changed, 100 insertions(+) create mode 100644 conversejs/build-conversejs-patch-i18n.js diff --git a/conversejs/build-conversejs-patch-i18n.js b/conversejs/build-conversejs-patch-i18n.js new file mode 100644 index 00000000..eb19020c --- /dev/null +++ b/conversejs/build-conversejs-patch-i18n.js @@ -0,0 +1,97 @@ +#!/bin/env node +const fs = require('node:fs') +const path = require('node:path') +const YAML = require('yaml') + +/** + * This script will patch ConverseJS .po files, to add custom strings. + */ + +const livechatDir = path.resolve(__dirname, '..', 'languages') +const converseDir = path.resolve(__dirname, '..', 'build', 'conversejs', 'src', 'i18n') + +// Labels to import: +const labels = loadLabels([ + 'slow_mode_info' +]) + +function loadLabels (keys) { + const labels = {} + const sourceContent = fs.readFileSync(path.resolve(livechatDir, 'en.yml')).toString() + const yaml = YAML.parse(sourceContent) + for (const key of keys) { + labels[key] = yaml[key] + } + return labels +} + +function patch (lang) { + const destLang = lang.replace('-', '_') // zh-Hans => zh_Hans + const destFile = lang === '' + ? path.resolve(converseDir, destLang, 'converse.pot') + : path.resolve(converseDir, destLang, 'LC_MESSAGES', 'converse.po') + + console.log(`Patching ${lang}/${destLang}...`) + if (!fs.existsSync(destFile)) { + console.log(`File ${destFile} does not exist, skipping.`) + return + } + + let yaml + if (lang === '') { // pot file, dont put translation + yaml = {} + } else { + const sourceContent = fs.readFileSync(path.resolve(livechatDir, lang + '.yml')).toString() + yaml = YAML.parse(sourceContent) ?? {} + } + + const destContent = fs.readFileSync(destFile).toString().split(/\n/) + + // FIXME: for a yet-to-understand reason, the first patched string in marked as fuzzy when ConverseJS compiles + // po files. So we insert an unecessary translation first... + patchLabel(destContent, 'Livechat', 'Livechat') + + for (const key in labels) { + const label = labels[key] + patchLabel(destContent, label, yaml[key] ?? '') + } + fs.writeFileSync(destFile, destContent.join('\n')) +} + +function patchLabel (fileLines, label, translation) { + // FIXME: here we are not escaping any string value, considering there is only text in concerned labels for now. + // But we should... + const msgid = `msgid "${label}"` + const msgstr = `msgstr "${translation}"` + console.log(` Patching ${msgid} => ${msgstr} ...`) + for (let i = 0; i < fileLines.length; i++) { + const line = fileLines[i] + if (line !== msgid) { continue } + console.log(` Found ${msgid}`) + // FIXME: we consider there is only one msgstr... Should consider there could be multiple. + fileLines[i + 1] = msgstr + return + } + + console.log(` Did not found ${msgid}, adding at the end`) + fileLines.push( + '# livechat-specific label', + msgid, + msgstr, + '' + ) +} + +if (!fs.existsSync(livechatDir)) { + throw new Error('Missing livechat language dir') +} +if (!fs.existsSync(converseDir)) { + throw new Error('Missing ConverseJS language dir, are you really building it?') +} + +patch('') +const files = fs.readdirSync(livechatDir).filter(f => /\.yml$/.test(f)) +for (const file of files) { + const l = file.replace('.yml', '') + patch(l) +} diff --git a/conversejs/build-conversejs.sh b/conversejs/build-conversejs.sh index df62608f..5e693ec9 100644 --- a/conversejs/build-conversejs.sh +++ b/conversejs/build-conversejs.sh @@ -79,6 +79,9 @@ echo "Adding the custom files..." cp -r "$src_dir/custom/" "$converse_build_dir/custom/" mv "$converse_build_dir/custom/webpack.livechat.js" "$converse_build_dir/" +echo "Patching i18n files to add custom labels..." +/bin/env node conversejs/build-conversejs-patch-i18n.js + if [[ ! -d "$converse_build_dir/node_modules" ]]; then echo "Missing node_modules directory, seems we have to call the makefile..." cd "$converse_build_dir"