-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
Conversation
Hi @kazupon I realised that this hook would be runtime instead of build time like |
initialSetup: boolean | ||
context: Context | ||
} | ||
) => MaybePromise<void> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
) => MaybePromise<void> | |
) => MaybePromise<string | void> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See docs:
https://v8.i18n.nuxtjs.org/guide/callbacks#onbeforelanguageswitch
Returns: string or nothing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of return, it may need to be something like nuxtApp.$i18n.locale = 'en'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work with callHook
it seems!
And these exports on |
What do you think about the logic of the change - any underlying implementation that may have been overlooked? |
I think that your logic is sound. |
One more request π |
Just working on the changes and making sure the tests are passing. What is your opinion on This is a kind of workaround: import { defineNuxtPlugin } from '#imports'
export default defineNuxtPlugin(nuxtApp => {
nuxtApp.hook('i18n:beforeLocaleSwitch', async ({ oldLocale, newLocale, initialSetup }) => {
console.log('onBeforeLanguageSwitch', oldLocale, newLocale, initialSetup)
if (newLocale === 'en') {
await nuxtApp.$i18n.setLocale('fr')
}
})
}) However, the function is async, and Edit: I'm able to await the function here, but without a return value for localeOverride, it may not work. Let me investigate further. const localeOverride = await onBeforeLanguageSwitch(i18n, oldLocale, newLocale, initial, context)
const localeCodes = getLocaleCodes(i18n)
if (localeOverride && localeCodes && localeCodes.includes(localeOverride)) {
if (localeOverride === oldLocale) {
return [ret, oldLocale]
}
newLocale = localeOverride
} Returning locale works! Just needed to await it. |
@ineshbose |
β¦ules#1919) * chore: initial plan * chore: progressing on it * chore: wrapping up * chore: minor polish
β Type of change
π Description
As a step towards stability, the module would also aim for stable code generation and handling that occurs to interact successfully with Nuxt. It's realised that passing functions through the module configuration may not be ideal, and hence this PR aims to use runtime hooks provided by Nuxt for such purposes.
π Checklist