[grade=pending] fix: i18n detectLanguage q-value parsing + update tests for 31-language expansion
- 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
This commit is contained in:
@@ -31,7 +31,7 @@ describe('DC-077: i18n system', () => {
|
||||
});
|
||||
|
||||
it('falls back to English for unsupported language', () => {
|
||||
expect(i18n.t('dashboard.title', 'zh')).toBe('Dashboard');
|
||||
expect(i18n.t('dashboard.title', 'klingon')).toBe('Dashboard');
|
||||
});
|
||||
|
||||
it('falls back to key if not found in any language', () => {
|
||||
@@ -58,8 +58,8 @@ describe('DC-077: i18n system', () => {
|
||||
});
|
||||
|
||||
it('returns false for unsupported languages', () => {
|
||||
expect(i18n.isSupported('zh')).toBe(false);
|
||||
expect(i18n.isSupported('ja')).toBe(false);
|
||||
expect(i18n.isSupported('klingon')).toBe(false);
|
||||
expect(i18n.isSupported('xx')).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,8 +81,8 @@ describe('DC-077: i18n system', () => {
|
||||
});
|
||||
|
||||
it('defaults to English for unsupported languages', () => {
|
||||
expect(i18n.detectLanguage('zh-CN,zh;q=0.9')).toBe('en');
|
||||
expect(i18n.detectLanguage('ja-JP,ja;q=0.9')).toBe('en');
|
||||
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', () => {
|
||||
|
||||
@@ -14,13 +14,13 @@ function createI18nApp() {
|
||||
}
|
||||
|
||||
describe('DC-077: i18n Routes', () => {
|
||||
it('GET /i18n/languages returns 5 languages', async () => {
|
||||
it('GET /i18n/languages returns supported languages', async () => {
|
||||
const app = createI18nApp();
|
||||
const res = await request(app).get('/api/v1/i18n/languages');
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.success).toBe(true);
|
||||
expect(res.body.languages).toHaveLength(5);
|
||||
expect(res.body.languages.length).toBeGreaterThanOrEqual(5);
|
||||
expect(res.body.default).toBe('en');
|
||||
});
|
||||
|
||||
|
||||
@@ -529,9 +529,22 @@ function isSupported(lang) { return SUPPORTED_LANGUAGES.indexOf(lang) >= 0; }
|
||||
function detectLanguage(acceptLanguage) {
|
||||
if (!acceptLanguage) return DEFAULT_LANGUAGE;
|
||||
var parts = acceptLanguage.split(',');
|
||||
var parsed = [];
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
var code = parts[i].trim().split(';')[0].split('-')[0].toLowerCase();
|
||||
if (isSupported(code)) return code;
|
||||
var raw = parts[i].trim();
|
||||
var langParts = raw.split(';');
|
||||
var code = langParts[0].split('-')[0].toLowerCase();
|
||||
var q = 1.0;
|
||||
for (var j = 1; j < langParts.length; j++) {
|
||||
var kv = langParts[j].trim().split('=');
|
||||
if (kv[0] === 'q' && kv[1]) q = parseFloat(kv[1]);
|
||||
}
|
||||
parsed.push({ code: code, q: isNaN(q) ? 1.0 : q });
|
||||
}
|
||||
// Sort by q-value descending so the highest-priority language is tried first
|
||||
parsed.sort(function (a, b) { return b.q - a.q; });
|
||||
for (var k = 0; k < parsed.length; k++) {
|
||||
if (isSupported(parsed[k].code)) return parsed[k].code;
|
||||
}
|
||||
return DEFAULT_LANGUAGE;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user