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

feat: support strategy property API #1533

Merged
merged 1 commit into from
Sep 24, 2022
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
6 changes: 6 additions & 0 deletions docs/content/50.API/2.vue-i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
11 changes: 9 additions & 2 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -35,11 +35,18 @@ const availableLocales = computed(() => {
<NuxtLink :to="localePath('/about')">About</NuxtLink>
</nav>
<h2>Current Language: {{ getLocaleName(locale) }}</h2>
<h2>Select Languages</h2>
<h2>Current Strategy: {{ strategy }}</h2>
<h2>Select Languages with switchLocalePath</h2>
<nav>
<span v-for="locale in availableLocales" :key="locale.code">
<NuxtLink :to="switchLocalePath(locale.code) || ''">{{ locale.name }}</NuxtLink> |
</span>
</nav>
<h2>Select Languages with setLocale</h2>
<nav>
<span v-for="locale in availableLocales" :key="locale.code">
<a href="#" @click="setLocale(locale.code)">{{ locale.name }}</a> |
</span>
</nav>
</div>
</template>
4 changes: 2 additions & 2 deletions src/gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -17,7 +17,7 @@ export type LoaderOptions = {
const debug = createDebug('@nuxtjs/i18n:gen')

export function generateLoaderOptions(
lazy: NoNullable<NuxtI18nOptions['lazy']>,
lazy: NonNullable<NuxtI18nOptions['lazy']>,
langDir: NuxtI18nOptions['langDir'],
options: LoaderOptions = {},
dev = true
Expand Down
11 changes: 11 additions & 0 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) || {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/vue-i18n-bridge.d.ts
Original file line number Diff line number Diff line change
@@ -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<LocaleObject>
setLocale: (locale: string) => Promise<void>
getBrowserLocale: () => string | undefined
Expand All @@ -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<void>
getBrowserLocale: () => string | undefined
Expand All @@ -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<void>
getBrowserLocale: () => string | undefined
Expand All @@ -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<void>
getBrowserLocale: () => string | undefined
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import type { Locale, I18nOptions } from '@intlify/vue-i18n-bridge'

// export type LegacyContext = Parameters<LegacyPlugin>[0] // nuxt2 context type

export type NoNullable<T> = Exclude<T, null | undefined>

export type RedirectOnOptions = 'all' | 'root' | 'no prefix'

export interface LazyOptions {
Expand Down