QuiiBz / next-international

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

Invalid typing of useCurrentLocale() #122

Closed roux1max closed 1 year ago

roux1max commented 1 year ago

Hi!

Thanks for the work here. I was just looking at the code and noticed that the createUseCurrentLocale() function in src/app/client has a return type that does not match the returned function (the config parameter is missing). Is it intentional?

If it is, how can we provide a custom segment name?

QuiiBz commented 1 year ago

Thanks, I've just released 0.9.2 which fixes the type bug. You could already pass a config parameter but the type was wrong.

roux1max commented 1 year ago

Awesome! Thanks!

In the same spirit, getStaticParams does not take a config param to change the segment name which means that if you use a segment name that is different than "locale", it won't work.

QuiiBz commented 1 year ago

getStaticParams doesn't need the segment name, as it uses directly the keys of the locales object from createI18nServer:

https://github.com/QuiiBz/next-international/blob/70bb097fe00c60d64231f363a47718217c5fbc4b/packages/next-international/src/app/server/create-get-static-params.ts#L3

roux1max commented 1 year ago

It does, the key returned by each object in the array is 'locale', which will match the path segment as per NextJs documentation.

In order for createGetStaticParams to work, it would need to be changed to something like this:

export function createGetStaticParams<Locales extends ImportedLocales>(locales: Locales) {
  return function getStaticParams(config?: I18nCurrentLocaleConfig) {
    return Object.keys(locales).map(locale => ({
      [config?.segmentName ?? DEFAULT_SEGMENT_NAME]: locale,
    }));
  };
}
QuiiBz commented 1 year ago

Oh I understand what you mean now, sorry. I'll make another fix for it.