Skip to content

Commit

Permalink
i18n: Improve handling of file existence check in compileCatalog
Browse files Browse the repository at this point in the history
*  Add a check for the existence of the file before attempting to read it
*  Log an error message if the file does not exist
* Improve readability by extracting the file existence check into a function
  • Loading branch information
Samk13 committed Oct 1, 2024
1 parent a4fbec8 commit 89efddb
Showing 1 changed file with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Invenio RDM is free software; you can redistribute it and/or modify it
// under the terms of the MIT License; see LICENSE file for more details.

const { readFileSync, writeFileSync } = require("fs");
const { readFileSync, writeFileSync, existsSync } = require("fs");
const { gettextToI18next } = require("i18next-conv");

const PACKAGE_JSON_BASE_PATH = "./";
Expand All @@ -13,7 +13,7 @@ const { languages } = require(`../package`).config;
// it accepts the same options as the cli.
// https://github.com/i18next/i18next-gettext-converter#options
const options = {
/* you options here */
/* your options here */
};

function save(target) {
Expand All @@ -24,17 +24,31 @@ function save(target) {

if ("lang" === process.argv[2]) {
const lang = process.argv[3];
gettextToI18next(
lang,
readFileSync(`${PACKAGE_JSON_BASE_PATH}messages/${lang}/messages.po`),
options
).then(save(`${PACKAGE_JSON_BASE_PATH}messages/${lang}/translations.json`));
const inputFilePath = `${PACKAGE_JSON_BASE_PATH}messages/${lang}/messages.po`;
const outputFilePath = `${PACKAGE_JSON_BASE_PATH}messages/${lang}/translations.json`;

if (existsSync(inputFilePath)) {
gettextToI18next(lang, readFileSync(inputFilePath), options)
.then(save(outputFilePath))
.catch((err) => {
console.error(`Error converting ${inputFilePath}:`, err);
});
} else {
console.error(`File not found: ${inputFilePath} for language ${lang}`);
}
} else {
for (const lang of languages) {
gettextToI18next(
lang,
readFileSync(`${PACKAGE_JSON_BASE_PATH}messages/${lang}/messages.po`),
options
).then(save(`${PACKAGE_JSON_BASE_PATH}messages/${lang}/translations.json`));
const inputFilePath = `${PACKAGE_JSON_BASE_PATH}messages/${lang}/messages.po`;
const outputFilePath = `${PACKAGE_JSON_BASE_PATH}messages/${lang}/translations.json`;

if (existsSync(inputFilePath)) {
gettextToI18next(lang, readFileSync(inputFilePath), options)
.then(save(outputFilePath))
.catch((err) => {
console.error(`Error converting ${inputFilePath}:`, err);
});
} else {
console.error(`File not found: ${inputFilePath} for language ${lang}`);
}
}
}

0 comments on commit 89efddb

Please sign in to comment.