Require languages to be at least 75% translated
This commit is contained in:
@@ -8,21 +8,55 @@
|
||||
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import {
|
||||
fetchWeblateLanguageStats,
|
||||
getWeblateLanguageStatsFor,
|
||||
meetsTranslatedPercentThreshold as meetsTranslatedPercentThresholdBase,
|
||||
} from './weblate/Weblate.utils.ts';
|
||||
import { WeblateLanguageStatistic } from './weblate/Weblate.types.ts';
|
||||
import { TRANSLATED_PERCENT_THRESHOLD } from './weblate/Weblate.constants.ts';
|
||||
|
||||
const outputFilePath = path.join(import.meta.dirname, '../../src/i18n/index.ts');
|
||||
const resourcesDirPath = path.join(import.meta.dirname, '../../public/locales');
|
||||
|
||||
const resourceFileNames = fs.readdirSync(resourcesDirPath);
|
||||
const extractLanguageCode = (resourceFileNames: string): string =>
|
||||
path.basename(resourceFileNames, path.extname(resourceFileNames));
|
||||
|
||||
const resourceNames = resourceFileNames.map((resourceFileName) =>
|
||||
path.basename(resourceFileName, path.extname(resourceFileName)),
|
||||
);
|
||||
const meetsTranslatedPercentThreshold = (
|
||||
resourceFileName: string,
|
||||
weblateLanguageStats: WeblateLanguageStatistic[],
|
||||
): boolean => {
|
||||
const code = extractLanguageCode(resourceFileName);
|
||||
const { name, translated_percent: translatedPercent } = getWeblateLanguageStatsFor(code, weblateLanguageStats);
|
||||
|
||||
const outputFileContent = fs.readFileSync(outputFilePath, 'utf-8');
|
||||
const meetsThreshold = meetsTranslatedPercentThresholdBase(translatedPercent);
|
||||
|
||||
const updatedFileContent = outputFileContent.replace(
|
||||
/(export const i18nResources = \[)[\s\S]*?(])/g,
|
||||
`$1'${resourceNames.join("', '")}'$2`,
|
||||
);
|
||||
if (!meetsThreshold) {
|
||||
console.log(
|
||||
`Language "${name} (${code})" does not meet the required translation percentage threshold (${translatedPercent} < ${TRANSLATED_PERCENT_THRESHOLD})`,
|
||||
);
|
||||
}
|
||||
|
||||
fs.writeFileSync(outputFilePath, updatedFileContent);
|
||||
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);
|
||||
};
|
||||
|
||||
generateResources();
|
||||
|
||||
Reference in New Issue
Block a user