Skip to content

Commit

Permalink
fix: add defaultLocale property (#2144)
Browse files Browse the repository at this point in the history
* fix: add defaultLocale property

resolve #2030

* fix
  • Loading branch information
kazupon committed Jun 9, 2023
1 parent 18bba6a commit 5ed73d5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions playground/pages/about/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineComponent({
console.log('$i18n.getBrowserLocale', this.$i18n.getBrowserLocale())
console.log('$i18n.getLocaleCookie', this.$i18n.getLocaleCookie())
console.log('$i18n.localeProperties', this.$i18n.localeProperties)
console.log('$i18n.defaultLocale', this.$i18n.defaultLocale)
},
computed: {
availableLocales() {
Expand Down
2 changes: 1 addition & 1 deletion playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { computed } from 'vue'
import { LocaleObject } from '#i18n'
const route = useRoute()
const { t, strategy, locale, locales, localeProperties, setLocale, finalizePendingLocaleChange } = useI18n()
const { t, strategy, locale, locales, localeProperties, setLocale, defaultLocale, finalizePendingLocaleChange } = useI18n()
const localePath = useLocalePath()
const switchLocalePath = useSwitchLocalePath()
const getRouteBaseName = useRouteBaseName()
Expand Down
11 changes: 11 additions & 0 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export default defineNuxtPlugin(async nuxt => {
)
}
composer.differentDomains = differentDomains
composer.defaultLocale = defaultLocale
composer.getBrowserLocale = () => _getBrowserLocale(nuxtI18nInternalOptions, nuxt.ssrContext)
composer.getLocaleCookie = () =>
_getLocaleCookie(nuxt.ssrContext, { ...nuxtI18nOptions.detectBrowserLanguage, localeCodes })
Expand Down Expand Up @@ -268,6 +269,11 @@ export default defineNuxtPlugin(async nuxt => {
return g.differentDomains
}
},
defaultLocale: {
get() {
return g.defaultLocale
}
},
getBrowserLocale: {
get() {
return () => Reflect.apply(g.getBrowserLocale, g, [])
Expand Down Expand Up @@ -330,6 +336,11 @@ export default defineNuxtPlugin(async nuxt => {
return composer.differentDomains
}
},
defaultLocale: {
get() {
return composer.defaultLocale
}
},
getBrowserLocale: {
get() {
return () => Reflect.apply(composer.getBrowserLocale, composer, [])
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/server/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ async function precompileConfig(filename: string, messages: NonNullable<I18nOpti
* (we know use monorepo packages...)
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function deepCopy(src: Record<string, any>, des: Record<string, any>, predicate?: (src: any, des: any) => boolean) {
for (const key in src) {
if (isObject(src[key])) {
Expand All @@ -127,7 +128,7 @@ function deepCopy(src: Record<string, any>, des: Record<string, any>, predicate?
export function getNeedPrecompileMessages(messages: NonNullable<I18nOptions['messages']>) {
const needPrecompileMessages: NonNullable<I18nOptions['messages']> = {}
// ignore, if messages will have function
const predicate = (src: any) => !isFunction(src)
const predicate = (src: any) => !isFunction(src) // eslint-disable-line @typescript-eslint/no-explicit-any

for (const [locale, message] of Object.entries(messages)) {
const dest = (needPrecompileMessages[locale] = {})
Expand Down
8 changes: 8 additions & 0 deletions src/runtime/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export interface ComposerCustomProperties {
* Default direction as specified in options.
*/
defaultDirection: Directions
/**
* Default locale as specified in options.
*/
defaultLocale: Locale
/**
* Switches locale of the app to specified locale code.
*
Expand Down Expand Up @@ -133,6 +137,10 @@ export interface NuxtI18nRoutingCustomProperties {
* Default direction as specified in options.
*/
readonly defaultDirection: Directions
/**
* Default locale as specified in options.
*/
readonly defaultLocale: Locale
/**
* Switches locale of the app to specified locale code.
*
Expand Down
1 change: 1 addition & 0 deletions src/transform/dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const ResourceDynamicPlugin = createUnplugin((options: ResourceDynamicPlu
return
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const resources = [...resoucesMap].reduce((obj, [_, { hash, type, locale, ref }]) => {
obj[hash] = { hash, type, locale, path: this.getFileName(ref) }
return obj
Expand Down

0 comments on commit 5ed73d5

Please sign in to comment.