vite-bundled-i18n 0.1.0

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.
Files changed (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +322 -0
  3. package/dist/cli/commands.d.ts +46 -0
  4. package/dist/cli/index.d.ts +2 -0
  5. package/dist/cli/index.js +231 -0
  6. package/dist/cli/index.js.map +1 -0
  7. package/dist/core/args.d.ts +15 -0
  8. package/dist/core/compiled-runtime.d.ts +75 -0
  9. package/dist/core/config.d.ts +65 -0
  10. package/dist/core/createI18n.d.ts +40 -0
  11. package/dist/core/data.d.ts +10 -0
  12. package/dist/core/fetcher.d.ts +5 -0
  13. package/dist/core/getTranslations.d.ts +20 -0
  14. package/dist/core/interpolator.d.ts +27 -0
  15. package/dist/core/resolver.d.ts +51 -0
  16. package/dist/core/store.d.ts +61 -0
  17. package/dist/core/t.d.ts +53 -0
  18. package/dist/core/types.d.ts +396 -0
  19. package/dist/data-Trz4mxne.js +894 -0
  20. package/dist/data-Trz4mxne.js.map +1 -0
  21. package/dist/extractor/ast-utils.d.ts +19 -0
  22. package/dist/extractor/bundle-generator.d.ts +46 -0
  23. package/dist/extractor/compiler.d.ts +47 -0
  24. package/dist/extractor/dictionary-ownership.d.ts +18 -0
  25. package/dist/extractor/extract.d.ts +12 -0
  26. package/dist/extractor/patterns.d.ts +10 -0
  27. package/dist/extractor/reports.d.ts +109 -0
  28. package/dist/extractor/scope-bundles.d.ts +9 -0
  29. package/dist/extractor/type-generator.d.ts +34 -0
  30. package/dist/extractor/types.d.ts +45 -0
  31. package/dist/extractor/walker-types.d.ts +44 -0
  32. package/dist/extractor/walker.d.ts +38 -0
  33. package/dist/getTranslations-D3q3OBTn.js +120 -0
  34. package/dist/getTranslations-D3q3OBTn.js.map +1 -0
  35. package/dist/index.d.ts +20 -0
  36. package/dist/index.js +5 -0
  37. package/dist/plugin/buildPlugin.d.ts +39 -0
  38. package/dist/plugin/devPlugin.d.ts +51 -0
  39. package/dist/plugin.d.ts +16 -0
  40. package/dist/plugin.js +226 -0
  41. package/dist/plugin.js.map +1 -0
  42. package/dist/react/DevToolbar.d.ts +22 -0
  43. package/dist/react/I18nProvider.d.ts +20 -0
  44. package/dist/react/context.d.ts +19 -0
  45. package/dist/react/useI18n.d.ts +33 -0
  46. package/dist/react.d.ts +11 -0
  47. package/dist/react.js +626 -0
  48. package/dist/react.js.map +1 -0
  49. package/dist/server-BpEocC73.js +58 -0
  50. package/dist/server-BpEocC73.js.map +1 -0
  51. package/dist/server-entry.d.ts +7 -0
  52. package/dist/server.d.ts +22 -0
  53. package/dist/server.js +4 -0
  54. package/dist/t-Bfb3ut7N.js +75 -0
  55. package/dist/t-Bfb3ut7N.js.map +1 -0
  56. package/dist/vanilla.d.ts +43 -0
  57. package/dist/vanilla.js +39 -0
  58. package/dist/vanilla.js.map +1 -0
  59. package/dist/vue.d.ts +31 -0
  60. package/dist/vue.js +87 -0
  61. package/dist/vue.js.map +1 -0
  62. package/dist/walker-DqlV3VrX.js +1307 -0
  63. package/dist/walker-DqlV3VrX.js.map +1 -0
  64. package/package.json +121 -0
@@ -0,0 +1,396 @@
1
+ import type { CompiledManifestModule } from './compiled-runtime';
2
+ /**
3
+ * Recursive type representing nested translation JSON.
4
+ * Leaf values are strings (the translated text).
5
+ * Branch values are nested objects containing more keys.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const data: NestedTranslations = {
10
+ * show: {
11
+ * title: "Product Details",
12
+ * price: "Price: {{amount}}"
13
+ * }
14
+ * };
15
+ * ```
16
+ */
17
+ export type NestedTranslations = {
18
+ [key: string]: string | NestedTranslations;
19
+ };
20
+ /**
21
+ * Augmentable interface for registering valid translation keys.
22
+ *
23
+ * When empty (no generated types), `TranslationKey` falls back to `string`,
24
+ * so `t()` accepts any key. When the type generator populates this interface
25
+ * via module augmentation, `t()` only accepts registered keys — enabling
26
+ * full autocomplete and compile-time validation.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * // Generated by: npm run i18n -- generate
31
+ * // src/i18n-types.d.ts
32
+ * declare module 'vite-bundled-i18n' {
33
+ * interface I18nKeyMap {
34
+ * 'shared.ok': true;
35
+ * 'products.show.title': true;
36
+ * }
37
+ * }
38
+ * ```
39
+ */
40
+ export interface I18nKeyMap {
41
+ }
42
+ /**
43
+ * Augmentable interface mapping fully qualified translation keys to their
44
+ * placeholder parameter objects. Generated alongside {@link I18nKeyMap}.
45
+ *
46
+ * Keys with no placeholders should map to `{}`.
47
+ */
48
+ export interface I18nParamsMap {
49
+ }
50
+ /**
51
+ * Resolves to the union of registered translation keys when `I18nKeyMap`
52
+ * has entries, or falls back to `string` when it's empty (no generated types).
53
+ */
54
+ export type TranslationKey = keyof I18nKeyMap extends never ? string : keyof I18nKeyMap & string;
55
+ /**
56
+ * Primitive value types accepted for interpolation placeholders.
57
+ */
58
+ export type Primitive = string | number | boolean | bigint | null | undefined;
59
+ /**
60
+ * Returns the generated placeholder parameter object for a fully qualified key.
61
+ *
62
+ * Unknown keys fall back to a permissive record so the runtime remains usable
63
+ * before generated types exist.
64
+ */
65
+ export type ParamsOf<K extends TranslationKey> = K extends keyof I18nParamsMap ? I18nParamsMap[K] : Record<string, Primitive>;
66
+ type TranslateArgs<K extends TranslationKey> = K extends keyof I18nParamsMap ? keyof I18nParamsMap[K] extends never ? [fallback?: string] : [params: I18nParamsMap[K], fallback?: string] : [fallback?: string] | [params: Record<string, unknown>, fallback?: string];
67
+ /**
68
+ * Translation function with overloaded signatures.
69
+ *
70
+ * Supports two calling conventions:
71
+ * - `t(key, fallback?)` — when no interpolation is needed
72
+ * - `t(key, params, fallback?)` — when interpolation parameters are needed
73
+ *
74
+ * Overload resolution: if the second argument is a string, it is treated as a
75
+ * fallback. If it is an object, it is treated as interpolation params.
76
+ *
77
+ * When generated types are present (`I18nKeyMap` is populated), the `key`
78
+ * parameter is constrained to valid translation keys with full autocomplete.
79
+ * Without generated types, it accepts any `string`.
80
+ */
81
+ export type TFunction = <K extends TranslationKey>(key: K, ...args: TranslateArgs<K>) => string;
82
+ /**
83
+ * Translation function for scoped/namespace-relative keys.
84
+ * Accepts any string since scoped keys are relative to the namespace
85
+ * and don't match the fully qualified `TranslationKey` union.
86
+ */
87
+ export type ScopedTFunction = {
88
+ (key: string): string;
89
+ (key: string, fallback: string): string;
90
+ (key: string, params: Record<string, unknown>): string;
91
+ (key: string, params: Record<string, unknown>, fallback: string): string;
92
+ };
93
+ /**
94
+ * Translation function that returns undefined when the key is not currently
95
+ * resolvable from loaded translations in the active or fallback locale.
96
+ *
97
+ * Unlike {@link TFunction}, this function does not accept fallback strings
98
+ * and does not degrade to key-as-value.
99
+ */
100
+ export type TryTFunction = <K extends TranslationKey>(key: K, ...args: K extends keyof I18nParamsMap ? keyof I18nParamsMap[K] extends never ? [] : [params: I18nParamsMap[K]] : [] | [params: Record<string, unknown>]) => string | undefined;
101
+ /**
102
+ * Namespace-relative version of {@link TryTFunction}.
103
+ */
104
+ export type ScopedTryTFunction = {
105
+ (key: string): string | undefined;
106
+ (key: string, params: Record<string, unknown>): string | undefined;
107
+ };
108
+ /**
109
+ * Translation function that throws when the key is not currently resolvable.
110
+ *
111
+ * Like {@link TryTFunction}, this function does not accept fallback strings.
112
+ */
113
+ export type RequireTFunction = <K extends TranslationKey>(key: K, ...args: K extends keyof I18nParamsMap ? keyof I18nParamsMap[K] extends never ? [] : [params: I18nParamsMap[K]] : [] | [params: Record<string, unknown>]) => string;
114
+ /**
115
+ * Namespace-relative version of {@link RequireTFunction}.
116
+ */
117
+ export type ScopedRequireTFunction = {
118
+ (key: string): string;
119
+ (key: string, params: Record<string, unknown>): string;
120
+ };
121
+ /**
122
+ * Checks whether a key is currently resolvable from loaded translations in
123
+ * either the bound locale or the configured default locale.
124
+ */
125
+ export type HasKeyFunction = (key: string) => boolean;
126
+ /**
127
+ * Translator object exposed by the hook, global accessors, and server helpers.
128
+ * It normalizes translation access across React and non-React environments.
129
+ */
130
+ export interface Translations {
131
+ /** Short UI alias. Same behavior and identity as `get`. */
132
+ t: TFunction;
133
+ /** Primary lookup method for all environments. */
134
+ get: TFunction;
135
+ /** Returns whether the key is currently resolvable. */
136
+ has: HasKeyFunction;
137
+ /** Alias of `has`. */
138
+ exists: HasKeyFunction;
139
+ /** Returns undefined for unresolved keys instead of degrading to fallback/key. */
140
+ tryGet: TryTFunction;
141
+ /** Throws for unresolved keys. */
142
+ require: RequireTFunction;
143
+ /** Returns a translator bound to a namespace and optional key prefix. */
144
+ namespace: (namespace: string, keyPrefix?: string) => NamespacedTranslations;
145
+ /** Returns a translator bound to another locale. */
146
+ forLocale: (locale: string) => Translations;
147
+ /** Bound locale for this translator object. */
148
+ locale: string;
149
+ }
150
+ /**
151
+ * Namespace-bound translator object for non-qualified translation access.
152
+ */
153
+ export interface NamespacedTranslations {
154
+ t: ScopedTFunction;
155
+ get: ScopedTFunction;
156
+ has: HasKeyFunction;
157
+ exists: HasKeyFunction;
158
+ tryGet: ScopedTryTFunction;
159
+ require: ScopedRequireTFunction;
160
+ locale: string;
161
+ namespace: string;
162
+ }
163
+ /**
164
+ * Configuration for a named dictionary — a group of namespaces
165
+ * that are preloaded at app initialization and available to all components.
166
+ */
167
+ export interface DictionaryConfig {
168
+ /**
169
+ * Legacy namespace names (filenames without extension) to include in this
170
+ * dictionary. Equivalent to `include: ['namespace.*']` for each entry.
171
+ */
172
+ keys?: string[];
173
+ /**
174
+ * Key ownership patterns included in this dictionary.
175
+ * Supported patterns:
176
+ * - `shared.*`
177
+ * - `checkout.summary.*`
178
+ * - `shared.ok`
179
+ * - `admin*` (namespace prefix)
180
+ */
181
+ include?: string[];
182
+ /** Higher priority dictionaries claim matching keys first. */
183
+ priority?: number;
184
+ /** Whether this dictionary should be pinned in the runtime cache. */
185
+ pinned?: boolean;
186
+ }
187
+ /**
188
+ * Runtime in-memory caching configuration.
189
+ */
190
+ export interface RuntimeCacheConfig {
191
+ /** Cache strategy. */
192
+ strategy?: 'memory' | 'none';
193
+ /** Namespace-level eviction behavior. */
194
+ eviction?: 'none' | 'lru';
195
+ /** Maximum number of locales to keep in memory. */
196
+ maxLocales?: number;
197
+ /** Maximum number of namespaces to keep in memory. */
198
+ maxNamespaces?: number;
199
+ /** Approximate byte budget across all namespaces. */
200
+ maxBytes?: number;
201
+ /** Whether dictionary namespaces should be pinned against eviction. */
202
+ pinDictionaries?: boolean;
203
+ }
204
+ /**
205
+ * Cache configuration for fetch behavior and runtime memory retention.
206
+ */
207
+ export interface CacheConfig {
208
+ /** Runtime memory cache configuration. Legacy string values remain supported. */
209
+ runtime?: 'memory' | 'none' | RuntimeCacheConfig;
210
+ }
211
+ export interface CacheStats {
212
+ totalLocales: number;
213
+ totalNamespaces: number;
214
+ approxTotalBytes: number;
215
+ pinnedNamespaces: number;
216
+ loadedScopes: number;
217
+ loadedDictionaries: number;
218
+ }
219
+ /**
220
+ * Optional compiled-runtime configuration. When enabled, the runtime loads
221
+ * precompiled `Map` modules instead of JSON bundles in production.
222
+ */
223
+ export interface CompiledConfig {
224
+ /**
225
+ * Whether compiled mode should be attempted.
226
+ * `auto` means "use the build-injected manifest URL when available".
227
+ */
228
+ enabled?: boolean | 'auto';
229
+ /**
230
+ * URL to the emitted compiled manifest module.
231
+ * Defaults to the build-injected value when present.
232
+ */
233
+ manifestUrl?: string;
234
+ /**
235
+ * Custom manifest loader override. Useful for SSR, tests, or advanced runtimes.
236
+ */
237
+ loadManifest?: () => Promise<CompiledManifestModule>;
238
+ }
239
+ /**
240
+ * Configuration object passed to `createI18n()`.
241
+ */
242
+ export interface I18nConfig {
243
+ /** Current active locale (e.g., `'en'`). */
244
+ locale: string;
245
+ /** Fallback locale used when a key is missing in the active locale. */
246
+ defaultLocale: string;
247
+ /** List of all supported locale codes. */
248
+ supportedLocales: string[];
249
+ /** Path to the directory containing locale folders (e.g., `'/locales'`). */
250
+ localesDir: string;
251
+ /**
252
+ * Named groups of namespaces to preload at app init.
253
+ * Keys are dictionary names, values configure which namespaces to include.
254
+ */
255
+ dictionaries?: Record<string, DictionaryConfig>;
256
+ /** Caching configuration. */
257
+ cache?: CacheConfig;
258
+ /** Optional compiled-runtime configuration. */
259
+ compiled?: CompiledConfig;
260
+ /**
261
+ * Custom options passed to every `fetch()` call made by the runtime.
262
+ * Use this to set headers, credentials, cache mode, abort signals, etc.
263
+ *
264
+ * - Static object: applied as-is to every request
265
+ * - Function: called before each request, may be async (e.g., for refreshing auth tokens)
266
+ */
267
+ requestInit?: RequestInit | (() => RequestInit | Promise<RequestInit>);
268
+ }
269
+ /**
270
+ * The i18n instance returned by `createI18n()`.
271
+ * Contains the resource store, resolved config, and methods for managing translations.
272
+ */
273
+ export interface I18nInstance {
274
+ /** The resolved configuration (frozen). */
275
+ config: Readonly<I18nConfig>;
276
+ /**
277
+ * Resolves a fully qualified key to a translated string.
278
+ * Handles namespace extraction, dot-path traversal, fallback chain, and interpolation.
279
+ */
280
+ translate: (locale: string, key: string, params?: Record<string, unknown>, fallback?: string) => string;
281
+ /**
282
+ * Resolves a fully qualified key without degrading to fallback strings or
283
+ * key-as-value. Returns undefined when the key is not currently resolvable.
284
+ */
285
+ tryTranslate: (locale: string, key: string, params?: Record<string, unknown>) => string | undefined;
286
+ /**
287
+ * Checks if a fully qualified key exists in the store for a locale.
288
+ */
289
+ hasKey: (locale: string, key: string) => boolean;
290
+ /**
291
+ * Loads namespace JSON files for a locale via fetch and adds them to the store.
292
+ * Skips namespaces already present in the store.
293
+ * Prefer `loadDictionaries` and `loadScope` for bundled loading.
294
+ */
295
+ loadNamespaces: (locale: string, namespaces: string[]) => Promise<void>;
296
+ /**
297
+ * Loads a named dictionary bundle for a locale.
298
+ * One HTTP request per dictionary: `/__i18n/{locale}/_dict/{name}.json`
299
+ */
300
+ loadDictionary: (locale: string, name: string) => Promise<void>;
301
+ /**
302
+ * Loads all configured dictionaries for a locale, in declaration order.
303
+ * One HTTP request per dictionary name.
304
+ */
305
+ loadAllDictionaries: (locale: string) => Promise<void>;
306
+ /**
307
+ * Loads a page/scope bundle for a locale.
308
+ * One HTTP request: `/__i18n/{locale}/{scope}.json`
309
+ */
310
+ loadScope: (locale: string, scope: string) => Promise<void>;
311
+ /**
312
+ * Adds resources directly to the store. Useful for testing and SSR.
313
+ */
314
+ addResources: (locale: string, namespace: string, data: NestedTranslations) => void;
315
+ /** Returns one namespace's currently loaded resources, if present. */
316
+ getResource: (locale: string, namespace: string) => NestedTranslations | undefined;
317
+ /** Returns all currently loaded namespace names for a locale. */
318
+ getLoadedNamespaces: (locale: string) => string[];
319
+ /** Returns true when the namespace data is present in the store. */
320
+ isNamespaceLoaded: (locale: string, namespace: string) => boolean;
321
+ /** Returns true when the scope bundle has already been loaded. */
322
+ isScopeLoaded: (locale: string, scope: string) => boolean;
323
+ /** Returns cache and residency information for the runtime store. */
324
+ getCacheStats: () => CacheStats;
325
+ /** Unloads all namespaces for a locale from the runtime store. */
326
+ unloadLocale: (locale: string) => void;
327
+ /** Unloads a single namespace from the runtime store. */
328
+ unloadNamespace: (locale: string, namespace: string) => void;
329
+ /** Applies cache eviction rules immediately. */
330
+ evictUnused: () => void;
331
+ /**
332
+ * Returns all unique namespace names referenced by configured dictionaries.
333
+ */
334
+ getDictionaryNamespaces: () => string[];
335
+ /** Returns the current active locale. */
336
+ getLocale: () => string;
337
+ /**
338
+ * Switches the active locale. Loads all dictionary namespaces and any
339
+ * previously loaded namespaces for the new locale before notifying listeners.
340
+ */
341
+ changeLocale: (locale: string) => Promise<void>;
342
+ /**
343
+ * Subscribes to locale changes. Returns an unsubscribe function.
344
+ */
345
+ onLocaleChange: (callback: (locale: string) => void) => () => void;
346
+ /**
347
+ * Returns all key usage entries recorded since initialization.
348
+ * Only tracks in dev mode. Useful for diagnostics, manifest generation,
349
+ * and the dev toolbar.
350
+ */
351
+ getKeyUsage: () => KeyUsageEntry[];
352
+ /**
353
+ * Returns the dictionary names in declaration order.
354
+ */
355
+ getDictionaryNames: () => string[];
356
+ }
357
+ /**
358
+ * Records how a translation key was resolved at runtime.
359
+ * Used for dev diagnostics, manifest generation, and the dev toolbar.
360
+ */
361
+ export interface KeyUsageEntry {
362
+ /** The fully qualified key that was looked up. */
363
+ key: string;
364
+ /** The namespace extracted from the key. */
365
+ namespace: string;
366
+ /** The locale used for resolution. */
367
+ locale: string;
368
+ /** Where the value came from. */
369
+ resolvedFrom: 'primary' | 'fallback-locale' | 'fallback-string' | 'key-as-value';
370
+ /** The scope/bundle that triggered this lookup, if any. */
371
+ scope?: string;
372
+ }
373
+ /**
374
+ * Return type of the `useI18n()` hook.
375
+ */
376
+ export interface UseI18nResult {
377
+ /** Translation function — same signature as the global `t()`. */
378
+ t: TFunction;
379
+ /** Alias of `t`, preferred outside UI code. */
380
+ get: TFunction;
381
+ /** Returns whether the key is currently resolvable. */
382
+ has: HasKeyFunction;
383
+ /** Alias of `has`. */
384
+ exists: HasKeyFunction;
385
+ /** Returns undefined instead of fallback/key-as-value. */
386
+ tryGet: TryTFunction;
387
+ /** Throws when the key is not currently resolvable. */
388
+ require: RequireTFunction;
389
+ /** Full translator object for richer non-hook usage. */
390
+ translations: Translations;
391
+ /** `true` when all required translations (dictionaries + bundle) are loaded. */
392
+ ready: boolean;
393
+ /** The currently active locale code. */
394
+ locale: string;
395
+ }
396
+ export {};