react-i18next 11.12.0 → 11.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ### 11.14.2
2
+
3
+ - Add type-safe support to deep keyPrefix [1403](https://github.com/i18next/react-i18next/pull/1403)
4
+
5
+ ### 11.14.1
6
+
7
+ - Rollback [1402](https://github.com/i18next/react-i18next/pull/1402): Remove generics from Trans component to suppress warning issue [1400](https://github.com/i18next/react-i18next/pull/1400)
8
+
9
+ ### 11.14.0
10
+
11
+ - Remove generics from Trans component to suppress warning issue [1400](https://github.com/i18next/react-i18next/pull/1400)
12
+ - Add type support to plurals [1399](https://github.com/i18next/react-i18next/pull/1399)
13
+
14
+ ### 11.13.0
15
+
16
+ - feat(types): add type-safe support to keyPrefix option [1390](https://github.com/i18next/react-i18next/pull/1390)
17
+ - feat(types): allow key separator augmentation [1367](https://github.com/i18next/react-i18next/pull/1367)
18
+
1
19
  ### 11.12.0
2
20
 
3
21
  - feature: add key prefix support to useTranslation hook [1371](https://github.com/i18next/react-i18next/pull/1371)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-i18next",
3
- "version": "11.12.0",
3
+ "version": "11.14.2",
4
4
  "description": "Internationalization for react done right. Using the i18next i18n ecosystem.",
5
5
  "main": "dist/commonjs/index.js",
6
6
  "types": "./index.d.ts",
@@ -67,7 +67,7 @@
67
67
  "eslint-plugin-react": "^7.16.0",
68
68
  "eslint-plugin-testing-library": "^3.10.1",
69
69
  "husky": "^3.0.3",
70
- "i18next": "^20.6.1",
70
+ "i18next": "^21.0.0",
71
71
  "jest": "^24.8.0",
72
72
  "jest-cli": "^24.8.4",
73
73
  "lint-staged": "^8.1.3",
package/ts4.1/index.d.ts CHANGED
@@ -35,8 +35,10 @@ export interface Resources {}
35
35
  * declare module 'react-i18next' {
36
36
  * interface CustomTypeOptions {
37
37
  * defaultNS: 'custom';
38
- * returnNull: false,
39
- * returnEmptyString: false,
38
+ * returnNull: false;
39
+ * returnEmptyString: false;
40
+ * keySeparator: '.';
41
+ * jsonFormat: 'v4';
40
42
  * resources: {
41
43
  * custom: {
42
44
  * foo: 'foo';
@@ -54,7 +56,9 @@ type TypeOptions = MergeBy<
54
56
  {
55
57
  returnNull: true;
56
58
  returnEmptyString: true;
59
+ keySeparator: '.';
57
60
  defaultNS: 'translation';
61
+ jsonFormat: 'v4';
58
62
  resources: Resources;
59
63
  },
60
64
  CustomTypeOptions
@@ -91,17 +95,28 @@ declare module 'i18next' {
91
95
  }
92
96
  }
93
97
 
98
+ type WithOrWithoutPlural<K> = TypeOptions['jsonFormat'] extends 'v4'
99
+ ? K extends `${infer B}_${'zero' | 'one' | 'two' | 'few' | 'many' | 'other'}`
100
+ ? B | K
101
+ : K
102
+ : K;
103
+
94
104
  // Normalize single namespace
95
- type AppendKeys<K1, K2> = `${K1 & string}.${K2 & string}`;
96
- type AppendKeys2<K1, K2> = `${K1 & string}.${Exclude<K2, keyof any[]> & string}`;
105
+ export type KeysWithSeparator<K1, K2, S extends string = TypeOptions['keySeparator']> = `${K1 &
106
+ string}${S}${K2 & string}`;
107
+ type KeysWithSeparator2<K1, K2> = KeysWithSeparator<K1, Exclude<K2, keyof any[]>>;
97
108
  type Normalize2<T, K = keyof T> = K extends keyof T
98
109
  ? T[K] extends Record<string, any>
99
110
  ? T[K] extends readonly any[]
100
- ? AppendKeys2<K, keyof T[K]> | AppendKeys2<K, Normalize2<T[K]>>
101
- : AppendKeys<K, keyof T[K]> | AppendKeys<K, Normalize2<T[K]>>
111
+ ?
112
+ | KeysWithSeparator2<K, WithOrWithoutPlural<keyof T[K]>>
113
+ | KeysWithSeparator2<K, Normalize2<T[K]>>
114
+ :
115
+ | KeysWithSeparator<K, WithOrWithoutPlural<keyof T[K]>>
116
+ | KeysWithSeparator<K, Normalize2<T[K]>>
102
117
  : never
103
118
  : never;
104
- type Normalize<T> = keyof T | Normalize2<T>;
119
+ type Normalize<T> = WithOrWithoutPlural<keyof T> | Normalize2<T>;
105
120
 
106
121
  // Normalize multiple namespaces
107
122
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void
@@ -135,12 +150,18 @@ export type NormalizeByTypeOptions<
135
150
  R = TypeOptionsFallback<TranslationValue, Options['returnEmptyString'], ''>
136
151
  > = TypeOptionsFallback<R, Options['returnNull'], null>;
137
152
 
138
- type NormalizeReturn<T, V> = V extends `${infer K}.${infer R}`
153
+ type NormalizeReturn<
154
+ T,
155
+ V,
156
+ S extends string | false = TypeOptions['keySeparator']
157
+ > = V extends keyof T
158
+ ? NormalizeByTypeOptions<T[V]>
159
+ : S extends false
160
+ ? V
161
+ : V extends `${infer K}${S}${infer R}`
139
162
  ? K extends keyof T
140
163
  ? NormalizeReturn<T[K], R>
141
164
  : never
142
- : V extends keyof T
143
- ? NormalizeByTypeOptions<T[V]>
144
165
  : never;
145
166
 
146
167
  type NormalizeMultiReturn<T, V> = V extends `${infer N}:${infer R}`
@@ -149,43 +170,74 @@ type NormalizeMultiReturn<T, V> = V extends `${infer N}:${infer R}`
149
170
  : never
150
171
  : never;
151
172
 
152
- export type TFuncKey<N extends Namespace = DefaultNamespace, T = DefaultResources> = N extends
153
- | (keyof T)[]
154
- | Readonly<(keyof T)[]>
173
+ type NormalizeWithKeyPrefix<
174
+ T,
175
+ K,
176
+ S extends string = TypeOptions['keySeparator']
177
+ > = K extends `${infer K1}${S}${infer K2}`
178
+ ? K1 extends keyof T
179
+ ? NormalizeWithKeyPrefix<T[K1], K2>
180
+ : never
181
+ : K extends keyof T
182
+ ? T[K] extends string
183
+ ? never
184
+ : Normalize<T[K]>
185
+ : never;
186
+
187
+ type KeyPrefix<N extends Namespace> = N extends keyof DefaultResources
188
+ ? Normalize<DefaultResources[N]> | undefined
189
+ : string | undefined;
190
+
191
+ export type TFuncKey<
192
+ N extends Namespace = DefaultNamespace,
193
+ TKPrefix = undefined,
194
+ T = DefaultResources
195
+ > = N extends (keyof T)[] | Readonly<(keyof T)[]>
155
196
  ? NormalizeMulti<T, N[number]>
156
197
  : N extends keyof T
157
- ? Normalize<T[N]>
198
+ ? TKPrefix extends undefined
199
+ ? Normalize<T[N]>
200
+ : NormalizeWithKeyPrefix<T[N], TKPrefix>
158
201
  : string;
159
202
 
160
- export type TFuncReturn<N, TKeys, TDefaultResult, T = DefaultResources> = N extends (keyof T)[]
203
+ export type TFuncReturn<
204
+ N,
205
+ TKeys,
206
+ TDefaultResult,
207
+ TKPrefix = undefined,
208
+ T = DefaultResources
209
+ > = N extends (keyof T)[]
161
210
  ? NormalizeMultiReturn<T, TKeys>
162
211
  : N extends keyof T
163
- ? NormalizeReturn<T[N], TKeys>
212
+ ? TKPrefix extends undefined
213
+ ? NormalizeReturn<T[N], TKeys>
214
+ : NormalizeReturn<T[N], KeysWithSeparator<TKPrefix, TKeys>>
164
215
  : Fallback<TDefaultResult>;
165
216
 
166
- export interface TFunction<N extends Namespace = DefaultNamespace> {
217
+ export interface TFunction<N extends Namespace = DefaultNamespace, TKPrefix = undefined> {
167
218
  <
168
- TKeys extends TFuncKey<N> | TemplateStringsArray extends infer A ? A : never,
219
+ TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
169
220
  TDefaultResult extends TFunctionResult = string,
170
221
  TInterpolationMap extends object = StringMap
171
222
  >(
172
223
  key: TKeys | TKeys[],
173
224
  options?: TOptions<TInterpolationMap> | string,
174
- ): TFuncReturn<N, TKeys, TDefaultResult>;
225
+ ): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
175
226
  <
176
- TKeys extends TFuncKey<N> | TemplateStringsArray extends infer A ? A : never,
227
+ TKeys extends TFuncKey<N, TKPrefix> | TemplateStringsArray extends infer A ? A : never,
177
228
  TDefaultResult extends TFunctionResult = string,
178
229
  TInterpolationMap extends object = StringMap
179
230
  >(
180
231
  key: TKeys | TKeys[],
181
232
  defaultValue?: string,
182
233
  options?: TOptions<TInterpolationMap> | string,
183
- ): TFuncReturn<N, TKeys, TDefaultResult>;
234
+ ): TFuncReturn<N, TKeys, TDefaultResult, TKPrefix>;
184
235
  }
185
236
 
186
237
  export interface TransProps<
187
- K extends TFuncKey<N> extends infer A ? A : never,
238
+ K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
188
239
  N extends Namespace = DefaultNamespace,
240
+ TKPrefix = undefined,
189
241
  E extends Element = HTMLDivElement
190
242
  > extends React.HTMLProps<E> {
191
243
  children?: React.ReactNode;
@@ -198,32 +250,41 @@ export interface TransProps<
198
250
  parent?: string | React.ComponentType<any> | null; // used in React.createElement if not null
199
251
  tOptions?: {};
200
252
  values?: {};
201
- t?: TFunction<N>;
253
+ t?: TFunction<N, TKPrefix>;
202
254
  }
255
+
203
256
  export function Trans<
204
- K extends TFuncKey<N> extends infer A ? A : never,
257
+ K extends TFuncKey<N, TKPrefix> extends infer A ? A : never,
205
258
  N extends Namespace = DefaultNamespace,
259
+ TKPrefix extends KeyPrefix<N> = undefined,
206
260
  E extends Element = HTMLDivElement
207
- >(props: TransProps<K, N, E>): React.ReactElement;
261
+ >(props: TransProps<K, N, TKPrefix, E>): React.ReactElement;
208
262
 
209
263
  export function useSSR(initialI18nStore: Resource, initialLanguage: string): void;
210
264
 
211
- export interface UseTranslationOptions {
265
+ export interface UseTranslationOptions<TKPrefix = undefined> {
212
266
  i18n?: i18n;
213
267
  useSuspense?: boolean;
214
- keyPrefix?: string;
268
+ keyPrefix?: TKPrefix;
215
269
  }
216
270
 
217
- type UseTranslationResponse<N extends Namespace> = [TFunction<N>, i18n, boolean] & {
218
- t: TFunction<N>;
271
+ type UseTranslationResponse<N extends Namespace, TKPrefix> = [
272
+ TFunction<N, TKPrefix>,
273
+ i18n,
274
+ boolean,
275
+ ] & {
276
+ t: TFunction<N, TKPrefix>;
219
277
  i18n: i18n;
220
278
  ready: boolean;
221
279
  };
222
280
 
223
- export function useTranslation<N extends Namespace = DefaultNamespace>(
281
+ export function useTranslation<
282
+ N extends Namespace = DefaultNamespace,
283
+ TKPrefix extends KeyPrefix<N> = undefined
284
+ >(
224
285
  ns?: N | Readonly<N>,
225
- options?: UseTranslationOptions,
226
- ): UseTranslationResponse<N>;
286
+ options?: UseTranslationOptions<TKPrefix>,
287
+ ): UseTranslationResponse<N, TKPrefix>;
227
288
 
228
289
  // Need to see usage to improve this
229
290
  export function withSSR(): <Props>(