Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add defaultLocale property #2144

Merged
merged 2 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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