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!: functions in the config to be used through hooks #1919

Merged
merged 4 commits into from Mar 12, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/constants.ts
Expand Up @@ -58,8 +58,6 @@ export const DEFAULT_OPTIONS = {
customRoutes: 'page',
pages: {},
skipSettingLocaleOnNavigate: false,
onBeforeLanguageSwitch: () => '',
onLanguageSwitched: () => null,
types: undefined,
debug: false
} as const
Expand Down
7 changes: 7 additions & 0 deletions src/module.ts
Expand Up @@ -284,5 +284,12 @@ declare module '@nuxt/schema' {

interface NuxtHooks {
'i18n:extend-messages': (messages: LocaleMessages<DefineLocaleMessage>[], localeCodes: string[]) => Promise<void>
'i18n:before-lang-switch': <Context = unknown>(
oldLocale: string,
newLocale: string,
initialSetup: boolean,
context: Context
) => string | void
'i18n:lang-switched': (oldLocale: string, newLocale: string) => void
ineshbose marked this conversation as resolved.
Show resolved Hide resolved
}
}
8 changes: 6 additions & 2 deletions src/runtime/plugins/i18n.ts
Expand Up @@ -224,8 +224,12 @@ export default defineNuxtPlugin(async nuxt => {
_getLocaleCookie(nuxt.ssrContext, { ...nuxtI18nOptions.detectBrowserLanguage, localeCodes })
composer.setLocaleCookie = (locale: string) =>
_setLocaleCookie(locale, nuxt.ssrContext, nuxtI18nOptions.detectBrowserLanguage || undefined)
composer.onBeforeLanguageSwitch = nuxtI18nOptions.onBeforeLanguageSwitch
composer.onLanguageSwitched = nuxtI18nOptions.onLanguageSwitched

// @ts-ignore
composer.onBeforeLanguageSwitch = (o, n, i, ctx) => nuxt.callHook('i18n:before-lang-switch', o, n, i, ctx)
ineshbose marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
composer.onLanguageSwitched = (o, n) => nuxt.callHook('i18n:lang-switched', o, n)

composer.finalizePendingLocaleChange = async () => {
if (!i18n.__pendingLocale) {
return
Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Expand Up @@ -47,8 +47,6 @@ export type LanguageSwitchedHandler = (oldLocale: string, newLocale: string) =>

export type NuxtI18nOptions<Context = unknown> = {
differentDomains?: boolean
onBeforeLanguageSwitch?: BeforeLanguageSwitchHandler
onLanguageSwitched?: LanguageSwitchedHandler
detectBrowserLanguage?: DetectBrowserLanguageOptions | false
langDir?: string | null
lazy?: boolean | LazyOptions
Expand Down