feat(i18n): @xenia/i18n translations, language resolution and venue-time formatting
Platform-v2 step 2.4 (APPS_PLATFORM D-11, D-12, §7.2, §8): - i18next 26.7.2 + react-i18next 17.1.13 (exact); en/el locale files (common, dev); createI18n, I18nProvider, useLanguage, setUserLanguage (screen first, then PATCH /api/auth/me through an app-provided save) - resolveLanguage: user -> device -> venue -> en - useFormat(): dates/times always in the venue zone from bootstrap (24 h, DST-safe, business dates never shifted), relative times, money and numbers per language; parseNumber accepts a comma or a dot - scripts/check-i18n.mjs guard: keys missing in en, placeholder mismatches, dates formatted without the venue zone in new code (fails); missing el (warns) - @xenia/ui components read their texts from UiLabelsProvider; useUiLabels() supplies the translated set - dev-only /i18n-sample in the manager; ui.mjs e2e checks it with the browser in New York; unit tests in run-all; lockfile keeps both apps' versions - new pack rule CS-17 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// The guard passes on the current code, and fails on what it's meant to catch.
|
||||
import assert from 'node:assert/strict'
|
||||
import { spawnSync } from 'node:child_process'
|
||||
import { mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { test } from 'node:test'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const PKG = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
||||
const SCRIPT = path.join(PKG, 'scripts', 'check-i18n.mjs')
|
||||
const run = () => spawnSync(process.execPath, [SCRIPT, '--quiet'], { encoding: 'utf8' })
|
||||
|
||||
test('the repo passes the i18n guard', () => {
|
||||
const r = run()
|
||||
assert.equal(r.status, 0, r.stdout + r.stderr)
|
||||
})
|
||||
|
||||
test('it catches unknown keys and dates without the venue zone', () => {
|
||||
const dir = path.join(PKG, 'sample', '__guard_probe__')
|
||||
mkdirSync(dir, { recursive: true })
|
||||
try {
|
||||
writeFileSync(path.join(dir, 'Probe.jsx'), [
|
||||
"import { useTranslation } from '@xenia/i18n'",
|
||||
"export function Probe() { const { t } = useTranslation('dev')",
|
||||
" return t('sample.doesNotExist') + t('sample.title') + t('common:actions.save') + t('actions.cancel')",
|
||||
" + new Date().toLocaleTimeString() + new Intl.DateTimeFormat('el').format(new Date()) }",
|
||||
].join('\n'))
|
||||
const r = run()
|
||||
assert.equal(r.status, 1)
|
||||
assert.match(r.stdout, /key "dev:sample\.doesNotExist" is not in locales\/en/)
|
||||
assert.doesNotMatch(r.stdout, /sample\.title"|actions\.save"|actions\.cancel"/)
|
||||
assert.match(r.stdout, /Probe\.jsx:4: toLocale…String\(\) without timeZone/)
|
||||
assert.match(r.stdout, /Probe\.jsx:4: Intl\.DateTimeFormat without timeZone/)
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,74 @@
|
||||
// Formatting in the venue's zone. The test process may run in any zone: force a far one
|
||||
// so a formatter that used the device zone would fail.
|
||||
process.env.TZ = 'America/New_York'
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
import { createFormatters, toDate } from '../src/format.js'
|
||||
|
||||
const el = createFormatters({ language: 'el', timeZone: 'Europe/Athens', currency: 'EUR' })
|
||||
const en = createFormatters({ language: 'en', timeZone: 'Europe/Athens', currency: 'EUR' })
|
||||
const nbsp = s => s.replace(/[\u00a0\u202f]/g, ' ')
|
||||
|
||||
test('times are venue time, not device time (summer UTC+3, winter UTC+2)', () => {
|
||||
assert.equal(new Date('2026-07-01T10:00:00Z').getHours(), 6, 'the process really runs in New York')
|
||||
assert.equal(el.formatTime('2026-07-01T10:00:00Z'), '13:00')
|
||||
assert.equal(el.formatTime('2026-01-15T10:00:00Z'), '12:00')
|
||||
assert.equal(en.formatTime('2026-07-01T22:30:00Z'), '01:30')
|
||||
})
|
||||
|
||||
test('the venue day, across midnight', () => {
|
||||
assert.equal(el.formatDate('2026-07-01T22:30:00Z'), '02/07/2026')
|
||||
assert.equal(el.venueDateKey('2026-07-01T22:30:00Z'), '2026-07-02')
|
||||
assert.equal(el.venueDateKey('2026-07-01T20:59:00Z'), '2026-07-01')
|
||||
})
|
||||
|
||||
test('DST changes in Athens (§7.3)', () => {
|
||||
// 25 Oct 2026, 04:00 local happens twice: 00:59Z is 03:59 EEST, 01:00Z is 03:00 EET
|
||||
assert.equal(el.formatTime('2026-10-25T00:59:00Z'), '03:59')
|
||||
assert.equal(el.formatTime('2026-10-25T01:00:00Z'), '03:00')
|
||||
// 29 Mar 2026, 03:00 local is skipped: 00:59Z is 02:59 EET, 01:00Z is 04:00 EEST
|
||||
assert.equal(el.formatTime('2026-03-29T00:59:00Z'), '02:59')
|
||||
assert.equal(el.formatTime('2026-03-29T01:00:00Z'), '04:00')
|
||||
})
|
||||
|
||||
test('business dates (YYYY-MM-DD) are never shifted', () => {
|
||||
const ny = createFormatters({ language: 'en', timeZone: 'America/Los_Angeles' })
|
||||
assert.equal(ny.formatDate('2026-11-13'), '13/11/2026')
|
||||
assert.equal(el.formatDate('2026-11-13'), '13/11/2026')
|
||||
assert.match(el.formatDate('2026-11-13', 'dateLong'), /Παρασκευή.*13.*Νοεμβρίου.*2026/)
|
||||
assert.match(en.formatDate('2026-11-13', 'dateLong'), /Friday.*13.*November.*2026/)
|
||||
})
|
||||
|
||||
test('backend strings without an offset are UTC', () => {
|
||||
assert.equal(toDate('2026-07-01 10:00:00.123456').toISOString(), '2026-07-01T10:00:00.123Z')
|
||||
assert.equal(toDate('2026-07-01T10:00:00').toISOString(), '2026-07-01T10:00:00.000Z')
|
||||
assert.equal(toDate('2026-07-01T13:00:00+03:00').toISOString(), '2026-07-01T10:00:00.000Z')
|
||||
assert.equal(toDate('nonsense'), null)
|
||||
assert.equal(el.formatDateTime(null), '')
|
||||
})
|
||||
|
||||
test('date-time styles use a 24-hour clock in both languages', () => {
|
||||
assert.equal(el.formatDateTime('2026-07-01T16:05:00Z'), '01/07/2026, 19:05')
|
||||
assert.equal(en.formatDateTime('2026-07-01T16:05:00Z'), '01/07/2026, 19:05')
|
||||
assert.equal(en.formatTime('2026-07-01T16:05:09Z', 'timeSeconds'), '19:05:09')
|
||||
})
|
||||
|
||||
test('money and numbers per language', () => {
|
||||
assert.equal(nbsp(el.formatMoney(1234.5)), '1.234,50 €')
|
||||
assert.equal(en.formatMoney(1234.5), '€1,234.50')
|
||||
assert.equal(nbsp(el.formatMoney(-3)), '-3,00 €')
|
||||
assert.equal(el.formatMoney(null), '')
|
||||
assert.equal(el.formatNumber(1234.567), '1.234,57')
|
||||
assert.equal(en.formatNumber(2.5, { decimals: 2 }), '2.50')
|
||||
assert.equal(nbsp(el.formatPercent(0.235)), '23,5%')
|
||||
})
|
||||
|
||||
test('relative times', () => {
|
||||
const now = Date.parse('2026-07-01T10:00:00Z')
|
||||
assert.equal(en.formatRelative('2026-07-01T09:55:00Z', now), '5 minutes ago')
|
||||
assert.equal(el.formatRelative('2026-07-01T09:55:00Z', now), 'πριν από 5 λεπτά')
|
||||
assert.equal(en.formatRelative('2026-07-01T09:59:40Z', now), 'now')
|
||||
assert.equal(en.formatRelative('2026-06-30T10:00:00Z', now), 'yesterday')
|
||||
assert.equal(en.formatRelative('2026-07-01T12:00:00Z', now), 'in 2 hours')
|
||||
assert.equal(en.formatRelative('2026-04-01T10:00:00Z', now), '01/04/2026')
|
||||
})
|
||||
@@ -0,0 +1,30 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
import { setUserLanguage } from '../src/language.js'
|
||||
|
||||
const fakeI18n = () => ({ language: 'en', async changeLanguage(l) { this.language = l } })
|
||||
|
||||
test('switches the screen, then saves to the profile', async () => {
|
||||
const i18n = fakeI18n(); const saved = []
|
||||
assert.equal(await setUserLanguage(i18n, 'el', { save: async l => saved.push(l) }), 'el')
|
||||
assert.deepEqual(saved, ['el'])
|
||||
})
|
||||
|
||||
test('null = inherit again: shows the fallback, saves ""', async () => {
|
||||
const i18n = fakeI18n(); const saved = []
|
||||
await setUserLanguage(i18n, null, { fallback: 'el', save: async l => saved.push(l) })
|
||||
assert.equal(i18n.language, 'el')
|
||||
assert.deepEqual(saved, [''])
|
||||
})
|
||||
|
||||
test('a failed save keeps the new language on screen and rethrows', async () => {
|
||||
const i18n = fakeI18n()
|
||||
await assert.rejects(setUserLanguage(i18n, 'el', { save: async () => { throw new Error('offline') } }), /offline/)
|
||||
assert.equal(i18n.language, 'el')
|
||||
})
|
||||
|
||||
test('unsupported languages are refused before anything changes', async () => {
|
||||
const i18n = fakeI18n()
|
||||
await assert.rejects(setUserLanguage(i18n, 'fr'), /unsupported/)
|
||||
assert.equal(i18n.language, 'en')
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
import { intlLocale, normalizeLanguage, resolveLanguage } from '../src/locale.js'
|
||||
|
||||
test('resolution order: user → device → venue → en (D-12)', () => {
|
||||
assert.equal(resolveLanguage({ user: 'el', device: 'en', venue: 'en' }), 'el')
|
||||
assert.equal(resolveLanguage({ user: null, device: 'el', venue: 'en' }), 'el')
|
||||
assert.equal(resolveLanguage({ user: '', device: undefined, venue: 'el' }), 'el')
|
||||
assert.equal(resolveLanguage({}), 'en')
|
||||
assert.equal(resolveLanguage(), 'en')
|
||||
})
|
||||
|
||||
test('unsupported or odd values are skipped', () => {
|
||||
assert.equal(resolveLanguage({ user: 'fr', venue: 'el' }), 'el')
|
||||
assert.equal(resolveLanguage({ user: 'el-GR' }), 'el')
|
||||
assert.equal(normalizeLanguage(' EN '), 'en')
|
||||
assert.equal(normalizeLanguage(42), null)
|
||||
})
|
||||
|
||||
test('Intl locales', () => {
|
||||
assert.equal(intlLocale('el'), 'el-GR')
|
||||
assert.equal(intlLocale('en'), 'en-GB')
|
||||
assert.equal(intlLocale('fr'), 'en-GB')
|
||||
})
|
||||
@@ -0,0 +1,38 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
import { parseNumber } from '../src/number.js'
|
||||
|
||||
test('comma or dot as the decimal separator', () => {
|
||||
for (const [input, want] of [
|
||||
['3,50', 3.5], ['3.50', 3.5], [',5', 0.5], ['.5', 0.5], ['3', 3], ['3,', 3], ['0,10', 0.1],
|
||||
['12', 12], ['2,500', 2.5], ['2.500', 2.5],
|
||||
]) assert.equal(parseNumber(input), want, input)
|
||||
})
|
||||
|
||||
test('thousands separators', () => {
|
||||
for (const [input, want] of [
|
||||
['1.234,56', 1234.56], ['1,234.56', 1234.56], ['1.234.567', 1234567], ['1,234,567', 1234567],
|
||||
['12.345,6', 12345.6], ['1.234.567,89', 1234567.89],
|
||||
]) assert.equal(parseNumber(input), want, input)
|
||||
})
|
||||
|
||||
test('signs, spaces, euro, percent', () => {
|
||||
for (const [input, want] of [
|
||||
[' -2,5 ', -2.5], ['+4', 4], ['€ 12,00', 12], ['12,00 €', 12], ['1\u00a0234,5', 1234.5], ['15%', 15],
|
||||
]) assert.equal(parseNumber(input), want, input)
|
||||
assert.equal(parseNumber('-3', { allowNegative: false }), null)
|
||||
})
|
||||
|
||||
test('not numbers', () => {
|
||||
for (const input of ['', ' ', 'abc', '1,2,3', '12.34.5', '1.23,4.5', '.', ',', '-', '3,5x', '1e5', null, undefined, {}]) {
|
||||
assert.equal(parseNumber(input), null, String(input))
|
||||
}
|
||||
})
|
||||
|
||||
test('numbers pass through; decimals rounds', () => {
|
||||
assert.equal(parseNumber(4.25), 4.25)
|
||||
assert.equal(parseNumber(Number.NaN), null)
|
||||
assert.equal(parseNumber('2,345', { decimals: 2 }), 2.35)
|
||||
assert.equal(parseNumber('1,005', { decimals: 2 }), 1.01)
|
||||
assert.equal(parseNumber('-1,005', { decimals: 2 }), -1.01)
|
||||
})
|
||||
Reference in New Issue
Block a user