i18next / next-i18next

The easiest way to translate your NextJs apps.
https://next.i18next.com
MIT License
5.57k stars 761 forks source link

Typescript - serverSideTranslations locale type shall accept string | undefined #1078

Closed njarraud closed 3 years ago

njarraud commented 3 years ago

Is your feature request related to a problem? Please describe.

The current code throws a typescript error for locale:

export const getStaticProps: GetStaticProps = async ({ locale }) => ({
  props: {
    ...(await serverSideTranslations(locale, ['common'])),
  },
});

GetStaticProps locale has the type string or undefined but serverSideTranslations currently only accepts string.

Describe the solution you'd like

Maybe it would be possible to update serverSideTranslations so that if locale is undefined then it uses the default local provided in the config file.

[next-i18next v.8.1.2]

isaachinman commented 3 years ago

@njarraud The locale argument cannot be undefined – the absence of locale doesn't mean we are in the default locale, it means that NextJs i18n is not enabled.

njarraud commented 3 years ago

You are very quick at closing posts...

Did you even try with your simple example? You will see that it is not valid:

image
isaachinman commented 3 years ago

Hi @njarraud – I understand that NextJs has typed locale as string | undefined, however, this package does not accept locale as undefined.

Feel free to use locale as string, or whatever you'd like!

yovanoc commented 3 years ago

Yes you're right but when creating a next lib we have to follow their rules. If you want just a

if (!locale) throw new Error...

in the serverSideTranslations function but it's a pain in the ass to do a global refactor to our codebase to replace all serverSideTranslations ..

isaachinman commented 3 years ago

@yovanoc Sure, that seems like an easy and useful PR.