Skip to content

Commit

Permalink
feat(shared): add new getBestMatchingLocaleName function
Browse files Browse the repository at this point in the history
  • Loading branch information
lego-technix committed Oct 2, 2024
1 parent f09455c commit 0d7f35a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
17 changes: 17 additions & 0 deletions shared/composables/useLocaleCookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;

const previousLocaleCookieToDelete = useCookie(LOCALE_COOKIE_NAME, {
maxAge: 31536000, // 1 year
sameSite: 'strict',
Expand All @@ -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,
};
}
44 changes: 44 additions & 0 deletions shared/tests/composables/useLocaleCookie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -18,6 +20,7 @@ mockNuxtImport('useRuntimeConfig', () => {
return () => ({
public: {
siteDomain: 'ORG',
availableLocaleNames,
},
});
});
Expand Down Expand Up @@ -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');
});
});
});
});

0 comments on commit 0d7f35a

Please sign in to comment.