feat: soporte multi idioma en la web (infraestructura)

i18next + react-i18next con es-AR (fuente), es-419, pt-BR y en; el idioma
sale del dispositivo (fallback a inglés) y se puede cambiar en Ajustes.
La elección se guarda en localStorage y, para el profe, en users.locale.
Las validaciones de shared pasan a claves y los errores de la API se
traducen por code (con params para límites de plan y cupo).
Migrados: layout, Ajustes, auth, PWA y lib/format.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jose Selesan
2026-11-03 12:14:59 -03:00
parent ff885bddfc
commit cb872eb8bc
102 changed files with 2332 additions and 290 deletions

View File

@@ -0,0 +1,69 @@
import { describe, expect, test } from 'bun:test'
import { readdirSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { SUPPORTED_LOCALES } from '@gruperly/shared'
import { NAMESPACES } from '../src/i18n/instance'
/* es-AR es la fuente. Cada idioma tiene que tener exactamente las mismas claves
(salvo las formas de plural, que dependen del idioma), sin textos vacíos y
con las mismas variables de interpolación. */
const root = join(import.meta.dir, '../src/i18n/locales')
const SOURCE = 'es-AR'
const PLURAL_SUFFIX = /_(zero|one|two|few|many|other)$/
type Tree = { [key: string]: string | Tree }
function load(locale: string, ns: string): Tree {
return JSON.parse(readFileSync(join(root, locale, `${ns}.json`), 'utf8')) as Tree
}
function flatten(tree: Tree, prefix = ''): Map<string, string> {
const out = new Map<string, string>()
for (const [key, value] of Object.entries(tree)) {
const path = prefix ? `${prefix}.${key}` : key
if (typeof value === 'string') out.set(path, value)
else for (const [k, v] of flatten(value, path)) out.set(k, v)
}
return out
}
/** Claves sin el sufijo de plural: `a_one` y `a_other` cuentan como `a`. */
function baseKeys(flat: Map<string, string>): Set<string> {
return new Set([...flat.keys()].map((key) => key.replace(PLURAL_SUFFIX, '')))
}
function variables(text: string): string[] {
return [...text.matchAll(/\{\{\s*(\w+)\s*\}\}/g)].map((m) => m[1]!).sort()
}
function variablesByBase(flat: Map<string, string>): Map<string, string> {
const out = new Map<string, Set<string>>()
for (const [key, text] of flat) {
const base = key.replace(PLURAL_SUFFIX, '')
const set = out.get(base) ?? new Set<string>()
for (const v of variables(text)) set.add(v)
out.set(base, set)
}
return new Map([...out].map(([k, v]) => [k, [...v].sort().join(',')]))
}
describe('traducciones', () => {
test('cada namespace declarado tiene su JSON en todos los idiomas', () => {
for (const locale of SUPPORTED_LOCALES) {
const files = readdirSync(join(root, locale)).filter((f) => f.endsWith('.json'))
expect(files.map((f) => f.slice(0, -5)).sort()).toEqual([...NAMESPACES].sort())
}
})
for (const ns of NAMESPACES) {
const source = flatten(load(SOURCE, ns))
for (const locale of SUPPORTED_LOCALES.filter((l) => l !== SOURCE)) {
test(`${locale}/${ns} tiene las mismas claves y variables que ${SOURCE}`, () => {
const target = flatten(load(locale, ns))
expect([...baseKeys(target)].sort()).toEqual([...baseKeys(source)].sort())
expect(Object.fromEntries(variablesByBase(target))).toEqual(Object.fromEntries(variablesByBase(source)))
for (const [key, text] of target) expect({ key, empty: text.trim() === '' }).toEqual({ key, empty: false })
})
}
}
})