From 89efddbecd103043242a1af35599a43605688707 Mon Sep 17 00:00:00 2001 From: Sam Arbid Date: Tue, 1 Oct 2024 20:30:38 +0200 Subject: [PATCH] i18n: Improve handling of file existence check in compileCatalog * 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 --- .../scripts/compileCatalog.js | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/invenio_rdm_records/assets/semantic-ui/translations/invenio_rdm_records/scripts/compileCatalog.js b/invenio_rdm_records/assets/semantic-ui/translations/invenio_rdm_records/scripts/compileCatalog.js index 1da04f208..e70b856fe 100644 --- a/invenio_rdm_records/assets/semantic-ui/translations/invenio_rdm_records/scripts/compileCatalog.js +++ b/invenio_rdm_records/assets/semantic-ui/translations/invenio_rdm_records/scripts/compileCatalog.js @@ -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 = "./"; @@ -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) { @@ -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}`); + } } }