2024-04-24 23:56:41 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import fs from 'fs';
|
2025-07-03 22:15:14 +02:00
|
|
|
import { execSync } from 'child_process';
|
2025-02-17 02:29:50 +01:00
|
|
|
import {
|
|
|
|
|
fetchWeblateLanguageStats,
|
|
|
|
|
getWeblateLanguageStatsFor,
|
|
|
|
|
meetsTranslatedPercentThreshold as meetsTranslatedPercentThresholdBase,
|
2025-02-17 02:45:02 +01:00
|
|
|
} from './Weblate.utils.ts';
|
|
|
|
|
import { WeblateLanguageStatistic } from './Weblate.types.ts';
|
|
|
|
|
import { TRANSLATED_PERCENT_THRESHOLD } from './Weblate.constants.ts';
|
2024-04-24 23:56:41 +02:00
|
|
|
|
2025-07-03 22:15:14 +02:00
|
|
|
const FILE_PATH = 'src/i18n/index.ts';
|
|
|
|
|
|
|
|
|
|
const outputFilePath = path.join(import.meta.dirname, `../../../${FILE_PATH}`);
|
2025-02-17 02:45:02 +01:00
|
|
|
const resourcesDirPath = path.join(import.meta.dirname, '../../../public/locales');
|
2024-04-24 23:56:41 +02:00
|
|
|
|
2025-02-17 02:29:50 +01:00
|
|
|
const extractLanguageCode = (resourceFileNames: string): string =>
|
|
|
|
|
path.basename(resourceFileNames, path.extname(resourceFileNames));
|
2024-04-24 23:56:41 +02:00
|
|
|
|
2025-02-17 02:29:50 +01:00
|
|
|
const meetsTranslatedPercentThreshold = (
|
|
|
|
|
resourceFileName: string,
|
|
|
|
|
weblateLanguageStats: WeblateLanguageStatistic[],
|
|
|
|
|
): boolean => {
|
|
|
|
|
const code = extractLanguageCode(resourceFileName);
|
|
|
|
|
const { name, translated_percent: translatedPercent } = getWeblateLanguageStatsFor(code, weblateLanguageStats);
|
2024-04-24 23:56:41 +02:00
|
|
|
|
2025-02-17 02:29:50 +01:00
|
|
|
const meetsThreshold = meetsTranslatedPercentThresholdBase(translatedPercent);
|
2024-04-24 23:56:41 +02:00
|
|
|
|
2025-02-17 02:29:50 +01:00
|
|
|
if (!meetsThreshold) {
|
|
|
|
|
console.log(
|
|
|
|
|
`Language "${name} (${code})" does not meet the required translation percentage threshold (${translatedPercent} < ${TRANSLATED_PERCENT_THRESHOLD})`,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-04-24 23:56:41 +02:00
|
|
|
|
2025-02-17 02:29:50 +01:00
|
|
|
return meetsThreshold;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const generateResources = async () => {
|
|
|
|
|
const weblateLanguageStats = await fetchWeblateLanguageStats();
|
|
|
|
|
|
|
|
|
|
const resourceFileNames = fs.readdirSync(resourcesDirPath);
|
|
|
|
|
|
|
|
|
|
const resourceNames = resourceFileNames
|
|
|
|
|
.filter((resourceFileName) => meetsTranslatedPercentThreshold(resourceFileName, weblateLanguageStats))
|
|
|
|
|
.map(extractLanguageCode);
|
|
|
|
|
|
|
|
|
|
const outputFileContent = fs.readFileSync(outputFilePath, 'utf-8');
|
|
|
|
|
|
|
|
|
|
const updatedFileContent = outputFileContent.replace(
|
|
|
|
|
/(export const i18nResources = \[)[\s\S]*?(])/g,
|
|
|
|
|
`$1'${resourceNames.join("', '")}'$2`,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(outputFilePath, updatedFileContent);
|
2025-07-03 22:15:14 +02:00
|
|
|
|
|
|
|
|
execSync('yarn tsc', { stdio: 'inherit' });
|
|
|
|
|
|
|
|
|
|
execSync(`yarn eslint --fix ${FILE_PATH}`, { stdio: 'inherit' });
|
|
|
|
|
|
|
|
|
|
const hasFileChanged = execSync('git status --porcelain').toString().includes(FILE_PATH);
|
|
|
|
|
if (!hasFileChanged) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execSync('git reset');
|
|
|
|
|
execSync(`git add ${FILE_PATH}`);
|
|
|
|
|
execSync('git commit -m "Update available languages"');
|
2025-02-17 02:29:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
generateResources();
|