Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
canUseDOM,
setCookie,
getCookieFromRequest,
isEqualCaseInsensitive,
} from './utilities'

class Internationalization<T extends string> {
Expand Down Expand Up @@ -46,8 +47,9 @@ class Internationalization<T extends string> {
(navigator.languages && navigator.languages[0]) || navigator.language

return (
this.supportedLanguages.find((lang) => lang === browserLanguage) ||
this.fallbackLanguage
this.supportedLanguages.find((lang) =>
isEqualCaseInsensitive(lang, browserLanguage)
) || this.fallbackLanguage
)
}

Expand All @@ -65,8 +67,9 @@ class Internationalization<T extends string> {
setLanguage(language: string): void {
if (canUseDOM()) {
const cookieLanguage =
this.supportedLanguages.find((lang) => lang === language) ||
this.fallbackLanguage
this.supportedLanguages.find((lang) =>
isEqualCaseInsensitive(lang, language)
) || this.fallbackLanguage

setCookie(this.cookieName, cookieLanguage)
}
Expand Down Expand Up @@ -109,7 +112,9 @@ class Internationalization<T extends string> {
.find(
({ locale }) =>
locale !== '*' &&
this.supportedLanguages.some((lang) => locale === lang)
this.supportedLanguages.some((lang) =>
isEqualCaseInsensitive(locale, lang)
)
)

// Check if the potential locale is not undefined else return the fallback language
Expand All @@ -131,8 +136,9 @@ class Internationalization<T extends string> {

// Return found language or fallback language
return (
this.supportedLanguages.find((lang) => lang === language) ||
this.fallbackLanguage
this.supportedLanguages.find((lang) =>
isEqualCaseInsensitive(lang, language)
) || this.fallbackLanguage
)
}

Expand Down
12 changes: 11 additions & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ function canUseDOM(): boolean {
)
}

export { getCookieFromDocument, getCookieFromRequest, setCookie, canUseDOM }
function isEqualCaseInsensitive(a: string, b: string): boolean {
return a.toLowerCase() === b.toLowerCase()
}

export {
getCookieFromDocument,
getCookieFromRequest,
setCookie,
canUseDOM,
isEqualCaseInsensitive,
}