Skip to content

Commit

Permalink
Fix UI language matching (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
eligrey authored Oct 21, 2024
1 parent 8c76268 commit 27dc599
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/transcend-io/consent-manager-ui.git"
},
"homepage": "https://github.com/transcend-io/consent-manager-ui",
"version": "4.21.0",
"version": "4.21.1",
"license": "MIT",
"main": "build/ui",
"files": [
Expand Down
42 changes: 22 additions & 20 deletions src/hooks/useLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,6 @@ export const getNearestSupportedLanguage = (
),
);

/**
* Picks a default language for the user
*
* @param supportedLanguages - Set of supported languages
* @returns the language key of the best default language for this user
*/
export function pickDefaultLanguage(
supportedLanguages: ConsentManagerLanguageKey[],
): ConsentManagerLanguageKey {
if (settings.locale && supportedLanguages.includes(settings.locale)) {
return settings.locale;
}

const preferredLanguages = getUserLanguages();
return (
getNearestSupportedLanguage(preferredLanguages, supportedLanguages) ||
ConsentManagerLanguageKey.En
);
}

/**
* Sorts the supported languages by the user's preferences
*
Expand All @@ -120,6 +100,28 @@ export const sortSupportedLanguagesByPreference = (
return rank(a) - rank(b);
});

/**
* Picks a default language for the user
*
* @param supportedLanguages - Set of supported languages
* @returns the language key of the best default language for this user
*/
export function pickDefaultLanguage(
supportedLanguages: ConsentManagerLanguageKey[],
): ConsentManagerLanguageKey {
if (settings.locale && supportedLanguages.includes(settings.locale)) {
return settings.locale;
}

const preferredLanguages = getUserLanguages();
return (
getNearestSupportedLanguage(
preferredLanguages,
sortSupportedLanguagesByPreference(supportedLanguages),
) || ConsentManagerLanguageKey.En
);
}

/**
* Fetch message translations
*
Expand Down

0 comments on commit 27dc599

Please sign in to comment.