feat(web): use browser language by default (#10849)

This commit is contained in:
Michel Heusschen 2024-07-05 05:56:54 +02:00 committed by GitHub
parent 6629bf50ae
commit 6030349a6f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 55 additions and 10 deletions

View file

@ -1,5 +1,6 @@
import { langs } from '$lib/constants';
import messages from '$lib/i18n/en.json';
import { getClosestAvailableLocale } from '$lib/utils/i18n';
import { exec as execCallback } from 'node:child_process';
import { readFileSync, readdirSync } from 'node:fs';
import { promisify } from 'node:util';
@ -52,4 +53,30 @@ describe('i18n', () => {
});
}
});
describe('getClosestAvailableLocale', () => {
const allLocales = ['ar', 'bg', 'en', 'en-US', 'en-DE', 'zh-Hans', 'zh-Hans-HK'];
it('returns undefined on mismatch', () => {
expect(getClosestAvailableLocale([], allLocales)).toBeUndefined();
expect(getClosestAvailableLocale(['invalid'], allLocales)).toBeUndefined();
});
it('returns the first matching locale', () => {
expect(getClosestAvailableLocale(['invalid', 'ar', 'bg'], allLocales)).toBe('ar');
expect(getClosestAvailableLocale(['bg'], allLocales)).toBe('bg');
expect(getClosestAvailableLocale(['bg', 'invalid', 'ar'], allLocales)).toBe('bg');
});
it('returns the locale for a less specific match', () => {
expect(getClosestAvailableLocale(['ar-AE'], allLocales)).toBe('ar-AE');
expect(getClosestAvailableLocale(['ar-AE', 'en'], allLocales)).toBe('ar-AE');
expect(getClosestAvailableLocale(['zh-Hans-HK', 'zh-Hans'], allLocales)).toBe('zh-Hans-HK');
});
it('ignores the locale for a more specific match', () => {
expect(getClosestAvailableLocale(['zh'], allLocales)).toBeUndefined();
expect(getClosestAvailableLocale(['de', 'zh', 'en-US'], allLocales)).toBe('en-US');
});
});
});