@hakistack/i18n 0.0.0-stage → 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.
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/cli/bin.cjs +940 -0
- package/fesm2022/hakistack-i18n-engine.mjs +339 -0
- package/fesm2022/hakistack-i18n-engine.mjs.map +1 -0
- package/fesm2022/hakistack-i18n.mjs +475 -0
- package/fesm2022/hakistack-i18n.mjs.map +1 -0
- package/package.json +61 -4
- package/schematics/collection.json +15 -0
- package/schematics/ng-add/index.cjs +301 -0
- package/schematics/ng-add/index.cjs.map +1 -0
- package/schematics/ng-add/schema.js +3 -0
- package/schematics/ng-add/schema.js.map +1 -0
- package/schematics/ng-add/schema.json +37 -0
- package/types/hakistack-i18n-engine.d.ts +260 -0
- package/types/hakistack-i18n.d.ts +334 -0
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { TranslationModule, AuthoredTranslationNode } from '@hakistack/i18n/engine';
|
|
2
|
+
export { AuthoredTranslationNode, I18nLocales, Language, LanguageTree, LocaleId, OtherLocales, SOURCE_LANG, SourceLang, TRANSLATION_BRAND, TranslationModule, TranslationValue, buildEagerTree, buildKeysTree, buildLanguageTree, buildScopedTree, defineTranslations, extractLanguage, getTranslation, isTranslationValue, normalizeScope, scopeToFilesystemPath, setAtScope, t } from '@hakistack/i18n/engine';
|
|
3
|
+
import * as i0 from '@angular/core';
|
|
4
|
+
import { EnvironmentProviders, InjectionToken } from '@angular/core';
|
|
5
|
+
import * as rxjs from 'rxjs';
|
|
6
|
+
import { TranslocoLoader, Translation, TranslocoInterceptor, TranslocoMissingHandler, TranslocoMissingHandlerData, AvailableLangs } from '@jsverse/transloco';
|
|
7
|
+
export { translateObjectSignal, translateSignal } from '@jsverse/transloco';
|
|
8
|
+
|
|
9
|
+
/** A selectable language: its id (locale code) and a human-facing label. */
|
|
10
|
+
interface I18nLanguageOption {
|
|
11
|
+
/** Locale code, e.g. `'en'`, `'es'`, `'es-MX'`. */
|
|
12
|
+
id: string;
|
|
13
|
+
/** Display label shown in language pickers, e.g. `'English'`, `'Español'`. */
|
|
14
|
+
label: string;
|
|
15
|
+
}
|
|
16
|
+
/** Which loader strategy backs Transloco. */
|
|
17
|
+
type I18nLoaderStrategy = 'registry' | 'http';
|
|
18
|
+
/** Behavior for missing keys in dev mode. */
|
|
19
|
+
type MissingKeyDevBehavior = 'show' | 'empty';
|
|
20
|
+
/**
|
|
21
|
+
* Consumer-facing configuration for {@link provideI18n}.
|
|
22
|
+
*
|
|
23
|
+
* Collapses what used to be a hand-written `provideTransloco(...)` config plus
|
|
24
|
+
* a list of custom hooks into one object.
|
|
25
|
+
*/
|
|
26
|
+
interface I18nConfig {
|
|
27
|
+
/**
|
|
28
|
+
* The discovered translation modules — the scanner-generated `ALL_TRANSLATIONS`
|
|
29
|
+
* tuple. Backs the registry loader and (via `buildKeysTree`) the typed
|
|
30
|
+
* directive's runtime key object.
|
|
31
|
+
*/
|
|
32
|
+
registry: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[];
|
|
33
|
+
/** Available languages with labels. The first is used as default if `defaultLang` is omitted. */
|
|
34
|
+
languages: I18nLanguageOption[];
|
|
35
|
+
/** Active language on bootstrap. Defaults to the first entry in `languages`. */
|
|
36
|
+
defaultLang?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Locale every other locale falls back to when a key is untranslated.
|
|
39
|
+
* Defaults to `'en'` (the source locale). Mirrors the build-time generator.
|
|
40
|
+
*/
|
|
41
|
+
fallbackLang?: string;
|
|
42
|
+
/**
|
|
43
|
+
* Missing-key rendering in dev mode:
|
|
44
|
+
* - `'show'` (default) → renders `[scope.key]` so typos jump out.
|
|
45
|
+
* - `'empty'` → renders `''` (Transloco's default).
|
|
46
|
+
* Production always renders `''`.
|
|
47
|
+
*/
|
|
48
|
+
missingKeyInDev?: MissingKeyDevBehavior;
|
|
49
|
+
/**
|
|
50
|
+
* Loader strategy:
|
|
51
|
+
* - `'registry'` (default) → serve translations from the in-memory registry
|
|
52
|
+
* (no HTTP, SSR-safe, offline by default).
|
|
53
|
+
* - `'http'` → fetch `<assetsPath>/<lang>.json` over HTTP.
|
|
54
|
+
*/
|
|
55
|
+
loader?: I18nLoaderStrategy;
|
|
56
|
+
/** Base path the HTTP loader fetches from. Default: `/assets/i18n`. Only used when `loader: 'http'`. */
|
|
57
|
+
assetsPath?: string;
|
|
58
|
+
/** When `true`, wires {@link LanguageService} localStorage persistence on bootstrap. Default: `false`. */
|
|
59
|
+
persistLanguage?: boolean;
|
|
60
|
+
}
|
|
61
|
+
/** Fully-resolved runtime config (defaults applied) shared via DI to loaders/handlers. */
|
|
62
|
+
interface ResolvedI18nRuntimeConfig {
|
|
63
|
+
registry: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[];
|
|
64
|
+
defaultLang: string;
|
|
65
|
+
fallbackLang: string;
|
|
66
|
+
missingKeyInDev: MissingKeyDevBehavior;
|
|
67
|
+
assetsPath: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* One-call i18n setup for a consumer app.
|
|
72
|
+
*
|
|
73
|
+
* Collapses what used to be a hand-written `provideTransloco(...)` config plus
|
|
74
|
+
* a list of custom hooks (`provideTranslocoInterceptor`,
|
|
75
|
+
* `provideTranslocoMissingHandler`) into a single provider. Wires:
|
|
76
|
+
* - Transloco with the registry loader (or HTTP loader).
|
|
77
|
+
* - The comment-stripping interceptor + the visible missing-key handler.
|
|
78
|
+
* - `I18N_KEYS` (the runtime `TK` tree) for the translate directive.
|
|
79
|
+
* - `I18N_RUNTIME_CONFIG` (resolved defaults) for the loader/handler.
|
|
80
|
+
* - Optional `LanguageService` localStorage restore on bootstrap.
|
|
81
|
+
*
|
|
82
|
+
* The consumer still imports the generated typed directive and
|
|
83
|
+
* `translateSignal`/`TK` from their local barrel.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* export const appConfig: ApplicationConfig = {
|
|
87
|
+
* providers: [
|
|
88
|
+
* provideI18n({
|
|
89
|
+
* registry: ALL_TRANSLATIONS,
|
|
90
|
+
* languages: [
|
|
91
|
+
* { id: 'en', label: 'English' },
|
|
92
|
+
* { id: 'es', label: 'Español' },
|
|
93
|
+
* ],
|
|
94
|
+
* defaultLang: 'es',
|
|
95
|
+
* fallbackLang: 'en',
|
|
96
|
+
* persistLanguage: true,
|
|
97
|
+
* }),
|
|
98
|
+
* ],
|
|
99
|
+
* };
|
|
100
|
+
*/
|
|
101
|
+
declare function provideI18n(config: I18nConfig): EnvironmentProviders;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The merged keys tree (`TK`) provided by {@link provideI18n}.
|
|
105
|
+
*
|
|
106
|
+
* Holds the consumer's nested object of dot-path key strings — the exact shape
|
|
107
|
+
* the scanner emits as the typed `TK` literal. `TranslateDirectiveBase` injects
|
|
108
|
+
* this instead of importing `TK` directly, so the library never depends on a
|
|
109
|
+
* consumer-generated symbol. Casing of keys is irrelevant at runtime: the
|
|
110
|
+
* directive reads string leaves and calls `transloco.translate(leaf)`.
|
|
111
|
+
*/
|
|
112
|
+
declare const I18N_KEYS: InjectionToken<Record<string, unknown>>;
|
|
113
|
+
/** Fully-resolved runtime config, shared with the loader and missing handler. */
|
|
114
|
+
declare const I18N_RUNTIME_CONFIG: InjectionToken<ResolvedI18nRuntimeConfig>;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Translation loader backed by the in-memory module registry.
|
|
118
|
+
*
|
|
119
|
+
* Reads directly from the registry passed to {@link provideI18n} and assembles
|
|
120
|
+
* a per-language tree on demand. No HTTP request, no `assets/i18n/<lang>.json`
|
|
121
|
+
* fetch at runtime — those JSON files are translator-facing build artifacts only.
|
|
122
|
+
*
|
|
123
|
+
* Wins:
|
|
124
|
+
* - Synchronous-after-import. No race between bootstrap and translation load.
|
|
125
|
+
* - Single source of truth at runtime — the "JSON drifted from TS" failure
|
|
126
|
+
* mode becomes structurally impossible.
|
|
127
|
+
* - Works offline; no service-worker config needed for i18n.
|
|
128
|
+
* - SSR-safe: no absolute-URL gotchas.
|
|
129
|
+
*/
|
|
130
|
+
declare class TranslocoRegistryLoader implements TranslocoLoader {
|
|
131
|
+
private readonly config;
|
|
132
|
+
getTranslation(input: string): rxjs.Observable<Translation>;
|
|
133
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoRegistryLoader, never>;
|
|
134
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoRegistryLoader>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* HTTP-based translation loader — fetches `<assetsPath>/<lang>.json` over HTTP.
|
|
139
|
+
*
|
|
140
|
+
* Selected via `provideI18n({ loader: 'http' })`. Consumes the same JSON files
|
|
141
|
+
* the generator emits, so switching loaders requires no other change.
|
|
142
|
+
*
|
|
143
|
+
* **Prefer the registry loader unless ANY of these are true:**
|
|
144
|
+
* - Translation payload exceeds ~50KB per language (JS bundle bloat matters).
|
|
145
|
+
* - The translation team needs to push updates without a code deploy.
|
|
146
|
+
* - 5+ locales where CDN-cached per-locale loading matters.
|
|
147
|
+
* - `provideTranslocoScope` lazy loading is adopted per route.
|
|
148
|
+
*
|
|
149
|
+
* **Costs:** reintroduces the runtime HTTP fetch (network race, offline issues),
|
|
150
|
+
* SSR needs absolute URLs, and the "JSON drifted from TS" mode returns unless
|
|
151
|
+
* the generator runs in the deploy pipeline.
|
|
152
|
+
*/
|
|
153
|
+
declare class TranslocoHttpLoader implements TranslocoLoader {
|
|
154
|
+
private readonly http;
|
|
155
|
+
private readonly config;
|
|
156
|
+
getTranslation(lang: string): rxjs.Observable<Translation>;
|
|
157
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoHttpLoader, never>;
|
|
158
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoHttpLoader>;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Strips `<key>.comment` siblings before translations enter Transloco's store.
|
|
163
|
+
*
|
|
164
|
+
* Translator-context comments (added via `t('en', { es: '…' }, { comment })`)
|
|
165
|
+
* are emitted into the runtime tree alongside their target keys — useful for
|
|
166
|
+
* translator review in the JSON, but they must never be looked up at runtime.
|
|
167
|
+
* Without this interceptor, a typo'd translate call could render comment text.
|
|
168
|
+
*/
|
|
169
|
+
declare class TranslocoCommentInterceptor implements TranslocoInterceptor {
|
|
170
|
+
preSaveTranslation(translation: Translation): Translation;
|
|
171
|
+
preSaveTranslationKey(_key: string, value: string): string;
|
|
172
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoCommentInterceptor, never>;
|
|
173
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoCommentInterceptor>;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Missing-key handler that surfaces failures in dev mode.
|
|
178
|
+
*
|
|
179
|
+
* Default Transloco behavior returns `''` for missing keys, making typos
|
|
180
|
+
* invisible in the UI. With `missingKeyInDev: 'show'` (the default) this returns
|
|
181
|
+
* `[scope.key]` in dev so missing-key bugs jump out immediately. In production,
|
|
182
|
+
* behavior is always `''`.
|
|
183
|
+
*
|
|
184
|
+
* The build-time validator remains the source of truth for *static* keys; this
|
|
185
|
+
* handler catches dynamic / `selectTranslate(varKey)` / typo cases.
|
|
186
|
+
*/
|
|
187
|
+
declare class TranslocoVisibleMissingHandler implements TranslocoMissingHandler {
|
|
188
|
+
private readonly config;
|
|
189
|
+
handle(key: string, _data: TranslocoMissingHandlerData): string;
|
|
190
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoVisibleMissingHandler, never>;
|
|
191
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoVisibleMissingHandler>;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* App-level active-language wrapper around `TranslocoService` with localStorage
|
|
196
|
+
* persistence and `<html lang>` syncing.
|
|
197
|
+
*
|
|
198
|
+
* Wired automatically when `provideI18n({ persistLanguage: true })` is set
|
|
199
|
+
* (an APP_INITIALIZER restores the stored language on bootstrap). It's also
|
|
200
|
+
* exported for direct use — inject it to build a language switcher:
|
|
201
|
+
*
|
|
202
|
+
* ```ts
|
|
203
|
+
* private readonly i18nLang = inject(LanguageService);
|
|
204
|
+
* switch(lang: string) { this.i18nLang.setLanguage(lang); }
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
declare class LanguageService {
|
|
208
|
+
private readonly STORAGE_KEY;
|
|
209
|
+
private readonly transloco;
|
|
210
|
+
private readonly document;
|
|
211
|
+
private readonly defaultLanguage;
|
|
212
|
+
/** Available languages from Transloco config. */
|
|
213
|
+
readonly availableLanguages: AvailableLangs;
|
|
214
|
+
/** Current active language (reactive signal). */
|
|
215
|
+
readonly language: i0.WritableSignal<string>;
|
|
216
|
+
initializeLanguage(): Promise<void>;
|
|
217
|
+
setLanguage(lang: string): Promise<void>;
|
|
218
|
+
/** Is `lang` one of the configured available languages? */
|
|
219
|
+
isAvailable(lang: string): boolean;
|
|
220
|
+
/**
|
|
221
|
+
* Stored language, or the default when nothing is stored, storage is
|
|
222
|
+
* unavailable (SSR), or the stored value is no longer an available language
|
|
223
|
+
* (stale/garbage localStorage must never become the active `<html lang>`).
|
|
224
|
+
*/
|
|
225
|
+
private getStoredLanguage;
|
|
226
|
+
private saveLanguage;
|
|
227
|
+
/** `localStorage` when it exists (browser); `null` on the server. */
|
|
228
|
+
private storage;
|
|
229
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LanguageService, never>;
|
|
230
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<LanguageService>;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** A translation key string transformed into a callable translate function. */
|
|
234
|
+
interface TranslateFn {
|
|
235
|
+
/** Get the translated string. */
|
|
236
|
+
(): string;
|
|
237
|
+
/** Get the translated string with interpolation params. */
|
|
238
|
+
(params: Record<string, unknown>): string;
|
|
239
|
+
/** The raw translation key (dev only, for debugging). */
|
|
240
|
+
readonly key: string;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Recursively transforms a keys object (`TK`) shape:
|
|
244
|
+
* - string leaves become {@link TranslateFn}
|
|
245
|
+
* - nested objects remain nested with transformed children
|
|
246
|
+
*/
|
|
247
|
+
type TranslatedKeys<T> = {
|
|
248
|
+
readonly [K in keyof T]: T[K] extends string ? TranslateFn : T[K] extends Record<string, unknown> ? TranslatedKeys<T[K]> : never;
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Context exposed to the template by the translate directive.
|
|
252
|
+
*
|
|
253
|
+
* Generic over the consumer's `TK` shape. The library ships the untyped base
|
|
254
|
+
* (`TranslateContext<Record<string, unknown>>`); the scanner-generated consumer
|
|
255
|
+
* wrapper pins `T = typeof TK` so templates get full autocomplete.
|
|
256
|
+
*/
|
|
257
|
+
interface TranslateContext<T = Record<string, unknown>> {
|
|
258
|
+
/**
|
|
259
|
+
* Typed translation object with full autocomplete.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* {{ t.common.buttons.save() }}
|
|
263
|
+
* {{ t.common.validation.minLength({ min: 3 }) }}
|
|
264
|
+
*/
|
|
265
|
+
$implicit: TranslatedKeys<T>;
|
|
266
|
+
/** Current active language code. */
|
|
267
|
+
lang: string;
|
|
268
|
+
/** Raw translate function for dynamic keys (escape hatch). */
|
|
269
|
+
translate: (key: string, params?: Record<string, unknown>) => string;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Untyped/generic core of the structural translate directive.
|
|
273
|
+
*
|
|
274
|
+
* The library can't reference a consumer-generated `TK`, so this base injects
|
|
275
|
+
* the keys object via the {@link I18N_KEYS} token (provided by `provideI18n`)
|
|
276
|
+
* and builds its lazy proxy over it. Casing is irrelevant — it reads string
|
|
277
|
+
* leaves and calls `transloco.translate(leaf)`.
|
|
278
|
+
*
|
|
279
|
+
* Consumers don't use this directly. The codegen emits a small wrapper that
|
|
280
|
+
* extends this base, declares the `<selector>Prefix` input (Angular's
|
|
281
|
+
* microsyntax binds `*appTranslate="let t; prefix: 'x'"` to `appTranslatePrefix`,
|
|
282
|
+
* so the input's name must follow the configured selector), and pins the
|
|
283
|
+
* template context type to *their* `typeof TK`:
|
|
284
|
+
*
|
|
285
|
+
* ```ts
|
|
286
|
+
* import { TK } from './definitions';
|
|
287
|
+
* @Directive({ selector: '[appTranslate]' })
|
|
288
|
+
* export class AppTranslateDirective extends TranslateDirectiveBase {
|
|
289
|
+
* readonly appTranslatePrefix = input<string | undefined>(undefined);
|
|
290
|
+
* protected override prefix(): string | undefined {
|
|
291
|
+
* return this.appTranslatePrefix();
|
|
292
|
+
* }
|
|
293
|
+
* static ngTemplateContextGuard(_d: AppTranslateDirective, ctx: unknown): ctx is TranslateContext<typeof TK> {
|
|
294
|
+
* return true;
|
|
295
|
+
* }
|
|
296
|
+
* }
|
|
297
|
+
* ```
|
|
298
|
+
*
|
|
299
|
+
* **Optimizations** (carried over from the original directive):
|
|
300
|
+
* - JS Proxy for lazy property access (no upfront tree building).
|
|
301
|
+
* - Proxy objects cached via WeakMap; translate fns cached per key via Map.
|
|
302
|
+
* - Signal-driven language reactivity (`toSignal` + `effect`), CD only on change.
|
|
303
|
+
*/
|
|
304
|
+
declare class TranslateDirectiveBase {
|
|
305
|
+
private readonly transloco;
|
|
306
|
+
private readonly keys;
|
|
307
|
+
private readonly templateRef;
|
|
308
|
+
private readonly viewContainer;
|
|
309
|
+
private viewRef;
|
|
310
|
+
/** Tracks the current language (auto-updates on language change). */
|
|
311
|
+
private readonly currentLang;
|
|
312
|
+
private readonly proxyCache;
|
|
313
|
+
private readonly translateFnCache;
|
|
314
|
+
private readonly isDev;
|
|
315
|
+
private readonly warnedKeys;
|
|
316
|
+
/**
|
|
317
|
+
* Optional prefix prepended to every key (for scoped translations).
|
|
318
|
+
*
|
|
319
|
+
* The base has no input of its own: the microsyntax binding name depends on
|
|
320
|
+
* the selector, so the generated wrapper declares `<selector>Prefix` and
|
|
321
|
+
* overrides this hook to return it. Read lazily (never during construction).
|
|
322
|
+
*/
|
|
323
|
+
protected prefix(): string | undefined;
|
|
324
|
+
private withPrefix;
|
|
325
|
+
constructor();
|
|
326
|
+
private createLazyTranslationProxy;
|
|
327
|
+
private createView;
|
|
328
|
+
private setupLangChangeEffect;
|
|
329
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslateDirectiveBase, never>;
|
|
330
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<TranslateDirectiveBase, never, never, {}, {}, never, never, true, never>;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export { I18N_KEYS, I18N_RUNTIME_CONFIG, LanguageService, TranslateDirectiveBase, TranslocoCommentInterceptor, TranslocoHttpLoader, TranslocoRegistryLoader, TranslocoVisibleMissingHandler, provideI18n };
|
|
334
|
+
export type { I18nConfig, I18nLanguageOption, I18nLoaderStrategy, MissingKeyDevBehavior, ResolvedI18nRuntimeConfig, TranslateContext, TranslateFn, TranslatedKeys };
|