One of my files, named locale.js
, is responsible for determining the user's locale. Below is the code snippet:
import store from '@/vuex/index'
let locale
const defaultLocale = 'en_US'
if (store.getters['auth/authenticated']) {
locale = store.getters['auth/currentUser'].locale || defaultLocale
} else {
if (localStorage.getItem('locale')) {
locale = localStorage.getItem('locale')
} else {
locale = defaultLocale
}
}
export default locale
Additionally, there is another file named i18n.js
that creates the i18n
instance used during app initialization.
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import locale from '@/services/locale'
Vue.use(VueI18n)
const fallbackLocale = 'en_US'
let i18n = new VueI18n({
locale,
fallbackLocale,
})
i18n.setLocaleMessage('ru_RU', require('@/lang/ru_RU.json'))
i18n.setLocaleMessage('en_US', require('@/lang/en_US.json'))
export { i18n }
I am considering adding locale prefixes to URLs, such as /en/profile
or /ru/profile
, for easier sharing. However, I am unsure of the best approach to implement this change. Simply making all routes children and adding /:locale?
is not ideal due to initialization constraints (I pass i18n
and router
instances simultaneously during app root initialization).
What would be the most effective method to achieve this?