Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n: Improve handling of file existence check in compileCatalog #1833

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// This file is part of InvenioRdmRecords
// Copyright (C) 2022 CERN.
// Copyright (C) 2024 KTH Royal Institute of Technology.
//
// 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 +14,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 +25,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}`);
}
}
}