Skip to content

Commit

Permalink
refactor: merge types and move prefixable util (#3284)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Dec 26, 2024
1 parent df49499 commit 757195a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 128 deletions.
7 changes: 3 additions & 4 deletions src/internal-global-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable no-var, @typescript-eslint/no-redundant-type-constituents */
import type { Composer, ExportedGlobalComposer, VueI18n } from 'vue-i18n'
import type { ComposerCustomProperties, NuxtI18nRoutingCustomProperties } from './runtime/types'

Expand All @@ -10,10 +9,11 @@ declare module 'vue-i18n' {

declare module '#app' {
interface NuxtApp {
$i18n: VueI18n & ExportedGlobalComposer & Composer & NuxtI18nRoutingCustomProperties & I18nRoutingCustomProperties
$i18n: VueI18n & ExportedGlobalComposer & Composer & NuxtI18nRoutingCustomProperties
}
}

/* eslint-disable no-var */
declare global {
var $t: Composer['t']
var $rt: Composer['rt']
Expand All @@ -22,7 +22,6 @@ declare global {
var $tm: Composer['tm']
var $te: Composer['te']
}
/* eslint-enable no-var */

export {}

/* eslint-enable no-var, @typescript-eslint/no-redundant-type-constituents */
9 changes: 1 addition & 8 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,12 @@ import { resolveRoute } from '../routing/routing'
import { localeHead } from '../routing/head'
import { useLocalePath, useLocaleRoute, useRouteBaseName, useSwitchLocalePath, useLocaleLocation } from '../composables'

import type { Locale, I18nOptions, Composer, I18n } from 'vue-i18n'
import type { Locale, I18nOptions, Composer } from 'vue-i18n'
import type { NuxtApp } from '#app'
import type { LocaleObject } from '#internal-i18n-types'
import type { I18nPublicRuntimeConfig } from '#internal-i18n-types'
import type { LocaleHeadFunction, ResolveRouteFunction } from '../composables'

// TODO: use @nuxt/module-builder to stub/prepare types
declare module '#app' {
interface NuxtApp {
_vueI18n: I18n
}
}

export default defineNuxtPlugin({
name: 'i18n:plugin',
parallel: parallelPlugin,
Expand Down
17 changes: 1 addition & 16 deletions src/runtime/routing/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,9 @@ import { extendPrefixable, extendSwitchLocalePathIntercepter, type CommonComposa

import type { Locale } from 'vue-i18n'
import type { RouteLocationRaw, RouteLocationPathRaw, RouteLocationNamedRaw, RouteRecordNameGeneric } from 'vue-router'
import type { I18nPublicRuntimeConfig, PrefixableOptions, Strategies } from '#internal-i18n-types'
import type { I18nPublicRuntimeConfig } from '#internal-i18n-types'
import type { CompatRoute } from '../types'

const RESOLVED_PREFIXED = new Set<Strategies>(['prefix_and_default', 'prefix_except_default'])

function prefixable(options: PrefixableOptions): boolean {
const { currentLocale, defaultLocale, strategy } = options
const isDefaultLocale = currentLocale === defaultLocale
// don't prefix default locale
return (
!(isDefaultLocale && RESOLVED_PREFIXED.has(strategy)) &&
// no prefix for any language
!(strategy === 'no_prefix')
)
}

export const DefaultPrefixable = prefixable

/**
* Returns base name of current (if argument not provided) or passed in route.
*
Expand Down
125 changes: 28 additions & 97 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NuxtApp } from '#app'
import type { ComputedRef } from 'vue'
import type { Directions, LocaleObject, Strategies } from '#internal-i18n-types'
import type { Locale } from 'vue-i18n'
import type { I18n, Locale } from 'vue-i18n'
import type { RouteLocationNormalizedGeneric, RouteRecordNameGeneric } from 'vue-router'

export type CompatRoute = Omit<RouteLocationNormalizedGeneric, 'name'> & {
Expand Down Expand Up @@ -36,32 +36,11 @@ type BeforeLanguageSwitchHandler = (
*/
type LanguageSwitchedHandler = (oldLocale: Locale, newLocale: Locale) => Promise<void>

export interface ComposerCustomProperties<
ConfiguredLocaleType extends Locale[] | LocaleObject[] = Locale[] | LocaleObject[]
> {
/**
* List of locales
*
* @remarks
* Can either be an array of string codes (e.g. `['en', 'fr']`) or an array of {@link LocaleObject} for more complex configurations
*/
locales: ComputedRef<ConfiguredLocaleType>
/**
* List of locale codes
*/
localeCodes: ComputedRef<Locale[]>
/**
* Base URL that is used in generating canonical links
*/
baseUrl: ComputedRef<string>
interface SharedProperties {
/**
* Routing strategy.
*/
strategy: Strategies
/**
* Current locale properties.
*/
localeProperties: ComputedRef<LocaleObject>
/**
* Whether differentDomains option is enabled.
*/
Expand Down Expand Up @@ -143,107 +122,59 @@ export interface ComposerCustomProperties<
waitForPendingLocaleChange: () => Promise<void>
}

export interface NuxtI18nRoutingCustomProperties<
export interface ComposerCustomProperties<
ConfiguredLocaleType extends Locale[] | LocaleObject[] = Locale[] | LocaleObject[]
> {
> extends SharedProperties {
/**
* List of locales
*
* @remarks
* Can either be an array of string codes (e.g. `['en', 'fr']`) or an array of {@link LocaleObject} for more complex configurations
*/
readonly locales: ConfiguredLocaleType
locales: ComputedRef<ConfiguredLocaleType>
/**
* List of locale codes
*/
readonly localeCodes: Locale[]
localeCodes: ComputedRef<Locale[]>
/**
* Base URL that is used in generating canonical links
*/
baseUrl: string
/**
* Routing strategy.
*/
readonly strategy: Strategies
baseUrl: ComputedRef<string>
/**
* Current locale properties.
*/
localeProperties: LocaleObject
/**
* Whether differentDomains option is enabled.
*/
readonly differentDomains: boolean
/**
* 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.
*
* @remarks
* If `useCookie` option is enabled, locale cookie will be updated with new value.
*
* If prefixes are enabled (`strategy` other than `no_prefix`), will navigate to new locale's route.
*
* @param locale - A {@link Locale}
*/
setLocale: (locale: Locale) => Promise<void>
/**
* Returns browser locale code filtered against the ones defined in options.
*
* @returns The browser locale.
*/
getBrowserLocale: () => string | undefined
/**
* Returns locale code from stored locale cookie.
*
* @returns The locale cookie
*/
getLocaleCookie: () => string | undefined
/**
* Updates stored locale cookie with specified locale code.
*
* @remarks
* Consider using `setLocale` instead if you want to switch locale.
*
* @param locale - A {@link Locale}
*/
setLocaleCookie: (locale: Locale) => void
localeProperties: ComputedRef<LocaleObject>
}

export interface NuxtI18nRoutingCustomProperties<
ConfiguredLocaleType extends Locale[] | LocaleObject[] = Locale[] | LocaleObject[]
> extends SharedProperties {
/**
* Called before the app's locale is switched.
* List of locales
*
* @remarks
* Can be used to override the new locale by returning a new locale code.
*
* @param oldLocale - The app's locale before the switch.
* @param newLocale - The app's locale after the switch.
* @param initialSetup - Set to `true` if it's the initial locale switch that triggers on app load. It's a special case since the locale is not technically set yet so we're switching from no locale to locale.
* @param context - the Nuxt app instance.
*
* @returns The new locale to switch, or `undefined` to keep the new locale.
* Can either be an array of string codes (e.g. `['en', 'fr']`) or an array of {@link LocaleObject} for more complex configurations
*/
onBeforeLanguageSwitch: BeforeLanguageSwitchHandler
readonly locales: ConfiguredLocaleType
/**
* Called after the app's locale is switched.
*
* @param oldLocale - The app's locale before the switch
* @param newLocale - The app's locale after the switch.
* List of locale codes
*/
onLanguageSwitched: LanguageSwitchedHandler
readonly localeCodes: Locale[]
/**
* Switches to the pending locale that would have been set on navigate, but was prevented by the `skipSettingLocaleOnNavigate` option.
* Base URL that is used in generating canonical links
*/
finalizePendingLocaleChange: () => Promise<void>
baseUrl: string
/**
* Returns a promise that will be resolved once the pending locale is set.
* Current locale properties.
*/
waitForPendingLocaleChange: () => Promise<void>
localeProperties: LocaleObject
}

loadLocaleMessages: (locale: Locale) => Promise<void>
declare module '#app' {
interface NuxtApp {
/** @internal */
_vueI18n: I18n
}
}

declare module 'vue-i18n' {
Expand Down
15 changes: 12 additions & 3 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
getHost
} from './internal'
import { loadLocale, makeFallbackLocaleCodes } from './messages'
import { localePath, switchLocalePath, DefaultPrefixable } from './routing/routing'
import { localePath, switchLocalePath } from './routing/routing'
import { createLogger } from 'virtual:nuxt-i18n-logger'
import { unref } from 'vue'

Expand Down Expand Up @@ -327,13 +327,22 @@ export async function navigate(
}
}

function prefixable({ currentLocale, defaultLocale, strategy }: PrefixableOptions): boolean {
return (
// strategy has no prefixes
strategy !== 'no_prefix' &&
// strategy should not prefix default locale
!(currentLocale === defaultLocale && (strategy === 'prefix_and_default' || strategy === 'prefix_except_default'))
)
}

// override prefix for route path, support domain
export function extendPrefixable(runtimeConfig = useRuntimeConfig()) {
const logger = /*#__PURE__*/ createLogger('extendPrefixable')
return (opts: PrefixableOptions): boolean => {
__DEBUG__ && logger.log(DefaultPrefixable(opts))
__DEBUG__ && logger.log(prefixable(opts))

return DefaultPrefixable(opts) && !runtimeConfig.public.i18n.differentDomains
return prefixable(opts) && !runtimeConfig.public.i18n.differentDomains
}
}

Expand Down

0 comments on commit 757195a

Please sign in to comment.