From 0d7f35ad4d2ae202d9461ecb968b5a4eb52c09ab Mon Sep 17 00:00:00 2001 From: LEGO Technix <109212476+lego-technix@users.noreply.github.com> Date: Wed, 2 Oct 2024 14:24:27 +0200 Subject: [PATCH] feat(shared): add new getBestMatchingLocaleName function --- shared/composables/useLocaleCookie.ts | 17 +++++++ .../tests/composables/useLocaleCookie.test.ts | 44 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/shared/composables/useLocaleCookie.ts b/shared/composables/useLocaleCookie.ts index 213b63012..805695442 100644 --- a/shared/composables/useLocaleCookie.ts +++ b/shared/composables/useLocaleCookie.ts @@ -3,9 +3,12 @@ const LOCALE_COOKIE_NAME = 'locale'; export default function useLocaleCookie() { const appConfig = useAppConfig(); const runtimeConfig = useRuntimeConfig(); + const domainFrUrl = new URL(appConfig.domainFr); const domainOrgUrl = new URL(appConfig.domainOrg); + const availableLocaleNames = runtimeConfig.public.availableLocaleNames as Array; + const previousLocaleCookieToDelete = useCookie(LOCALE_COOKIE_NAME, { maxAge: 31536000, // 1 year sameSite: 'strict', @@ -29,8 +32,22 @@ export default function useLocaleCookie() { if (callback) callback(); } + function getBestMatchingLocaleName(localeName: string) { + if (availableLocaleNames.includes(localeName)) { + return localeName; + } + + const languageLocaleName = new Intl.Locale(localeName).language; + if (availableLocaleNames.includes(languageLocaleName)) { + return languageLocaleName; + } + + return availableLocaleNames[0]; + } + return { localeCookie, setLocaleCookie, + getBestMatchingLocaleName, }; } diff --git a/shared/tests/composables/useLocaleCookie.test.ts b/shared/tests/composables/useLocaleCookie.test.ts index a672448cc..e1b7189ab 100644 --- a/shared/tests/composables/useLocaleCookie.test.ts +++ b/shared/tests/composables/useLocaleCookie.test.ts @@ -3,6 +3,8 @@ import { mockNuxtImport } from '@nuxt/test-utils/runtime'; import useLocaleCookie from '../../composables/useLocaleCookie'; +const availableLocaleNames = ['en', 'fr', 'fr-fr']; + mockNuxtImport('useCookie', () => { return () => ({ value: '', @@ -18,6 +20,7 @@ mockNuxtImport('useRuntimeConfig', () => { return () => ({ public: { siteDomain: 'ORG', + availableLocaleNames, }, }); }); @@ -74,4 +77,45 @@ describe('#useLocaleCookie', () => { }); }); }); + + describe('getBestMatchingLocaleName', () => { + describe('when wanted locale is available', () => { + test('returns the same locale name', () => { + // given + const { getBestMatchingLocaleName } = useLocaleCookie(); + + // when + const matchingLocaleName = getBestMatchingLocaleName('fr-fr'); + + // then + expect(matchingLocaleName).toEqual('fr-fr'); + }); + }); + + describe('when wanted locale is unavailable but an available locale with same language is', () => { + test('returns the same locale name', () => { + // given + const { getBestMatchingLocaleName } = useLocaleCookie(); + + // when + const matchingLocaleName = getBestMatchingLocaleName('fr-be'); + + // then + expect(matchingLocaleName).toEqual('fr'); + }); + }); + + describe('when wanted locale is unavailable and no available locale with same language is', () => { + test('returns the first available locale name', () => { + // given + const { getBestMatchingLocaleName } = useLocaleCookie(); + + // when + const matchingLocaleName = getBestMatchingLocaleName('nl-be'); + + // then + expect(matchingLocaleName).toEqual('en'); + }); + }); + }); });