| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
import fs from 'node:fs'; |
| 3 |
import path from 'node:path'; |
| 4 |
import process from 'node:process'; |
| 5 |
|
| 6 |
import gettext_parser from "gettext-parser"; |
| 7 |
|
| 8 |
const config = {}; |
| 9 |
|
| 10 |
const DEFAULT_WRAPPER = 'cockpit.locale(PO_DATA);'; |
| 11 |
const RTL_LANGS = new Set(["ar", "fa", "he", "ur"]); |
| 12 |
|
| 13 |
function get_po_files() { |
| 14 |
const poDir = path.resolve(config.srcdir, 'po'); |
| 15 |
try { |
| 16 |
return fs.readdirSync(poDir) |
| 17 |
.filter(file => file.endsWith('.po')) |
| 18 |
.map(file => path.join(poDir, file)); |
| 19 |
} catch (error) { |
| 20 |
return []; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
function get_plural_expr(statement) { |
| 25 |
const expr = statement.replace(/nplurals=[1-9]; plural=([^;]*);?$/, '(n) => $1'); |
| 26 |
if (expr === statement) { |
| 27 |
console.error("bad plural forms: " + statement); |
| 28 |
process.exit(1); |
| 29 |
} |
| 30 |
|
| 31 |
return expr; |
| 32 |
} |
| 33 |
|
| 34 |
function parsePo(po_file) { |
| 35 |
// Read the PO file, remove fuzzy/disabled lines to avoid tripping up the validator |
| 36 |
const po_data = fs.readFileSync(po_file, 'utf8') |
| 37 |
.split('\n') |
| 38 |
.filter(line => !line.startsWith('#~')) |
| 39 |
.join('\n'); |
| 40 |
const parsed = gettext_parser.po.parse(po_data, { defaultCharset: 'utf8', validation: true }); |
| 41 |
delete parsed.translations[""][""]; // second header copy |
| 42 |
|
| 43 |
const dir = RTL_LANGS.has(parsed.headers.Language) ? "rtl" : "ltr"; |
| 44 |
return { parsed, dir }; |
| 45 |
} |
| 46 |
|
| 47 |
function buildFile(data, subdir, filename, filter) { |
| 48 |
const parsed = data.parsed; |
| 49 |
const dir = data.dir; |
| 50 |
|
| 51 |
// cockpit.js only looks at "plural-forms" and "language" |
| 52 |
const chunks = [ |
| 53 |
'{\n', |
| 54 |
' "": {\n', |
| 55 |
` "plural-forms": ${get_plural_expr(parsed.headers['Plural-Forms'])},\n`, |
| 56 |
` "language": "${parsed.headers.Language}",\n`, |
| 57 |
` "language-direction": "${dir}"\n`, |
| 58 |
' }' |
| 59 |
]; |
| 60 |
for (const [msgctxt, context] of Object.entries(parsed.translations)) { |
| 61 |
const context_prefix = msgctxt ? msgctxt + '\u0004' : ''; /* for cockpit.ngettext */ |
| 62 |
|
| 63 |
for (const [msgid, translation] of Object.entries(context)) { |
| 64 |
/* Only include msgids which appear in this source directory */ |
| 65 |
const references = translation.comments.reference.split(/\s/); |
| 66 |
if (!references.some(str => str.startsWith(`pkg/${subdir}`) || str.startsWith(config.src_directory) || str.startsWith(`pkg/lib`))) |
| 67 |
continue; |
| 68 |
|
| 69 |
if (translation.comments.flag?.match(/\bfuzzy\b/)) |
| 70 |
continue; |
| 71 |
|
| 72 |
if (!references.some(filter)) |
| 73 |
continue; |
| 74 |
|
| 75 |
const key = JSON.stringify(context_prefix + msgid); |
| 76 |
// cockpit.js always ignores the first item |
| 77 |
chunks.push(`,\n ${key}: [\n null`); |
| 78 |
for (const str of translation.msgstr) { |
| 79 |
chunks.push(',\n ' + JSON.stringify(str)); |
| 80 |
} |
| 81 |
chunks.push('\n ]'); |
| 82 |
} |
| 83 |
} |
| 84 |
chunks.push('\n}'); |
| 85 |
|
| 86 |
const wrapper = config.wrapper?.(subdir) || DEFAULT_WRAPPER; |
| 87 |
const output = wrapper.replace('PO_DATA', chunks.join('')) + '\n'; |
| 88 |
|
| 89 |
const out_path = path.join(subdir ? (subdir + '/') : '', filename); |
| 90 |
fs.writeFileSync(path.resolve(config.outdir, out_path), output); |
| 91 |
} |
| 92 |
|
| 93 |
function init(options) { |
| 94 |
config.srcdir = process.env.SRCDIR || './'; |
| 95 |
config.subdirs = options.subdirs || ['']; |
| 96 |
config.src_directory = options.src_directory || 'src'; |
| 97 |
config.wrapper = options.wrapper; |
| 98 |
config.outdir = options.outdir || './dist'; |
| 99 |
} |
| 100 |
|
| 101 |
function run() { |
| 102 |
const parsedPoFiles = get_po_files().map(po_file => ({ |
| 103 |
data: parsePo(po_file), |
| 104 |
lang: path.basename(po_file).slice(0, -3), |
| 105 |
})); |
| 106 |
|
| 107 |
for (const subdir of config.subdirs) { |
| 108 |
for (const { data, lang } of parsedPoFiles) { |
| 109 |
// Separate translations for the manifest.json file and normal pages |
| 110 |
buildFile(data, subdir, `po.${lang}.js`, str => !str.includes('manifest.json')); |
| 111 |
buildFile(data, subdir, `po.manifest.${lang}.js`, str => str.includes('manifest.json')); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
export const cockpitPoEsbuildPlugin = options => ({ |
| 117 |
name: 'cockpitPoEsbuildPlugin', |
| 118 |
setup(build) { |
| 119 |
init({ ...options, outdir: build.initialOptions.outdir }); |
| 120 |
build.onEnd(result => { result.errors.length === 0 && run() }); |
| 121 |
}, |
| 122 |
}); |