sergiodxa / remix-i18next

The easiest way to translate your Remix apps
https://sergiodxa.github.io/remix-i18next/
MIT License
618 stars 44 forks source link

6.2.0 broke typed locale files #202

Closed stevensacks closed 3 months ago

stevensacks commented 3 months ago

Describe the bug

Something in remix-i18next 6.2.0 broke something with typescript typed language files and now keys in the t function returned by i18next.getFixedT() throw TS errors if you don't include keyPrefix in the getFixedT() options. It's still working at runtime, the keys are returning their values. But, TypeScript complains inside the t() function. Adding the keyPrefix option makes the TS error go away.

TS Error no keyPrefix causes typescript error

With KeyPrefix only way to resolve typescript error


There is also a TS error if you do not include the namespace(s) param to getFixedT() and instead pass the ns as an option in t(), where the keyPrefix option is not available. I have not figured out a way to resolve this one. Again, the string values are still returned correctly at runtime, but TypeScript has an error.

ns option also has error

Your Example Website or App

https://stackblitz.com/edit/remix-run-remix-qazht2

Steps to Reproduce the Bug or Issue

Stackblitz unfortunately appears to not read in *.d.ts files in the project, so you cannot reproduce this issue in stackblitz because it doesn't apply any typing to i18next keys at all. Everything else in the project is set up in Stackblitz.

Adding typing is as simple as adding a type definition file i18next.d.ts to your project, as per their documentation.

import type resources from '~/languages/en';

declare module 'i18next' {
  interface CustomTypeOptions {
    resources: typeof resources;
  }
}

This works as expected outside of Stackblitz. I couldn't figure out how to get it to work in Stackblitz. If you know how, you should be able to turn that on and reproduce the issue.

Expected behavior

Prior to 6.2, the types were correctly identifying the namespaces and key paths. Now, they're broken as described above.

Screenshots or Videos

No response

Platform

Additional context

Rolling back to remix-i18next v6.1.1 fixes the issue.

sergiodxa commented 3 months ago

Could you check if it's fixed in 6.2.1?

davidesigner commented 3 months ago

Hi @sergiodxa,

Same error here with 6.2.1 (only with the getFixedT method, the useTranslation method is working as before):

Screenshot 2024-08-06 at 23 21 42

I can "solve" the problem by defining the namespace as the second argument to the t function (but this is not the intended use):

Screenshot 2024-08-06 at 23 23 18

N.B. I have the same typing definition as @stevensacks:

import { type resources, type defaultNS } from '~/app/locales/i18n.ts';

declare module 'i18next' {
  interface CustomTypeOptions {
    defaultNS: typeof defaultNS;
    resources: typeof resources.fr;
  }
}

And translations that do not have dynamic parameters also have an error:

Screenshot 2024-08-06 at 23 29 34
sergiodxa commented 3 months ago

I added typed namespaces to the getFixedT tests and confirmed that now it should work in v6.3.0

davidesigner commented 3 months ago

I confirm that is working well, thanks a lot for reactivity!