- Fixed detectLanguage() to properly parse Accept-Language q-values (sort by q descending before matching, was returning first-in-list) - Updated 5 stale test assertions that assumed only 5 languages existed (zh and ja are now supported after the DC-077 31-language expansion) - All 1770 tests pass
101 lines
3.4 KiB
JavaScript
101 lines
3.4 KiB
JavaScript
/**
|
|
* DC-077: Tests for the i18n system
|
|
*/
|
|
const i18n = require('../src/utilities/i18n');
|
|
|
|
describe('DC-077: i18n system', () => {
|
|
describe('t() translation function', () => {
|
|
it('translates keys in English by default', () => {
|
|
expect(i18n.t('dashboard.title')).toBe('Dashboard');
|
|
expect(i18n.t('action.start')).toBe('Start');
|
|
});
|
|
|
|
it('translates keys in Spanish', () => {
|
|
expect(i18n.t('dashboard.title', 'es')).toBe('Panel de control');
|
|
expect(i18n.t('action.start', 'es')).toBe('Iniciar');
|
|
});
|
|
|
|
it('translates keys in French', () => {
|
|
expect(i18n.t('dashboard.title', 'fr')).toBe('Tableau de bord');
|
|
expect(i18n.t('action.stop', 'fr')).toBe('Arrêter');
|
|
});
|
|
|
|
it('translates keys in German', () => {
|
|
expect(i18n.t('dashboard.title', 'de')).toBe('Dashboard');
|
|
expect(i18n.t('action.delete', 'de')).toBe('Löschen');
|
|
});
|
|
|
|
it('translates keys in Arabic', () => {
|
|
expect(i18n.t('dashboard.title', 'ar')).toBe('لوحة التحكم');
|
|
expect(i18n.t('action.start', 'ar')).toBe('تشغيل');
|
|
});
|
|
|
|
it('falls back to English for unsupported language', () => {
|
|
expect(i18n.t('dashboard.title', 'klingon')).toBe('Dashboard');
|
|
});
|
|
|
|
it('falls back to key if not found in any language', () => {
|
|
expect(i18n.t('nonexistent.key.xyz')).toBe('nonexistent.key.xyz');
|
|
});
|
|
});
|
|
|
|
describe('getSupportedLanguages()', () => {
|
|
it('returns array of language codes', () => {
|
|
const langs = i18n.getSupportedLanguages();
|
|
expect(langs).toContain('en');
|
|
expect(langs).toContain('es');
|
|
expect(langs).toContain('fr');
|
|
expect(langs).toContain('de');
|
|
expect(langs).toContain('ar');
|
|
expect(langs.length).toBeGreaterThanOrEqual(5);
|
|
});
|
|
});
|
|
|
|
describe('isSupported()', () => {
|
|
it('returns true for supported languages', () => {
|
|
expect(i18n.isSupported('en')).toBe(true);
|
|
expect(i18n.isSupported('fr')).toBe(true);
|
|
});
|
|
|
|
it('returns false for unsupported languages', () => {
|
|
expect(i18n.isSupported('klingon')).toBe(false);
|
|
expect(i18n.isSupported('xx')).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('detectLanguage()', () => {
|
|
it('detects from Accept-Language header', () => {
|
|
expect(i18n.detectLanguage('es-ES,es;q=0.9,en;q=0.8')).toBe('es');
|
|
expect(i18n.detectLanguage('fr-FR,fr;q=0.9')).toBe('fr');
|
|
expect(i18n.detectLanguage('de-DE,de;q=0.9,en;q=0.8')).toBe('de');
|
|
});
|
|
|
|
it('handles quality values correctly', () => {
|
|
expect(i18n.detectLanguage('en;q=0.9,fr;q=1.0')).toBe('fr');
|
|
});
|
|
|
|
it('defaults to English for no header', () => {
|
|
expect(i18n.detectLanguage(null)).toBe('en');
|
|
expect(i18n.detectLanguage(undefined)).toBe('en');
|
|
expect(i18n.detectLanguage('')).toBe('en');
|
|
});
|
|
|
|
it('defaults to English for unsupported languages', () => {
|
|
expect(i18n.detectLanguage('klingon-KR,klingon;q=0.9')).toBe('en');
|
|
expect(i18n.detectLanguage('xx-XX,xx;q=0.9')).toBe('en');
|
|
});
|
|
|
|
it('strips region codes before matching', () => {
|
|
expect(i18n.detectLanguage('en-US,en;q=0.9')).toBe('en');
|
|
expect(i18n.detectLanguage('de-AT,de;q=0.9')).toBe('de');
|
|
});
|
|
});
|
|
|
|
describe('RTL support', () => {
|
|
it('Arabic is in supported languages', () => {
|
|
expect(i18n.isSupported('ar')).toBe(true);
|
|
expect(i18n.t('dashboard.title', 'ar')).toBeTruthy();
|
|
});
|
|
});
|
|
});
|