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

refactor!: functions in the config to be used through hooks #1919

merged 4 commits into from
Mar 12, 2023

Conversation

ineshbose
Copy link
Collaborator

@ineshbose ineshbose commented Mar 9, 2023

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to 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

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@ineshbose ineshbose changed the title fix!: functions in the config to be used through hooks refactor!: functions in the config to be used through hooks Mar 9, 2023
src/runtime/plugins/i18n.ts Outdated Show resolved Hide resolved
src/module.ts Outdated Show resolved Hide resolved
@ineshbose
Copy link
Collaborator Author

Hi @kazupon

I realised that this hook would be runtime instead of build time like i18n:extend-messages, so I've made changes accordingly, but will require your expertise at this point to thoroughly review and test! (I tried to change callbacks.spec.ts but I believe I'm unable to get the other snapshots passing)

@ineshbose ineshbose marked this pull request as ready for review March 11, 2023 13:23
initialSetup: boolean
context: Context
}
) => MaybePromise<void>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
) => MaybePromise<void>
) => MaybePromise<string | void>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@ineshbose ineshbose Mar 11, 2023

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'

Copy link
Collaborator Author

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!

src/runtime/types.d.ts Outdated Show resolved Hide resolved
src/runtime/types.d.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
@kazupon
Copy link
Collaborator

kazupon commented Mar 11, 2023

And these exports on options.d.ts should be removed too.
https://github.com/nuxt-modules/i18n/blob/next/src/options.d.ts#L40-L42

@ineshbose
Copy link
Collaborator Author

What do you think about the logic of the change - any underlying implementation that may have been overlooked?

@kazupon
Copy link
Collaborator

kazupon commented Mar 11, 2023

I think that your logic is sound.
The callbacks.spec.ts needs to be changed to nuxt hooks, but what you are testing should be the same.

@kazupon
Copy link
Collaborator

kazupon commented Mar 11, 2023

One more request πŸ™
Changing to nuxt hooks is breaking change for nuxt2 apps using v7.
So, we need to explain it in the migration docs.
https://v8.i18n.nuxtjs.org/guide/migrating

@ineshbose
Copy link
Collaborator Author

ineshbose commented Mar 11, 2023

Just working on the changes and making sure the tests are passing.

What is your opinion on beforeLocaleSwitch not being able to return string anymore?

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 callHook is also async so we need to await it somehow in the composer.

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.

src/runtime/utils.ts Outdated Show resolved Hide resolved
src/options.d.ts Outdated Show resolved Hide resolved
@ineshbose ineshbose requested a review from kazupon March 12, 2023 10:04
@kazupon
Copy link
Collaborator

kazupon commented Mar 12, 2023

@ineshbose
Thanks for all your hard work!
I'll merge this PR!

@kazupon kazupon merged commit bd1169e into nuxt-modules:next Mar 12, 2023
DarthGigi pushed a commit to DarthGigi/i18n that referenced this pull request Apr 16, 2024
…ules#1919)

* chore: initial plan

* chore: progressing on it

* chore: wrapping up

* chore: minor polish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants