Skip to content

Commit 8311b17

Browse files
Support language code on URL in the form ?l=cy (#202)
Precedence as per classroom/Python.
1 parent 0c1d441 commit 8311b17

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/__tests__/i18n.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ describe('Initialization tests', () => {
2828
navigator: {
2929
languages: ['cy', 'en'],
3030
},
31+
location: {
32+
search: '',
33+
},
3134
}));
3235
const i18n = await import('../i18n');
3336
const getText = get(i18n.t);
@@ -42,6 +45,9 @@ describe('Initialization tests', () => {
4245
navigator: {
4346
languages: ['random-language'],
4447
},
48+
location: {
49+
search: '',
50+
},
4551
}));
4652
const i18n = await import('../i18n');
4753
const getText = get(i18n.t);
@@ -50,4 +56,21 @@ describe('Initialization tests', () => {
5056

5157
expect(translatedText).toBe('You are currently recording!');
5258
});
59+
60+
test('Language is set to welsh when it is defined query string', async () => {
61+
windowSpy.mockImplementation(() => ({
62+
navigator: {
63+
languages: ['some languages'],
64+
},
65+
location: {
66+
search: '?l=cy',
67+
},
68+
}));
69+
const i18n = await import('../i18n');
70+
const getText = get(i18n.t);
71+
72+
const translatedText = getText('alert.isRecording');
73+
74+
expect(translatedText).toBe("Rydych chi'n recordio ar hyn o bryd!");
75+
});
5376
});

src/i18n.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import browserLang from 'browser-lang';
88
import { FormatXMLElementFn } from 'intl-messageformat';
9-
import { init, locale, register } from 'svelte-i18n';
9+
import { getLocaleFromQueryString, init, locale, register } from 'svelte-i18n';
1010
import { get } from 'svelte/store';
1111
import { persistantWritable } from './script/stores/storeUtil';
1212
export { t } from 'svelte-i18n';
@@ -54,10 +54,12 @@ export const allLanguages = [
5454
register('en', () => import('./messages/ui.en.json'));
5555
register('cy', () => import('./messages/ui.cy.json'));
5656

57-
const initialLocale = browserLang({
58-
languages: allLanguages.map(l => l.id),
59-
fallback: 'en',
60-
});
57+
const initialLocale =
58+
getLocaleFromQueryString('l') ||
59+
browserLang({
60+
languages: allLanguages.map(l => l.id),
61+
fallback: 'en',
62+
});
6163

6264
const persistantLocale = persistantWritable('lang', initialLocale);
6365

0 commit comments

Comments
 (0)