generated from posva/vite-tailwind-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(i18n): add i18n configuration (French translation + language sel…
…ector in user settings) (#154) * feat(i18n): added config * feat(i18n): simplified UserSettings + rm unused files * Cleanup * Additional translations --------- Co-authored-by: dquirin <[email protected]> Co-authored-by: Raphael Odini <[email protected]>
- Loading branch information
1 parent
484f092
commit fe3f54d
Showing
37 changed files
with
1,383 additions
and
130 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
VITE_OPEN_PRICES_API_URL = "http://127.0.0.1:8000/api/v1" | ||
VITE_MANIFEST_PATH = "/manifest.local.json" | ||
VITE_DEFAULT_LOCALE = en | ||
VITE_FALLBACK_LOCALE = en |
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,2 +1,4 @@ | ||
VITE_OPEN_PRICES_API_URL = "https://prices.openfoodfacts.net/api/v1" | ||
VITE_MANIFEST_PATH = "/app/manifest.json" | ||
VITE_DEFAULT_LOCALE = en | ||
VITE_FALLBACK_LOCALE = en |
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,2 +1,4 @@ | ||
VITE_OPEN_PRICES_API_URL = "https://prices.openfoodfacts.org/api/v1" | ||
VITE_MANIFEST_PATH = "/app/manifest.json" | ||
VITE_DEFAULT_LOCALE = en | ||
VITE_FALLBACK_LOCALE = en |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,17 @@ | ||
import { createI18n } from 'vue-i18n' | ||
import en from './locales/en.json' | ||
|
||
const i18n = createI18n({ | ||
locale: import.meta.env.VITE_DEFAULT_LOCALE, | ||
fallbackLocale: import.meta.env.VITE_FALLBACK_LOCALE, | ||
legacy: false, | ||
globalInjection: true, | ||
messages: { en }, | ||
}) | ||
|
||
// React to language changes | ||
i18n.onLanguageChanged = (newLocale, oldLocale) => { | ||
console.log(`Language changed from ${oldLocale} to ${newLocale}`) | ||
// You can perform additional actions here if needed | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import i18n from '@/i18n' | ||
import { nextTick } from 'vue' | ||
import constants from '../constants' | ||
|
||
const localeManager = { | ||
get defaultLocale() { | ||
return import.meta.env.VITE_DEFAULT_LOCALE | ||
}, | ||
|
||
get supportedLocales() { | ||
return constants.LANGUAGE_LIST | ||
}, | ||
|
||
get currentLocale() { | ||
return i18n.global.locale.value | ||
}, | ||
|
||
set currentLocale(newLocale) { | ||
i18n.global.locale.value = newLocale | ||
}, | ||
|
||
async changeLanguage(newLocale) { | ||
await localeManager.loadLocaleMessages(newLocale) | ||
localeManager.currentLocale = newLocale | ||
document.querySelector('html').setAttribute('lang', newLocale) | ||
localStorage.setItem('user-locale', newLocale) | ||
|
||
}, | ||
|
||
async loadLocaleMessages(locale) { | ||
if(!i18n.global.availableLocales.includes(locale)) { | ||
const messages = await import(`@/i18n/locales/${locale}.json`) | ||
i18n.global.setLocaleMessage(locale, messages.default) | ||
} | ||
|
||
return nextTick() | ||
}, | ||
|
||
isLocaleSupported(locale) { | ||
return localeManager.supportedLocales.some(lang => lang.code === locale) | ||
}, | ||
|
||
getUserLocale() { | ||
const locale = window.navigator.language || | ||
window.navigator.userLanguage || | ||
localeManager.defaultLocale | ||
|
||
return { | ||
locale: locale, | ||
localeNoRegion: locale.split('-')[0] | ||
} | ||
}, | ||
|
||
getPersistedLocale() { | ||
const persistedLocale = localStorage.getItem('user-locale') | ||
|
||
if(localeManager.isLocaleSupported(persistedLocale)) { | ||
return persistedLocale | ||
} else { | ||
return null | ||
} | ||
}, | ||
|
||
guessDefaultLocale() { | ||
const userPersistedLocale = localeManager.getPersistedLocale() | ||
if(userPersistedLocale) { | ||
return userPersistedLocale | ||
} | ||
|
||
const userPreferredLocale = localeManager.getUserLocale() | ||
|
||
if (localeManager.isLocaleSupported(userPreferredLocale.locale)) { | ||
return userPreferredLocale.locale | ||
} | ||
|
||
if (localeManager.isLocaleSupported(userPreferredLocale.localeNoRegion)) { | ||
return userPreferredLocale.localeNoRegion | ||
} | ||
|
||
return localeManager.defaultLocale | ||
} | ||
|
||
} | ||
|
||
export default localeManager |
Oops, something went wrong.