From f745d71278d3e89a44cb7b50108cc2f182696b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Gran=C3=A1t?= Date: Wed, 6 Nov 2024 13:33:25 +0100 Subject: [PATCH 1/2] fix: language detector incorrectly touching window --- packages/web/src/package/LanguageDetector.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web/src/package/LanguageDetector.ts b/packages/web/src/package/LanguageDetector.ts index a2cf29bad5..cd20d689fb 100644 --- a/packages/web/src/package/LanguageDetector.ts +++ b/packages/web/src/package/LanguageDetector.ts @@ -10,7 +10,7 @@ export function detectLanguage(language: string, availableLanguages: string[]) { const getTwoLetters = (fullTag: string) => fullTag.replace(/^(.+?)(-.*)?$/, '$1'); - const preferredTwoLetter = getTwoLetters(window.navigator.language); + const preferredTwoLetter = getTwoLetters(language); const twoLetterMatch = availableLanguages.find( (l) => getTwoLetters(l) === preferredTwoLetter ); From 1e76a60b11deae200f42c3a9af26b7165e296064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Gran=C3=A1t?= Date: Wed, 6 Nov 2024 13:37:16 +0100 Subject: [PATCH 2/2] chore: add tests --- .../web/src/package/LanguageDetector.test.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/web/src/package/LanguageDetector.test.ts b/packages/web/src/package/LanguageDetector.test.ts index efe0d8989b..45bbc783ac 100644 --- a/packages/web/src/package/LanguageDetector.test.ts +++ b/packages/web/src/package/LanguageDetector.test.ts @@ -1,12 +1,12 @@ -import { createLanguageDetector } from './LanguageDetector'; +import { createLanguageDetector, detectLanguage } from './LanguageDetector'; -describe('language detector', () => { +describe('language detector plugin', () => { beforeEach(() => { const languageGetter = jest.spyOn(window.navigator, 'language', 'get'); languageGetter.mockReturnValue('cs-CZ'); }); - it('detects language', () => { + it('detects language from window.navigator', () => { const detector = createLanguageDetector(); const result = detector.getLanguage({ @@ -17,3 +17,13 @@ describe('language detector', () => { expect(result).toEqual('cs'); }); }); + +describe('detect language from parameters', () => { + it('detects language exact', () => { + expect(detectLanguage('cs', ['en', 'cs'])).toEqual('cs'); + }); + + it('detects language prefix', () => { + expect(detectLanguage('cs-CZ', ['en', 'cs'])).toEqual('cs'); + }); +});