diff --git a/playground/pages/about/index.vue b/playground/pages/about/index.vue index fc984d8c0..e63c8b87e 100644 --- a/playground/pages/about/index.vue +++ b/playground/pages/about/index.vue @@ -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() { diff --git a/playground/pages/index.vue b/playground/pages/index.vue index 504c291cd..3a1fefbd7 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -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() diff --git a/src/runtime/plugins/i18n.ts b/src/runtime/plugins/i18n.ts index d4f26edcb..b24134efc 100644 --- a/src/runtime/plugins/i18n.ts +++ b/src/runtime/plugins/i18n.ts @@ -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 }) @@ -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, []) @@ -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, []) diff --git a/src/runtime/server/dynamic.ts b/src/runtime/server/dynamic.ts index 38b1c11d5..0b067b368 100644 --- a/src/runtime/server/dynamic.ts +++ b/src/runtime/server/dynamic.ts @@ -107,6 +107,7 @@ async function precompileConfig(filename: string, messages: NonNullable, des: Record, predicate?: (src: any, des: any) => boolean) { for (const key in src) { if (isObject(src[key])) { @@ -127,7 +128,7 @@ function deepCopy(src: Record, des: Record, predicate? export function getNeedPrecompileMessages(messages: NonNullable) { const needPrecompileMessages: NonNullable = {} // 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] = {}) diff --git a/src/runtime/types.d.ts b/src/runtime/types.d.ts index 1fd4ef14e..c41f0f0d3 100644 --- a/src/runtime/types.d.ts +++ b/src/runtime/types.d.ts @@ -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. * @@ -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. * diff --git a/src/transform/dynamic.ts b/src/transform/dynamic.ts index 151fbc6eb..3e0b2fae6 100644 --- a/src/transform/dynamic.ts +++ b/src/transform/dynamic.ts @@ -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