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

feat: generate Locale type based on configuration #3025

Merged
merged 6 commits into from
Jul 16, 2024

Conversation

BobbieGoede
Copy link
Collaborator

@BobbieGoede BobbieGoede commented Jul 12, 2024

🔗 Linked issue

❓ Type of change

  • 📖 Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 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)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This changes type generation to narrow the Locale type to the configured locales, this way functions that accept a locale parameter (such as our composables) will have proper autocompletion for all configured locales.

@kazupon
There's still an issue/quirk with the type, I'm don't know how to narrow the locale property on the Composer type in the same way, so currently the following would cause a type error:

const { locale } = useI18n()
const switchLocalePath = useSwitchLocalePath()

switchLocalePath(locale.value) // Argument of type 'string' is not assignable to parameter of type 'Locale'.

Maybe you know how to resolve this? I don't think this should be merged while this issue is present 😅

Also working on this BobbieGoede#49.

📝 Checklist

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

@BobbieGoede BobbieGoede requested a review from kazupon July 12, 2024 17:14
@BobbieGoede BobbieGoede self-assigned this Jul 12, 2024
@kazupon
Copy link
Collaborator

kazupon commented Jul 14, 2024

@BobbieGoede
Hmm, 🤔
The locale of the current Compose is resolved by the type parameter as locale: WritableComputedRef<Locales>.

https://github.com/intlify/vue-i18n/blob/c03fec09f9c3f9f16f8773295523a8d693e9ce0b/packages/vue-i18n-core/src/composer.ts#L1268-L1296

To support nuxt-i18n use cases, this could be extended more on the Vue I18n side.

e.g. locale: IsNever<CustomLocales> extends true ? WritableComputedRef<Locales> : WritableComputedRef<CustomLocale> .

It may be possible to dynamically extend the locale.

@BobbieGoede
Copy link
Collaborator Author

@kazupon
I have a possible solution here intlify/vue-i18n#1890, I have updated this PR to make use of that solution. Let me know if this works for you locally or if a different approach is preferred.

@BobbieGoede BobbieGoede marked this pull request as ready for review July 16, 2024 06:07
Copy link
Collaborator

@kazupon kazupon left a comment

Choose a reason for hiding this comment

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

Okay, I could work it with vue-i18n v10 latest version!

@kazupon kazupon merged commit 9aa7171 into nuxt-modules:main Jul 16, 2024
7 checks passed
@BobbieGoede BobbieGoede mentioned this pull request Jul 17, 2024
4 tasks
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