Skip to content

Commit

Permalink
Update translations
Browse files Browse the repository at this point in the history
  • Loading branch information
lslezak committed Nov 21, 2024
1 parent fd94e8d commit 0495fd3
Show file tree
Hide file tree
Showing 20 changed files with 17,240 additions and 31,471 deletions.
25 changes: 21 additions & 4 deletions web/share/po-converter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#! /usr/bin/env node

// Helper script for converting Gettext PO file to Javascript so they can loaded
// by the web frontend.
// Helper script for converting Gettext PO files to Javascript so they can be
// loaded by the web frontend.
//
// Inspired by the Cockpit Webpack plugin
// https://github.com/cockpit-project/cockpit/blob/main/pkg/lib/cockpit-po-plugin.js
//
// Usage:
// cd web/src/po
Expand All @@ -22,6 +25,13 @@ function poFiles() {
return glob.sync(path.resolve(srcdir, "*.po"));
}

// read the supported languages from languages.json file
function supportedLanguages() {
const langs = path.resolve(__dirname, "../src/languages.json");
const data = JSON.parse(fs.readFileSync(langs, "utf8"));
return Object.keys(data).map((l) => l.replace("-", "_"));
}

// extract the plural form function
function pluralForm(statement) {
try {
Expand All @@ -30,7 +40,7 @@ function pluralForm(statement) {
// is insecure for 3rd party files
jed.PF.parse(statement);
} catch (error) {
console.error("Invalid plural form definition", statement)
console.error("Invalid plural form definition", statement);
console.error(error.message);
process.exit(1);
}
Expand Down Expand Up @@ -84,4 +94,11 @@ function buildFile(po_file) {
});
}

Promise.all(poFiles().map((f) => buildFile(f)));
const supported = supportedLanguages();
const files = poFiles().filter((f) => {
const base = path.basename(f, ".po");
// full match or language match
return supported.includes(base) || supported.some((s) => s.split("_")[0] === base);
});

Promise.all(files.map((f) => buildFile(f)));
10 changes: 2 additions & 8 deletions web/src/agama.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ agama.locale = function locale(po) {
agama.gettext = function gettext(str) {
if (translations) {
const translated = translations[str];
// skip the `null` item in the list generated by cockpit-po-plugin
// TODO: get rid of that later
if (translated?.[1]) return translated[1];
if (translated?.[0]) return translated[0];
}

// fallback, return the original text
Expand All @@ -86,11 +84,7 @@ agama.ngettext = function ngettext(str1, strN, n) {
// the plural function either returns direct index (integer) in the plural
// translations or a boolean indicating simple plural form which
// needs to be converted to index 0 (singular) or 1 (plural)
let index = plural_index === true ? 1 : plural_index || 0;

// skip the `null` item in the list generated by cockpit-po-plugin
// TODO: get rid of that later
index += 1;
const index = plural_index === true ? 1 : plural_index || 0;

if (translation[index]) return translation[index];
}
Expand Down
22 changes: 16 additions & 6 deletions web/src/context/installerL10n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,25 @@ function InstallerL10nProvider({ children }: { children?: React.ReactNode }) {
const newLanguage = findSupportedLanguage(candidateLanguages) || "en-US";
const mustReload = storeAgamaLanguage(newLanguage);

// FIXME: split("-")[0] code does not find the "po.pt_BR.js" file, use languages.json to get
// the correct file name (add a new attribute there)
const po = newLanguage.split("-")[0];
// load the translations dynamically, first try language + territory
const po = newLanguage.replace("-", "_");
await import(
/* webpackChunkName: "[request]" */
`../../src/po/po.${po}`
).catch((error) =>
console.error("Cannot load frontend translations for", newLanguage, error),
);
).catch(async () => {
// if it fails then try the language only
const po = newLanguage.split("-")[0];
await import(
/* webpackChunkName: "[request]" */
`../../src/po/po.${po}`
).catch((error) => {
if (newLanguage !== "en-US") {
console.error("Cannot load frontend translations for", newLanguage, error);
}
// reset the current translations (use the original English texts)
agama.locale(null);
});
});

if (mustReload) {
reload(newLanguage);
Expand Down
Loading

0 comments on commit 0495fd3

Please sign in to comment.