@depup/i18next 25.8.18-depup.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/.nvmrc +1 -0
- package/LICENSE +21 -0
- package/README.md +31 -0
- package/changes.json +10 -0
- package/dist/cjs/i18next.js +2245 -0
- package/dist/esm/i18next.bundled.js +2256 -0
- package/dist/esm/i18next.js +2256 -0
- package/dist/esm/package.json +1 -0
- package/dist/umd/i18next.js +2251 -0
- package/dist/umd/i18next.min.js +1 -0
- package/i18next.js +2251 -0
- package/i18next.min.js +1 -0
- package/index.d.mts +63 -0
- package/index.d.ts +587 -0
- package/index.js +5 -0
- package/jsr.json +15 -0
- package/package.json +142 -0
- package/typescript/helpers.d.ts +77 -0
- package/typescript/options.d.ts +802 -0
- package/typescript/t.d.ts +516 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
$OmitArrayKeys,
|
|
3
|
+
$PreservedValue,
|
|
4
|
+
$Dictionary,
|
|
5
|
+
$SpecialObject,
|
|
6
|
+
$StringKeyPathToRecord,
|
|
7
|
+
$NoInfer,
|
|
8
|
+
$Prune,
|
|
9
|
+
$Turtles,
|
|
10
|
+
} from './helpers.js';
|
|
11
|
+
import type {
|
|
12
|
+
TypeOptions,
|
|
13
|
+
Namespace,
|
|
14
|
+
FlatNamespace,
|
|
15
|
+
DefaultNamespace,
|
|
16
|
+
TOptions,
|
|
17
|
+
TOptionsBase,
|
|
18
|
+
} from './options.js';
|
|
19
|
+
|
|
20
|
+
/** @todo consider to replace {} with Record<string, never> */
|
|
21
|
+
|
|
22
|
+
// Type Options
|
|
23
|
+
type _ReturnObjects = TypeOptions['returnObjects'];
|
|
24
|
+
type _ReturnEmptyString = TypeOptions['returnEmptyString'];
|
|
25
|
+
type _ReturnNull = TypeOptions['returnNull'];
|
|
26
|
+
type _KeySeparator = TypeOptions['keySeparator'];
|
|
27
|
+
type _NsSeparator = TypeOptions['nsSeparator'];
|
|
28
|
+
type _PluralSeparator = TypeOptions['pluralSeparator'];
|
|
29
|
+
type _ContextSeparator = TypeOptions['contextSeparator'];
|
|
30
|
+
type _FallbackNamespace = TypeOptions['fallbackNS'];
|
|
31
|
+
type _Resources = TypeOptions['resources'];
|
|
32
|
+
type _CompatibilityJSON = TypeOptions['compatibilityJSON'];
|
|
33
|
+
type _InterpolationPrefix = TypeOptions['interpolationPrefix'];
|
|
34
|
+
type _InterpolationSuffix = TypeOptions['interpolationSuffix'];
|
|
35
|
+
type _UnescapePrefix = TypeOptions['unescapePrefix'];
|
|
36
|
+
type _UnescapeSuffix = TypeOptions['unescapeSuffix'];
|
|
37
|
+
type _StrictKeyChecks = TypeOptions['strictKeyChecks'];
|
|
38
|
+
type _EnableSelector = TypeOptions['enableSelector'];
|
|
39
|
+
|
|
40
|
+
type $IsResourcesDefined = [keyof _Resources] extends [never] ? false : true;
|
|
41
|
+
type $ValueIfResourcesDefined<Value, Fallback> = $IsResourcesDefined extends true
|
|
42
|
+
? Value
|
|
43
|
+
: Fallback;
|
|
44
|
+
type $FirstNamespace<Ns extends Namespace> = Ns extends readonly any[] ? Ns[0] : Ns;
|
|
45
|
+
|
|
46
|
+
type Resources = $ValueIfResourcesDefined<_Resources, $Dictionary<string>>;
|
|
47
|
+
|
|
48
|
+
type PluralSuffix = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
|
|
49
|
+
|
|
50
|
+
type WithOrWithoutPlural<Key> = _CompatibilityJSON extends 'v4'
|
|
51
|
+
? Key extends `${infer KeyWithoutOrdinalPlural}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
52
|
+
? KeyWithoutOrdinalPlural | Key
|
|
53
|
+
: Key extends `${infer KeyWithoutPlural}${_PluralSeparator}${PluralSuffix}`
|
|
54
|
+
? KeyWithoutPlural | Key
|
|
55
|
+
: Key
|
|
56
|
+
: Key;
|
|
57
|
+
|
|
58
|
+
type JoinKeys<K1, K2> = `${K1 & string}${_KeySeparator}${K2 & string}`;
|
|
59
|
+
type AppendNamespace<Ns, Keys> = `${Ns & string}${_NsSeparator}${Keys & string}`;
|
|
60
|
+
|
|
61
|
+
type TrimSpaces<T extends string, Acc extends string = ''> = T extends `${infer Char}${infer Rest}`
|
|
62
|
+
? Char extends ' '
|
|
63
|
+
? TrimSpaces<Rest, Acc>
|
|
64
|
+
: TrimSpaces<Rest, `${Acc}${Char}`>
|
|
65
|
+
: T extends ''
|
|
66
|
+
? Acc
|
|
67
|
+
: never;
|
|
68
|
+
|
|
69
|
+
interface Branded<Ns extends Namespace> {
|
|
70
|
+
$TFunctionBrand: $IsResourcesDefined extends true
|
|
71
|
+
? `${Ns extends readonly any[] ? Ns[0] : Ns}`
|
|
72
|
+
: never;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** ****************************************************
|
|
76
|
+
* Build all keys and key prefixes based on Resources *
|
|
77
|
+
***************************************************** */
|
|
78
|
+
type KeysBuilderWithReturnObjects<Res, Key = keyof Res> = Key extends keyof Res
|
|
79
|
+
? Res[Key] extends $Dictionary | readonly unknown[]
|
|
80
|
+
?
|
|
81
|
+
| JoinKeys<Key, WithOrWithoutPlural<keyof $OmitArrayKeys<Res[Key]>>>
|
|
82
|
+
| JoinKeys<Key, KeysBuilderWithReturnObjects<Res[Key]>>
|
|
83
|
+
: never
|
|
84
|
+
: never;
|
|
85
|
+
|
|
86
|
+
type KeysBuilderWithoutReturnObjects<Res, Key = keyof $OmitArrayKeys<Res>> = Key extends keyof Res
|
|
87
|
+
? Res[Key] extends $Dictionary | readonly unknown[]
|
|
88
|
+
? JoinKeys<Key, KeysBuilderWithoutReturnObjects<Res[Key]>>
|
|
89
|
+
: Key
|
|
90
|
+
: never;
|
|
91
|
+
|
|
92
|
+
type KeysBuilder<Res, WithReturnObjects> = $IsResourcesDefined extends true
|
|
93
|
+
? WithReturnObjects extends true
|
|
94
|
+
? keyof Res | KeysBuilderWithReturnObjects<Res>
|
|
95
|
+
: KeysBuilderWithoutReturnObjects<Res>
|
|
96
|
+
: string;
|
|
97
|
+
|
|
98
|
+
type KeysWithReturnObjects = {
|
|
99
|
+
[Ns in FlatNamespace]: WithOrWithoutPlural<KeysBuilder<Resources[Ns], true>>;
|
|
100
|
+
};
|
|
101
|
+
type KeysWithoutReturnObjects = {
|
|
102
|
+
[Ns in FlatNamespace]: WithOrWithoutPlural<KeysBuilder<Resources[Ns], false>>;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
type ResourceKeys<WithReturnObjects = _ReturnObjects> = WithReturnObjects extends true
|
|
106
|
+
? KeysWithReturnObjects
|
|
107
|
+
: KeysWithoutReturnObjects;
|
|
108
|
+
|
|
109
|
+
/** **********************************************************************
|
|
110
|
+
* Parse t function keys based on the namespace, options and key prefix *
|
|
111
|
+
*********************************************************************** */
|
|
112
|
+
export type KeysByTOptions<TOpt extends TOptions> = TOpt['returnObjects'] extends true
|
|
113
|
+
? ResourceKeys<true>
|
|
114
|
+
: ResourceKeys;
|
|
115
|
+
|
|
116
|
+
export type NsByTOptions<Ns extends Namespace, TOpt extends TOptions> = TOpt['ns'] extends Namespace
|
|
117
|
+
? TOpt['ns']
|
|
118
|
+
: Ns;
|
|
119
|
+
|
|
120
|
+
type ParseKeysByKeyPrefix<Keys, KPrefix> = KPrefix extends string
|
|
121
|
+
? Keys extends `${KPrefix}${_KeySeparator}${infer Key}`
|
|
122
|
+
? Key
|
|
123
|
+
: never
|
|
124
|
+
: Keys;
|
|
125
|
+
|
|
126
|
+
type ParseKeysByNamespaces<Ns extends Namespace, Keys> = Ns extends readonly (infer UnionNsps)[]
|
|
127
|
+
? UnionNsps extends keyof Keys
|
|
128
|
+
? AppendNamespace<UnionNsps, Keys[UnionNsps]>
|
|
129
|
+
: never
|
|
130
|
+
: never;
|
|
131
|
+
|
|
132
|
+
type ParseKeysByFallbackNs<Keys extends $Dictionary> = _FallbackNamespace extends false
|
|
133
|
+
? never
|
|
134
|
+
: _FallbackNamespace extends (infer UnionFallbackNs extends string)[]
|
|
135
|
+
? Keys[UnionFallbackNs]
|
|
136
|
+
: Keys[_FallbackNamespace & string];
|
|
137
|
+
|
|
138
|
+
export type FilterKeysByContext<Keys, Context> = Context extends string
|
|
139
|
+
? Keys extends
|
|
140
|
+
| `${infer Prefix}${_ContextSeparator}${Context}${_PluralSeparator}${PluralSuffix}`
|
|
141
|
+
| `${infer Prefix}${_ContextSeparator}${Context}`
|
|
142
|
+
? Prefix
|
|
143
|
+
: never
|
|
144
|
+
: Keys;
|
|
145
|
+
|
|
146
|
+
export type ParseKeys<
|
|
147
|
+
Ns extends Namespace = DefaultNamespace,
|
|
148
|
+
TOpt extends TOptions = {},
|
|
149
|
+
KPrefix = undefined,
|
|
150
|
+
Keys extends $Dictionary = KeysByTOptions<TOpt>,
|
|
151
|
+
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
|
|
152
|
+
Context extends TOpt['context'] = TOpt['context'],
|
|
153
|
+
> = $IsResourcesDefined extends true
|
|
154
|
+
? FilterKeysByContext<
|
|
155
|
+
| ParseKeysByKeyPrefix<Keys[$FirstNamespace<ActualNS>], KPrefix>
|
|
156
|
+
| ParseKeysByNamespaces<ActualNS, Keys>
|
|
157
|
+
| ParseKeysByFallbackNs<Keys>,
|
|
158
|
+
Context
|
|
159
|
+
>
|
|
160
|
+
: string;
|
|
161
|
+
|
|
162
|
+
/** *******************************************************
|
|
163
|
+
* Parse t function return type and interpolation values *
|
|
164
|
+
******************************************************** */
|
|
165
|
+
type ParseActualValue<Ret> = Ret extends `${_UnescapePrefix}${infer ActualValue}${_UnescapeSuffix}`
|
|
166
|
+
? TrimSpaces<ActualValue>
|
|
167
|
+
: Ret;
|
|
168
|
+
|
|
169
|
+
type ParseInterpolationValues<Ret> =
|
|
170
|
+
Ret extends `${string}${_InterpolationPrefix}${infer Value}${_InterpolationSuffix}${infer Rest}`
|
|
171
|
+
?
|
|
172
|
+
| (Value extends `${infer ActualValue},${string}`
|
|
173
|
+
? ParseActualValue<ActualValue>
|
|
174
|
+
: ParseActualValue<Value>)
|
|
175
|
+
| ParseInterpolationValues<Rest>
|
|
176
|
+
: never;
|
|
177
|
+
|
|
178
|
+
export type InterpolationMap<Ret> = $PreservedValue<
|
|
179
|
+
$StringKeyPathToRecord<ParseInterpolationValues<Ret>, unknown>,
|
|
180
|
+
Record<string, unknown>
|
|
181
|
+
>;
|
|
182
|
+
|
|
183
|
+
type ParseTReturnPlural<
|
|
184
|
+
Res,
|
|
185
|
+
Key,
|
|
186
|
+
KeyWithPlural = `${Key & string}${_PluralSeparator}${PluralSuffix}`,
|
|
187
|
+
> = Res[(KeyWithPlural | Key) & keyof Res];
|
|
188
|
+
|
|
189
|
+
type ParseTReturnPluralOrdinal<
|
|
190
|
+
Res,
|
|
191
|
+
Key,
|
|
192
|
+
KeyWithOrdinalPlural = `${Key &
|
|
193
|
+
string}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`,
|
|
194
|
+
> = Res[(KeyWithOrdinalPlural | Key) & keyof Res];
|
|
195
|
+
|
|
196
|
+
type ParseTReturnWithFallback<Key, Val> = Val extends ''
|
|
197
|
+
? _ReturnEmptyString extends true
|
|
198
|
+
? ''
|
|
199
|
+
: Key
|
|
200
|
+
: Val extends null
|
|
201
|
+
? _ReturnNull extends true
|
|
202
|
+
? null
|
|
203
|
+
: Key
|
|
204
|
+
: Val;
|
|
205
|
+
|
|
206
|
+
type ParseTReturn<Key, Res, TOpt extends TOptions = {}> = ParseTReturnWithFallback<
|
|
207
|
+
Key,
|
|
208
|
+
Key extends `${infer K1}${_KeySeparator}${infer RestKey}`
|
|
209
|
+
? ParseTReturn<RestKey, Res[K1 & keyof Res], TOpt>
|
|
210
|
+
: // Process plurals only if count is provided inside options
|
|
211
|
+
TOpt['count'] extends number
|
|
212
|
+
? TOpt['ordinal'] extends boolean
|
|
213
|
+
? ParseTReturnPluralOrdinal<Res, Key>
|
|
214
|
+
: ParseTReturnPlural<Res, Key>
|
|
215
|
+
: // otherwise access plain key without adding plural and ordinal suffixes
|
|
216
|
+
Res extends readonly unknown[]
|
|
217
|
+
? Key extends `${infer NKey extends number}`
|
|
218
|
+
? Res[NKey]
|
|
219
|
+
: never
|
|
220
|
+
: Res[Key & keyof Res]
|
|
221
|
+
>;
|
|
222
|
+
|
|
223
|
+
type TReturnOptionalNull = _ReturnNull extends true ? null : never;
|
|
224
|
+
type TReturnOptionalObjects<TOpt extends { returnObjects?: unknown }> = _ReturnObjects extends true
|
|
225
|
+
? $SpecialObject | string
|
|
226
|
+
: TOpt['returnObjects'] extends true
|
|
227
|
+
? $SpecialObject
|
|
228
|
+
: string;
|
|
229
|
+
type DefaultTReturn<TOpt extends { returnObjects?: unknown }> =
|
|
230
|
+
| TReturnOptionalObjects<TOpt>
|
|
231
|
+
| TReturnOptionalNull;
|
|
232
|
+
|
|
233
|
+
export type KeyWithContext<Key, TOpt extends TOptions> = TOpt['context'] extends string
|
|
234
|
+
? `${Key & string}${_ContextSeparator}${TOpt['context']}`
|
|
235
|
+
: Key;
|
|
236
|
+
|
|
237
|
+
// helper that maps the configured fallbackNS value to the matching resources slice
|
|
238
|
+
type FallbackResourcesOf<FallbackNS, R> = FallbackNS extends readonly (infer FN)[]
|
|
239
|
+
? R[FN & keyof R]
|
|
240
|
+
: [FallbackNS] extends [false]
|
|
241
|
+
? never
|
|
242
|
+
: R[Extract<FallbackNS, keyof R> & keyof R];
|
|
243
|
+
|
|
244
|
+
/* reuse the parse helpers as top-level aliases (no nested type declarations) */
|
|
245
|
+
type _PrimaryParse<
|
|
246
|
+
ActualKey,
|
|
247
|
+
PrimaryNS extends keyof Resources,
|
|
248
|
+
TOpt extends TOptions,
|
|
249
|
+
> = ParseTReturn<ActualKey, Resources[PrimaryNS], TOpt>;
|
|
250
|
+
|
|
251
|
+
type _FallbackParse<ActualKey, FallbackNS, TOpt extends TOptions> = [
|
|
252
|
+
FallbackResourcesOf<FallbackNS, Resources>,
|
|
253
|
+
] extends [never]
|
|
254
|
+
? never
|
|
255
|
+
: ParseTReturn<ActualKey, FallbackResourcesOf<FallbackNS, Resources>, TOpt>;
|
|
256
|
+
|
|
257
|
+
export type TFunctionReturn<
|
|
258
|
+
Ns extends Namespace,
|
|
259
|
+
Key,
|
|
260
|
+
TOpt extends TOptions,
|
|
261
|
+
ActualNS extends Namespace = NsByTOptions<Ns, TOpt>,
|
|
262
|
+
ActualKey = KeyWithContext<Key, TOpt>,
|
|
263
|
+
> = $IsResourcesDefined extends true
|
|
264
|
+
? ActualKey extends `${infer Nsp}${_NsSeparator}${infer RestKey}`
|
|
265
|
+
? ParseTReturn<RestKey, Resources[Nsp & keyof Resources], TOpt>
|
|
266
|
+
: $FirstNamespace<ActualNS> extends infer PrimaryNS
|
|
267
|
+
? [PrimaryNS] extends [keyof Resources]
|
|
268
|
+
? [_PrimaryParse<ActualKey, PrimaryNS & keyof Resources, TOpt>] extends [never]
|
|
269
|
+
? [_FallbackParse<ActualKey, _FallbackNamespace, TOpt>] extends [never]
|
|
270
|
+
? DefaultTReturn<TOpt>
|
|
271
|
+
: _FallbackParse<ActualKey, _FallbackNamespace, TOpt>
|
|
272
|
+
: _PrimaryParse<ActualKey, PrimaryNS & keyof Resources, TOpt>
|
|
273
|
+
: never
|
|
274
|
+
: never
|
|
275
|
+
: DefaultTReturn<TOpt>;
|
|
276
|
+
|
|
277
|
+
export type TFunctionDetailedResult<T = string, TOpt extends TOptions = {}> = {
|
|
278
|
+
/**
|
|
279
|
+
* The plain used key
|
|
280
|
+
*/
|
|
281
|
+
usedKey: string;
|
|
282
|
+
/**
|
|
283
|
+
* The translation result.
|
|
284
|
+
*/
|
|
285
|
+
res: T;
|
|
286
|
+
/**
|
|
287
|
+
* The key with context / plural
|
|
288
|
+
*/
|
|
289
|
+
exactUsedKey: string;
|
|
290
|
+
/**
|
|
291
|
+
* The used language for this translation.
|
|
292
|
+
*/
|
|
293
|
+
usedLng: string;
|
|
294
|
+
/**
|
|
295
|
+
* The used namespace for this translation.
|
|
296
|
+
*/
|
|
297
|
+
usedNS: string;
|
|
298
|
+
/**
|
|
299
|
+
* The parameters used for interpolation.
|
|
300
|
+
*/
|
|
301
|
+
usedParams: InterpolationMap<T> & { count?: TOpt['count'] };
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
type TFunctionProcessReturnValue<Ret, DefaultValue> = Ret extends string | $SpecialObject | null
|
|
305
|
+
? Ret
|
|
306
|
+
: [DefaultValue] extends [never]
|
|
307
|
+
? Ret
|
|
308
|
+
: DefaultValue;
|
|
309
|
+
|
|
310
|
+
type TFunctionReturnOptionalDetails<Ret, TOpt extends TOptions> = TOpt['returnDetails'] extends true
|
|
311
|
+
? TFunctionDetailedResult<Ret, TOpt>
|
|
312
|
+
: Ret;
|
|
313
|
+
|
|
314
|
+
type AppendKeyPrefix<Key, KPrefix> = KPrefix extends string
|
|
315
|
+
? `${KPrefix}${_KeySeparator}${Key & string}`
|
|
316
|
+
: Key;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Resolves the effective key prefix by preferring a per-call `keyPrefix` from
|
|
320
|
+
* options over the interface-level `KPrefix` (set via getFixedT's 3rd argument).
|
|
321
|
+
*/
|
|
322
|
+
type EffectiveKPrefix<KPrefix, TOpt> = TOpt extends { keyPrefix: infer OptKP extends string }
|
|
323
|
+
? OptKP
|
|
324
|
+
: KPrefix;
|
|
325
|
+
|
|
326
|
+
/** ************************
|
|
327
|
+
* T function declaration *
|
|
328
|
+
************************* */
|
|
329
|
+
|
|
330
|
+
interface TFunctionStrict<
|
|
331
|
+
Ns extends Namespace = DefaultNamespace,
|
|
332
|
+
KPrefix = undefined,
|
|
333
|
+
> extends Branded<Ns> {
|
|
334
|
+
<
|
|
335
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
336
|
+
const TOpt extends TOptions,
|
|
337
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
338
|
+
>(
|
|
339
|
+
key: Key | Key[],
|
|
340
|
+
options?: TOpt & InterpolationMap<Ret>,
|
|
341
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
342
|
+
<
|
|
343
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
344
|
+
const TOpt extends TOptions,
|
|
345
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
346
|
+
>(
|
|
347
|
+
key: Key | Key[],
|
|
348
|
+
defaultValue: string,
|
|
349
|
+
options?: TOpt & InterpolationMap<Ret>,
|
|
350
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, never>, TOpt>;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
interface TFunctionNonStrict<
|
|
354
|
+
Ns extends Namespace = DefaultNamespace,
|
|
355
|
+
KPrefix = undefined,
|
|
356
|
+
> extends Branded<Ns> {
|
|
357
|
+
<
|
|
358
|
+
const Key extends ParseKeys<Ns, TOpt, EffectiveKPrefix<KPrefix, TOpt>> | TemplateStringsArray,
|
|
359
|
+
const TOpt extends TOptions,
|
|
360
|
+
Ret extends TFunctionReturn<Ns, AppendKeyPrefix<Key, EffectiveKPrefix<KPrefix, TOpt>>, TOpt>,
|
|
361
|
+
const ActualOptions extends TOpt & InterpolationMap<Ret> = TOpt & InterpolationMap<Ret>,
|
|
362
|
+
DefaultValue extends string = never,
|
|
363
|
+
>(
|
|
364
|
+
...args:
|
|
365
|
+
| [key: Key | Key[], options?: ActualOptions]
|
|
366
|
+
| [key: string | string[], options: TOpt & $Dictionary & { defaultValue: DefaultValue }]
|
|
367
|
+
| [key: string | string[], defaultValue: DefaultValue, options?: TOpt & $Dictionary]
|
|
368
|
+
): TFunctionReturnOptionalDetails<TFunctionProcessReturnValue<$NoInfer<Ret>, DefaultValue>, TOpt>;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
type TFunctionSignature<
|
|
372
|
+
Ns extends Namespace = DefaultNamespace,
|
|
373
|
+
KPrefix = undefined,
|
|
374
|
+
> = _EnableSelector extends true | 'optimize'
|
|
375
|
+
? TFunctionSelector<Ns, KPrefix, GetSource<Ns, KPrefix>>
|
|
376
|
+
: _StrictKeyChecks extends true
|
|
377
|
+
? TFunctionStrict<Ns, KPrefix>
|
|
378
|
+
: TFunctionNonStrict<Ns, KPrefix>;
|
|
379
|
+
|
|
380
|
+
export interface TFunction<
|
|
381
|
+
Ns extends Namespace = DefaultNamespace,
|
|
382
|
+
KPrefix = undefined,
|
|
383
|
+
> extends TFunctionSignature<Ns, KPrefix> {}
|
|
384
|
+
|
|
385
|
+
export type KeyPrefix<Ns extends Namespace> = ResourceKeys<true>[$FirstNamespace<Ns>] | undefined;
|
|
386
|
+
|
|
387
|
+
/// ////////////// ///
|
|
388
|
+
/// ↆ selector ↆ ///
|
|
389
|
+
/// ////////////// ///
|
|
390
|
+
|
|
391
|
+
type NsArg<Ns extends Namespace> = Ns[number] | readonly Ns[number][];
|
|
392
|
+
|
|
393
|
+
interface TFunctionSelector<Ns extends Namespace, KPrefix, Source> extends Branded<Ns> {
|
|
394
|
+
<
|
|
395
|
+
Target extends ConstrainTarget<Opts>,
|
|
396
|
+
const NewNs extends NsArg<Ns> & Namespace,
|
|
397
|
+
const Opts extends SelectorOptions<NewNs>,
|
|
398
|
+
NewSrc extends GetSource<NewNs, KPrefix>,
|
|
399
|
+
>(
|
|
400
|
+
selector:
|
|
401
|
+
| SelectorFn<NewSrc, ApplyTarget<Target, Opts>, Opts>
|
|
402
|
+
| readonly SelectorFn<NewSrc, ApplyTarget<Target, Opts>, Opts>[],
|
|
403
|
+
options: Opts & InterpolationMap<Target> & { ns: NewNs },
|
|
404
|
+
): SelectorReturn<Target, Opts>;
|
|
405
|
+
|
|
406
|
+
<
|
|
407
|
+
Target extends ConstrainTarget<Opts>,
|
|
408
|
+
const NewNs extends NsArg<Ns> = Ns[number],
|
|
409
|
+
const Opts extends SelectorOptions<NewNs> = SelectorOptions<NewNs>,
|
|
410
|
+
>(
|
|
411
|
+
selector:
|
|
412
|
+
| SelectorFn<Source, ApplyTarget<Target, Opts>, Opts>
|
|
413
|
+
| readonly SelectorFn<Source, ApplyTarget<Target, Opts>, Opts>[],
|
|
414
|
+
options?: Opts & InterpolationMap<Target>,
|
|
415
|
+
): SelectorReturn<Target, Opts>;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
interface SelectorOptions<Ns = Namespace>
|
|
419
|
+
extends Omit<TOptionsBase, 'ns' | 'nsSeparator'>, $Dictionary {
|
|
420
|
+
ns?: Ns;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
type SelectorReturn<
|
|
424
|
+
Target,
|
|
425
|
+
Opts extends { defaultValue?: unknown; returnObjects?: boolean },
|
|
426
|
+
> = $IsResourcesDefined extends true
|
|
427
|
+
? TFunctionReturnOptionalDetails<ProcessReturnValue<Target, Opts['defaultValue']>, Opts>
|
|
428
|
+
: DefaultTReturn<Opts>;
|
|
429
|
+
|
|
430
|
+
interface SelectorFn<Source, Target, Opts extends SelectorOptions<unknown>> {
|
|
431
|
+
(translations: Select<Source, Opts['context']>): Target;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
type ApplyKeyPrefix<
|
|
435
|
+
T extends [any],
|
|
436
|
+
KPrefix,
|
|
437
|
+
> = KPrefix extends `${infer Head}${_KeySeparator}${infer Tail}`
|
|
438
|
+
? ApplyKeyPrefix<[T[0][Head]], Tail>
|
|
439
|
+
: T[0][KPrefix & string];
|
|
440
|
+
|
|
441
|
+
type ApplyTarget<
|
|
442
|
+
Target,
|
|
443
|
+
Opts extends { returnObjects?: unknown },
|
|
444
|
+
> = Opts['returnObjects'] extends true ? unknown : Target;
|
|
445
|
+
|
|
446
|
+
type ConstrainTarget<Opts extends SelectorOptions<any>> = _ReturnObjects extends true
|
|
447
|
+
? unknown
|
|
448
|
+
: Opts['returnObjects'] extends true
|
|
449
|
+
? unknown
|
|
450
|
+
: $IsResourcesDefined extends false
|
|
451
|
+
? unknown
|
|
452
|
+
: string;
|
|
453
|
+
|
|
454
|
+
type ProcessReturnValue<Target, DefaultValue> = $Turtles extends Target
|
|
455
|
+
? string
|
|
456
|
+
: [DefaultValue] extends [never]
|
|
457
|
+
? Target
|
|
458
|
+
: unknown extends DefaultValue
|
|
459
|
+
? Target
|
|
460
|
+
: Target | DefaultValue;
|
|
461
|
+
|
|
462
|
+
type PickNamespaces<T, K extends keyof any> = {
|
|
463
|
+
[P in K as P extends keyof T ? P : never]: T[P & keyof T];
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
type GetSource<
|
|
467
|
+
Ns extends Namespace,
|
|
468
|
+
KPrefix,
|
|
469
|
+
Res = Ns extends readonly [keyof Resources, any, ...any]
|
|
470
|
+
? Resources[Ns[0]] & PickNamespaces<Resources, Ns[number]>
|
|
471
|
+
: Resources[$FirstNamespace<Ns>],
|
|
472
|
+
> = KPrefix extends keyof Res
|
|
473
|
+
? Res[KPrefix]
|
|
474
|
+
: undefined extends KPrefix
|
|
475
|
+
? Res
|
|
476
|
+
: ApplyKeyPrefix<[Res], KPrefix>;
|
|
477
|
+
|
|
478
|
+
type Select<T, Context> = $IsResourcesDefined extends false
|
|
479
|
+
? $Turtles
|
|
480
|
+
: [_EnableSelector] extends ['optimize']
|
|
481
|
+
? T
|
|
482
|
+
: FilterKeys<T, Context>;
|
|
483
|
+
|
|
484
|
+
type FilterKeys<T, Context> = never | T extends readonly any[]
|
|
485
|
+
? { [I in keyof T]: FilterKeys<T[I], Context> }
|
|
486
|
+
: $Prune<
|
|
487
|
+
{
|
|
488
|
+
[K in keyof T as T[K] extends object
|
|
489
|
+
? K
|
|
490
|
+
: Context extends string
|
|
491
|
+
? never
|
|
492
|
+
: K extends `${string}${_PluralSeparator}${PluralSuffix}`
|
|
493
|
+
? never
|
|
494
|
+
: K]: T[K] extends object ? FilterKeys<T[K], Context> : T[K];
|
|
495
|
+
} & {
|
|
496
|
+
[K in keyof T as T[K] extends object
|
|
497
|
+
? never
|
|
498
|
+
: Context extends string
|
|
499
|
+
? never
|
|
500
|
+
: K extends
|
|
501
|
+
| `${infer Prefix}${_PluralSeparator}${PluralSuffix}`
|
|
502
|
+
| `${infer Prefix}${_PluralSeparator}ordinal${_PluralSeparator}${PluralSuffix}`
|
|
503
|
+
? Prefix
|
|
504
|
+
: never]: T[K] extends object ? FilterKeys<T[K], Context> : T[K];
|
|
505
|
+
} & {
|
|
506
|
+
[K in keyof T as T[K] extends object
|
|
507
|
+
? never
|
|
508
|
+
: Context extends string
|
|
509
|
+
? K extends
|
|
510
|
+
| `${infer Prefix}${_ContextSeparator}${Context}`
|
|
511
|
+
| `${infer Prefix}${_ContextSeparator}${Context}${_PluralSeparator}${PluralSuffix}`
|
|
512
|
+
? Prefix
|
|
513
|
+
: never
|
|
514
|
+
: never]: T[K] extends object ? FilterKeys<T[K], Context> : T[K];
|
|
515
|
+
}
|
|
516
|
+
>;
|