feat: updates
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2024-12-26 14:27:18 +01:00
parent 188840ccaa
commit 023492652f
1 changed files with 39 additions and 27 deletions

View File

@ -3,8 +3,9 @@ import fs from 'fs';
import path from 'path';
import { glob } from 'glob';
import yaml from 'js-yaml';
import assert from 'assert';
let locales = {};
let i18n = {};
async function init() {
const files = await glob(['src/i18n/locale/**.yml', 'src/pages/**/locale/*.yml']);
@ -14,9 +15,15 @@ async function init() {
return acc;
}, {});
}
function validateKeys(keys, translations) {
const missingKeys = [];
function validateKeys(keys, locale) {
const missingKeys = validateLocale(keys, locales[locale]);
const missingKeys2 = validateLocale(missingKeys, i18n[locale]);
return missingKeys2;
}
function validateLocale(keys, translations) {
const missingKeys = [];
if (translations === undefined) return missingKeys;
keys.forEach((key) => {
const parts = key.split('.');
let current = translations;
@ -43,15 +50,23 @@ describe('🔍 Translation Keys Validation', async () => {
'src/components/**/*.vue',
]);
const regex = /(?:=\s*)\$t\(['"`]([\w.]+)['"`]\)|(?:=\s*)t\(['"`]([\w.]+)['"`]\)/g;
const regex = /="\$t\(['"`]([\w.]+)['"`]\)|="t\(['"`]([\w.]+)['"`]\)/g;
vueFiles.forEach((file) => {
vueFiles.forEach(async (file) => {
const keys = new Set();
const content = fs.readFileSync(file, 'utf8');
let match;
while ((match = regex.exec(content)) !== null) {
keys.add(match[1] || match[2]);
}
const parts = file.split(path.sep);
const cardIndex = parts.indexOf('Card');
let previousElement = '';
if (cardIndex > -1) {
previousElement = parts[cardIndex - 1];
previousElement =
previousElement.charAt(0).toLowerCase() + previousElement.slice(1);
}
const i18nMatch = content.match(/<i18n[^>]*>([\s\S]*?)<\/i18n>/);
if (i18nMatch) {
@ -66,33 +81,30 @@ describe('🔍 Translation Keys Validation', async () => {
i18nContent[lang] = i18nContent[current[0]];
});
}
Object.entries(i18nContent).forEach(([local, value]) => {
Object.entries(value).forEach(([k, v]) => (locales[local][k] = v));
});
i18n = i18nContent;
// Object.entries(i18nContent).forEach(([local, value]) => {
// Object.entries(value).forEach(([k, v]) => (i18n[local] = { [k]: v }));
// });
} catch (err) {
console.warn(`⚠️ Error parsing <i18n> block in ${file}:`, err.message);
}
}
Object.entries(locales).forEach(([locale, translations]) => {
['es'].forEach((locale) => {
let missingKeys = validateKeys(keys, locale);
if (missingKeys.length > 0) {
console.log(`Elemento anterior a 'Card': ${previousElement}`);
const updatedKeys = new Set();
missingKeys.forEach((key) => {
if (!key.startsWith(`${previousElement}.`))
updatedKeys.add(`${previousElement}.${key}`);
});
missingKeys = validateKeys(updatedKeys, locale);
}
assert(
missingKeys.length === 0,
`Missing keys in ${locale}.${file}:\n${missingKeys.join('\n')}`
);
it(`should have all translation keys in ${locale}.${file}`, () => {
let missingKeys = validateKeys(keys, translations);
if (missingKeys.length > 0) {
const parts = file.split(path.sep);
const cardIndex = parts.indexOf('Card');
if (cardIndex > 0) {
let previousElement = parts[cardIndex - 1];
previousElement =
previousElement.charAt(0).toLowerCase() +
previousElement.slice(1);
console.log(`Elemento anterior a 'Card': ${previousElement}`);
const updatedKeys = new Set();
keys.forEach((key) => {
updatedKeys.add(`${previousElement}.${key}`);
});
missingKeys = validateKeys(updatedKeys, translations);
}
}
expect(missingKeys, `Missing keys in ${file}`).toHaveLength(0);
});
});