diff --git a/docs/content/50.API/2.vue-i18n.md b/docs/content/50.API/2.vue-i18n.md index b84204c0b..ca90f92bd 100644 --- a/docs/content/50.API/2.vue-i18n.md +++ b/docs/content/50.API/2.vue-i18n.md @@ -55,6 +55,12 @@ Switches to the pending locale that would have been set on navigate, but was pre Returns a promise that will be resolved once the pending locale is set. +### strategy + +- **Type**: `Strategies` + +Routing strategy as specified in options. + ### defaultDirection ::alert{type="warning"} diff --git a/playground/pages/index.vue b/playground/pages/index.vue index 343bfcaa3..b2414e57f 100644 --- a/playground/pages/index.vue +++ b/playground/pages/index.vue @@ -4,7 +4,7 @@ import { computed } from 'vue' // import { useLocalePath, useSwitchLocalePath, useLocaleHead, useBrowserLocale } from '#i18n' import { LocaleObject, useI18n } from '#i18n' -const { t, locale, locales, localeProperties, setLocale } = useI18n() +const { t, strategy, locale, locales, localeProperties, setLocale } = useI18n() const localePath = useLocalePath() const switchLocalePath = useSwitchLocalePath() @@ -35,11 +35,18 @@ const availableLocales = computed(() => { About

Current Language: {{ getLocaleName(locale) }}

-

Select Languages

+

Current Strategy: {{ strategy }}

+

Select Languages with switchLocalePath

+

Select Languages with setLocale

+ diff --git a/src/gen.ts b/src/gen.ts index 3f7feb998..58fad8a04 100644 --- a/src/gen.ts +++ b/src/gen.ts @@ -3,7 +3,7 @@ import { isString, isRegExp, isFunction, isArray, isObject } from '@intlify/shar import { generateJSON } from '@intlify/bundle-utils' import { genImport, genSafeVariableName, genDynamicImport } from 'knitwork' -import type { NuxtI18nOptions, NuxtI18nInternalOptions, LocaleInfo, NoNullable } from './types' +import type { NuxtI18nOptions, NuxtI18nInternalOptions, LocaleInfo } from './types' import type { NuxtI18nOptionsDefault } from './constants' export type LoaderOptions = { @@ -17,7 +17,7 @@ export type LoaderOptions = { const debug = createDebug('@nuxtjs/i18n:gen') export function generateLoaderOptions( - lazy: NoNullable, + lazy: NonNullable, langDir: NuxtI18nOptions['langDir'], options: LoaderOptions = {}, dev = true diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index 66f54be37..6f10cf3e1 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -100,6 +100,7 @@ export default defineNuxtPlugin(async nuxt => { baseUrl: nuxtI18nOptions.baseUrl, hooks: { onExtendComposer(composer: Composer) { + composer.strategy = nuxtI18nOptions.strategy composer.localeProperties = computed(() => { return ( nuxtI18nInternalOptions.__normalizedLocales.find((l: LocaleObject) => l.code === composer.locale.value) || { @@ -161,6 +162,11 @@ export default defineNuxtPlugin(async nuxt => { }, onExtendExportedGlobal(g: Composer): ExtendProperyDescripters { return { + strategy: { + get() { + return g.strategy + } + }, localeProperties: { get() { return g.localeProperties.value @@ -208,6 +214,11 @@ export default defineNuxtPlugin(async nuxt => { }, onExtendVueI18n(composer: Composer): ExtendProperyDescripters { return { + strategy: { + get() { + return composer.strategy + } + }, localeProperties: { get() { return composer.localeProperties.value diff --git a/src/runtime/vue-i18n-bridge.d.ts b/src/runtime/vue-i18n-bridge.d.ts index 96df07924..0c4bf16a3 100644 --- a/src/runtime/vue-i18n-bridge.d.ts +++ b/src/runtime/vue-i18n-bridge.d.ts @@ -1,8 +1,9 @@ import type { ComputedRef } from 'vue-demi' -import type { LocaleObject } from 'vue-i18n-routing' +import type { LocaleObject, Strategies } from 'vue-i18n-routing' import type { BeforeLanguageSwitchHandler, LanguageSwitchedHandler } from '#build/i18n.options.mjs' export interface ComposerCustomProperties { + strategy: Strategies localeProperties: ComputedRef setLocale: (locale: string) => Promise getBrowserLocale: () => string | undefined @@ -18,6 +19,7 @@ declare module 'vue-i18n' { // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ComposerCustom extends ComposerCustomProperties {} export interface VueI18n { + readonly strategy: Strategies localeProperties: LocaleObject setLocale: (locale: string) => Promise getBrowserLocale: () => string | undefined @@ -40,6 +42,7 @@ declare module 'vue-i18n-bridge' { // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ComposerCustom extends ComposerCustomProperties {} export interface VueI18n { + readonly strategy: Strategies localeProperties: LocaleObject setLocale: (locale: string) => Promise getBrowserLocale: () => string | undefined @@ -61,6 +64,7 @@ declare module '@intlify/vue-i18n-bridge' { // eslint-disable-next-line @typescript-eslint/no-empty-interface export interface ComposerCustom extends ComposerCustomProperties {} export interface VueI18n { + readonly strategy: Strategies localeProperties: LocaleObject setLocale: (locale: string) => Promise getBrowserLocale: () => string | undefined diff --git a/src/types.ts b/src/types.ts index 51285ec97..64b66b619 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,8 +4,6 @@ import type { Locale, I18nOptions } from '@intlify/vue-i18n-bridge' // export type LegacyContext = Parameters[0] // nuxt2 context type -export type NoNullable = Exclude - export type RedirectOnOptions = 'all' | 'root' | 'no prefix' export interface LazyOptions {