-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve browser language detection and set nl-be fallback
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"frontend-gelinkt-notuleren": minor | ||
--- | ||
|
||
Improve browser language detection and set a fallback of `nl-BE` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Helper function to help pick supported locales for ember-intl. | ||
* Returns exact matches first, then language matches, then the default. | ||
**/ | ||
export function decentLocaleMatch( | ||
userLocales, | ||
supportedLocales, | ||
defaultLocale, | ||
) { | ||
// Ember-intl lowercases locales so we can make comparisons easier by doing the same | ||
const userLocs = userLocales.map((locale) => locale.toLowerCase()); | ||
const supportedLocs = supportedLocales.map((locale) => locale.toLowerCase()); | ||
|
||
// First find exact matches. Use a set to avoid duplicates while preserving insert order. | ||
const matches = new Set( | ||
userLocs.filter((locale) => supportedLocs.includes(locale)), | ||
); | ||
|
||
// Then look for locales that just match based on language, | ||
// e.g. match en or en-US if looking for en-GB | ||
const languageMap = {}; | ||
supportedLocs.forEach((locale) => { | ||
const lang = locale.split('-')[0]; | ||
languageMap[lang] = [...(languageMap[lang] || []), locale]; | ||
}); | ||
userLocs.forEach((locale) => { | ||
const looseMatches = languageMap[locale.split('-')[0]] ?? []; | ||
looseMatches.forEach((match) => matches.add(match)); | ||
}); | ||
|
||
// Add the default so we always have something | ||
matches.add(defaultLocale.toLowerCase()); | ||
return [...matches]; | ||
} |