-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove useLocale composition and use router based locale selection
- Loading branch information
Showing
6 changed files
with
76 additions
and
144 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
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 |
---|---|---|
@@ -1,27 +1,38 @@ | ||
import { createI18n } from 'vue-i18n' | ||
import { selectedLocale } from './utilities' | ||
|
||
export const availableLocales: Readonly<Array<string>> = ['en', 'ru'] | ||
export type AvailableLocale = 'en' | 'ru' | ||
export const DEFAULT_LOCALE: AvailableLocale = 'en' | ||
export const AVAILABLE_LOCALES: Readonly<Array<AvailableLocale>> = ['en', 'ru'] | ||
|
||
const i18n = createI18n({ | ||
legacy: false, | ||
locale: 'en', | ||
availableLocales, | ||
locale: DEFAULT_LOCALE, | ||
availableLocales: AVAILABLE_LOCALES, | ||
}) | ||
|
||
export async function loadLocaleMessages(locale: string) { | ||
return await import(`./locales/${locale}.json`) | ||
} | ||
|
||
export function getBrowserLanguage() { | ||
if (!navigator.language) return 'en' | ||
export function getBrowserLanguage(): AvailableLocale { | ||
if (!navigator.language) return DEFAULT_LOCALE | ||
|
||
// get first language code (e.g. get 'en' from en-US) | ||
const navigatorLanguage = navigator.language.trim().split(/-|_/)[0] | ||
if (availableLocales.includes(navigatorLanguage)) { | ||
return navigatorLanguage | ||
const browserLanguage = navigator.language.trim().split(/-|_/)[0] | ||
if (AVAILABLE_LOCALES.some((language) => language === browserLanguage)) { | ||
return browserLanguage as AvailableLocale | ||
} else { | ||
return 'en' | ||
return DEFAULT_LOCALE | ||
} | ||
} | ||
|
||
export async function setLocale(locale: AvailableLocale) { | ||
selectedLocale.value = locale | ||
const messages = await loadLocaleMessages(locale) | ||
i18n.global.setLocaleMessage(locale, messages.default) | ||
i18n.global.locale.value = locale | ||
document.querySelector('html')?.setAttribute('lang', locale) | ||
} | ||
|
||
export default i18n |
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
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