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

refactor: integrate vue-i18n-routing #2686

Merged
merged 15 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: cleanup routing utils and extends
  • Loading branch information
BobbieGoede committed Jan 11, 2024
commit 32d59d842a69635d834c7e3c980028e4920f2717
13 changes: 10 additions & 3 deletions src/runtime/routing/extends/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ import {
} from '../compatibles'

import type { NuxtApp } from 'nuxt/app'
import type { Composer, ComposerExtender, Disposer, I18n, VueI18n, VueI18nExtender } from 'vue-i18n'
import type {
Composer,
ComposerExtender,
Disposer,
ExportedGlobalComposer,
I18n,
VueI18n,
VueI18nExtender
} from 'vue-i18n'
import type { I18nRoutingOptions, LocaleObject } from '#build/i18n.options.mjs'

/**
Expand Down Expand Up @@ -218,8 +226,7 @@ function extendPropertyDescriptors(
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function extendExportedGlobal(exported: any, g: Composer, hook?: ExtendExportedGlobalHook) {
function extendExportedGlobal(exported: ExportedGlobalComposer, g: Composer, hook?: ExtendExportedGlobalHook) {
extendPropertyDescriptors(g, exported, hook)
}

Expand Down
35 changes: 6 additions & 29 deletions src/runtime/routing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@ import { isRef, unref } from '#imports'

import type { LocaleObject, Strategies, BaseUrlResolveHandler } from '#build/i18n.options.mjs'
import type { Composer, I18n, Locale, VueI18n } from 'vue-i18n'
import type { useRoute, useRouter } from '#imports'

/**
* @public
*/
export interface ComposableOptions {
/**
* vue-router route instance, which is returned with `useRoute`.
*/
route?: ReturnType<typeof useRoute>
/**
* vue-router router instance, which is returned with `useRouter`.
*/
router?: ReturnType<typeof useRouter>
/**
* vue-i18n Composer instance.
*/
i18n?: Composer
}

export const inBrowser = typeof window !== 'undefined'

Expand Down Expand Up @@ -57,14 +38,12 @@ export function getI18nTarget(i18n: I18n | VueI18n | Composer) {
}

export function getComposer(i18n: I18n | VueI18n | Composer): Composer {
// prettier-ignore
return isI18nInstance(i18n)
? isComposer(i18n.global)
? i18n.global
: ((i18n.global as any).__composer as Composer)
: isVueI18n(i18n)
? ((i18n as any).__composer as Composer)
: i18n
const target = getI18nTarget(i18n)

if (isComposer(target)) return target
if (isVueI18n(target)) return (target as any).__composer as Composer

return target
}

/**
Expand All @@ -75,7 +54,6 @@ export function getComposer(i18n: I18n | VueI18n | Composer): Composer {
* @returns A locale
*/
export function getLocale(i18n: I18n | Composer | VueI18n): Locale {
// TODO: we might re-design `getLocale` for vue-i18n-next & vue-i18n-bridge (legacy mode & Vue 2)
return unref(getI18nTarget(i18n).locale)
}

Expand All @@ -102,7 +80,6 @@ export function setLocale(i18n: I18n | Composer, locale: Locale): void {
}
}

// Language: typescript
export function adjustRoutePathForTrailingSlash(
pagePath: string,
trailingSlash: boolean,
Expand Down