-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Type-safe translations do not work with plurals #1386
Comments
Was this supported with i18next json format v3 in TypeScript? |
I haven't used v3, but it should work, as singular value has no suffix: {
"keyPluralSimple": "the singular",
"keyPluralSimple_plural": "the plural"
} |
I don't think we support plurals for |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@pedrodurek @adrai types in @@ -34,14 +34,15 @@ export interface Resources {}
* import 'react-i18next';
* declare module 'react-i18next' {
* interface CustomTypeOptions {
- * defaultNS: 'custom';
+ * defaultNS: 'custom',
* returnNull: false,
* returnEmptyString: false,
* resources: {
* custom: {
- * foo: 'foo';
- * };
- * };
+ * foo: 'foo',
+ * },
+ * },
+ * jsonFormat: 'v4',
* }
* }
* ```
@@ -56,6 +57,7 @@ type TypeOptions = MergeBy<
returnEmptyString: true;
defaultNS: 'translation';
resources: Resources;
+ jsonFormat: 'v4',
},
CustomTypeOptions
>;
@@ -92,16 +94,24 @@ declare module 'i18next' {
}
// Normalize single namespace
+type WithOrWithoutPlural<K> =
+ TypeOptions['jsonFormat'] extends 'v4' ?
+ K extends unknown ?
+ K extends `${infer B}_${'zero' | 'one' | 'two' | 'few' | 'many' | 'other'}` ? B | K : K
+ : never
+ :
+ K
+;
type AppendKeys<K1, K2> = `${K1 & string}.${K2 & string}`;
type AppendKeys2<K1, K2> = `${K1 & string}.${Exclude<K2, keyof any[]> & string}`;
type Normalize2<T, K = keyof T> = K extends keyof T
? T[K] extends Record<string, any>
? T[K] extends readonly any[]
- ? AppendKeys2<K, keyof T[K]> | AppendKeys2<K, Normalize2<T[K]>>
- : AppendKeys<K, keyof T[K]> | AppendKeys<K, Normalize2<T[K]>>
+ ? AppendKeys2<K, WithOrWithoutPlural<keyof T[K]>> | AppendKeys2<K, Normalize2<T[K]>>
+ : AppendKeys<K, WithOrWithoutPlural<keyof T[K]>> | AppendKeys<K, Normalize2<T[K]>>
: never
: never;
-type Normalize<T> = keyof T | Normalize2<T>;
+type Normalize<T> = WithOrWithoutPlural<keyof T> | Normalize2<T>;
// Normalize multiple namespaces
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void |
how can I put that in a react app? |
@njacob1001 you can use |
I'll look into it, PR is welcome as well |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
🐛 Bug Report
After setting up TypeScript type definitions I cannot use plurals support:
To Reproduce
Expected behavior
Such resources should be allowed to be used without the suffix.
Your Environment
The text was updated successfully, but these errors were encountered: