From f50dfa7358d18a727bf0f330b51d23992d0987d4 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 26 Dec 2024 12:23:15 +0100 Subject: [PATCH] feat: add module/locale --- src/utils/validate-translations.spec.js | 110 +++++------------------- 1 file changed, 20 insertions(+), 90 deletions(-) diff --git a/src/utils/validate-translations.spec.js b/src/utils/validate-translations.spec.js index b4c2848d7..f69a3a812 100644 --- a/src/utils/validate-translations.spec.js +++ b/src/utils/validate-translations.spec.js @@ -4,71 +4,17 @@ import path from 'path'; import { glob } from 'glob'; import yaml from 'js-yaml'; -// ๐Ÿ“Œ Cargar traducciones YAML const localesPath = path.resolve(__dirname + '../../i18n/locale'); -const locales = fs.readdirSync(localesPath).reduce((acc, file) => { - const locale = path.basename(file, '.yml'); - acc[locale] = yaml.load(fs.readFileSync(path.join(localesPath, file), 'utf8')); - return acc; -}, {}); +let locales = {}; +async function init() { + const files = await glob(['src/i18n/locale/**.yml', 'src/pages/**/locale/*.yml']); -// ๐Ÿ“Œ Extraer todas las claves de traducciรณn de los archivos .vue -async function extractTranslationKeysFromUsage() { - const vueFiles = await glob('src/**/*.vue'); - const regex = /\$t\(['"`]([\w.]+)['"`]\)|t\(['"`]([\w.]+)['"`]\)/g; - - const keys = new Set(); - - vueFiles.forEach((file) => { - const content = fs.readFileSync(file, 'utf8'); - let match; - while ((match = regex.exec(content)) !== null) { - keys.add(match[1] || match[2]); - } - }); - - return Array.from(keys); + locales = files.reduce((acc, file) => { + const locale = path.basename(file, '.yml'); + acc[locale] = { ...acc[locale], ...yaml.load(fs.readFileSync(file, 'utf8')) }; + return acc; + }, {}); } - -// ๐Ÿ“Œ Extraer claves de la etiqueta `` en los componentes Vue -async function extractTranslationKeysFromI18nTag() { - const vueFiles = await glob('src/**/*.vue'); - const keys = new Set(); - - for (const file of vueFiles) { - const content = fs.readFileSync(file, 'utf8'); - const i18nMatch = content.match(/]*>([\s\S]*?)<\/i18n>/); - - if (i18nMatch) { - try { - const i18nContent = yaml.load(i18nMatch[1]); - extractKeysFromObject(i18nContent).forEach((key) => keys.add(key)); - } catch (err) { - console.warn(`โš ๏ธ Error parsing block in ${file}:`, err.message); - } - } - } - - return Array.from(keys); -} - -// ๐Ÿ“Œ Extraer claves y valores de un objeto anidado -function extractKeysFromObject(obj, prefix = '') { - let keys = []; - - for (const key in obj) { - if (typeof obj[key] === 'object' && obj[key] !== null) { - keys = keys.concat(extractKeysFromObject(obj[key], `${prefix}${key}.`)); - } else { - // Aquรญ estamos recogiendo tanto la clave como su valor - keys.push({ [`${prefix}${key}`]: obj[key] }); - } - } - - return keys; -} - -// ๐Ÿ“Œ Validar claves extraรญdas contra las traducciones disponibles function validateKeys(keys, translations) { const missingKeys = []; @@ -89,11 +35,13 @@ function validateKeys(keys, translations) { return missingKeys; } -// ๐Ÿ“Œ Ejecutar las validaciones describe('๐Ÿ” Translation Keys Validation', async () => { + await init(); + const vueFiles = await glob([ 'src/pages/*.vue', - 'src/components/CreateNewCityForm.vue', + 'src/pages/**/*.vue', + 'src/components/**/*.vue', ]); const regex = /\$t\(['"`]([\w.]+)['"`]\)|t\(['"`]([\w.]+)['"`]\)/g; @@ -122,7 +70,6 @@ describe('๐Ÿ” Translation Keys Validation', async () => { Object.entries(i18nContent).forEach(([local, value]) => { Object.entries(value).forEach(([k, v]) => (locales[local][k] = v)); }); - // extractKeysFromObject(i18nContent).forEach((key) => keys.add(key)); } catch (err) { console.warn(`โš ๏ธ Error parsing block in ${file}:`, err.message); } @@ -130,33 +77,16 @@ describe('๐Ÿ” Translation Keys Validation', async () => { Object.entries(locales).forEach(([locale, translations]) => { it(`should have all translation keys in ${locale}.${file}`, () => { const missingKeys = validateKeys(keys, translations); - - // if (missingKeys.length > 0) { - // console.error(`โŒ Missing keys in ${file}:`, missingKeys); + // try { + console.info( + `xMissing keys in ${file}:${missingKeys.join(', ')}`, + missingKeys + ); + expect(missingKeys, `xxMissing keys in ${file}`).toHaveLength(0); + // } catch (error) { + // console.error(`xxxMissing keys in ${file}${missingKeys.join(', ')}`); // } - - expect(missingKeys, `Missing keys in ${file}`).toHaveLength(0); }); }); }); - - /* - const [usageKeys, i18nTagKeys] = await Promise.all([ - extractTranslationKeysFromUsage(), - // extractTranslationKeysFromI18nTag(), - ]); - - const allKeys = [...new Set([...usageKeys, ...i18nTagKeys])]; - - Object.entries(locales).forEach(([locale, translations]) => { - it(`should have all translation keys in ${locale}`, () => { - const missingKeys = validateKeys(allKeys, translations); - - if (missingKeys.length > 0) { - console.error(`โŒ Missing keys in ${locale}:`, missingKeys); - } - - expect(missingKeys).toEqual([]); - }); - });*/ });