QuiiBz / next-international

Type-safe internationalization (i18n) for Next.js
https://next-international.vercel.app
MIT License
1.26k stars 59 forks source link

Default locale schema seems to be taken from the first member of the locales array but should be taken from the default locale #186

Closed sshmaxime closed 12 months ago

sshmaxime commented 12 months ago

Title.

QuiiBz commented 12 months ago

Please follow the issue template, it's here for a reason.

Are you talking about the defaultLocale option in createI18nMiddleware?`

sshmaxime commented 12 months ago

Yes. I think the default schema is taken from the first element array in the locales array, rather than being taken from the defaultLocale.

QuiiBz commented 12 months ago

That's not how it works. By default (only if the user hasn't yet visited the website), the locale is resolved from the Accept-Language header. You can override this behavior by using the resolveLocaleFromRequest option if you don't want to use this logic by default.

See the code for the middleware here: https://github.com/QuiiBz/next-international/blob/main/packages/next-international/src/app/middleware/index.ts

sshmaxime commented 12 months ago

I meant from the developer perspective not from the end user. Typescript wise, the t("hello") is typed depending on the locale file of the first element in the locales array and not from the defaultLocale. Does that make more sense ?

QuiiBz commented 12 months ago

Oh okay, I didn't understand correctly. It's because of this line: https://github.com/QuiiBz/next-international/blob/282c84ebe8362be9823adcb3d0a868bcc4c2b65c/packages/international-types/index.ts#L76

It's not possible to get the defaultLocale defined in the middleware from createI18nServer/createI18nClient because these are completely different and not related. The only way would be to introduce a new defaultLocale option inside createI18nServer/createI18nClient that would only be useful for this purpose.

I don't think introducing this option is necessary, since an easier fix would be to move the default locale as the first item in the locales objects.

sshmaxime commented 12 months ago

Oh alright, well fair enough then. Might be worth mentioning it in the docs then. Thanks for the fast replies 🙏