@hakistack/i18n 0.1.0 → 0.2.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/README.md +181 -30
- package/cli/bin.cjs +894 -423
- package/fesm2022/hakistack-i18n-engine.mjs +140 -85
- package/fesm2022/hakistack-i18n-engine.mjs.map +1 -1
- package/fesm2022/hakistack-i18n.mjs +302 -92
- package/fesm2022/hakistack-i18n.mjs.map +1 -1
- package/package.json +14 -7
- package/schematics/ng-add/index.cjs +9 -34
- package/schematics/ng-add/index.cjs.map +1 -1
- package/schematics/ng-add/schema.json +1 -1
- package/types/hakistack-i18n-engine.d.ts +94 -30
- package/types/hakistack-i18n.d.ts +227 -53
|
@@ -1,20 +1,45 @@
|
|
|
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';
|
|
1
|
+
import { TranslationModule, AuthoredTranslationNode, LocaleId, KeyParams, ParamsOf } from '@hakistack/i18n/engine';
|
|
2
|
+
export { AuthoredTranslationNode, I18nKey, I18nLocales, KeyConflict, KeyParams, Language, LanguageTree, LanguageTreeOptions, LocaleId, OtherLocales, ParamsOf, SOURCE_LANG, SourceLang, TRANSLATION_BRAND, TranslationModule, TranslationValue, buildEagerTree, buildKeysTree, buildLanguageTree, buildScopedTree, defineTranslations, extractLanguage, findKeyConflicts, getTranslation, isTranslationValue, mergeKeys, normalizeScope, scopeToFilesystemPath, setAtScope, t } from '@hakistack/i18n/engine';
|
|
3
3
|
import * as i0 from '@angular/core';
|
|
4
|
-
import { EnvironmentProviders, InjectionToken } from '@angular/core';
|
|
4
|
+
import { EnvironmentProviders, InjectionToken, Signal } from '@angular/core';
|
|
5
|
+
import { Translation, TranslocoLoader, TranslocoInterceptor, TranslocoMissingHandler, TranslocoMissingHandlerData, AvailableLangs, translateSignal as translateSignal$1 } from '@jsverse/transloco';
|
|
6
|
+
export { translateObjectSignal } from '@jsverse/transloco';
|
|
5
7
|
import * as rxjs from 'rxjs';
|
|
6
|
-
import {
|
|
7
|
-
export { translateObjectSignal, translateSignal } from '@jsverse/transloco';
|
|
8
|
+
import { Observable } from 'rxjs';
|
|
8
9
|
|
|
9
|
-
/** A selectable language: its id (
|
|
10
|
+
/** A selectable language: its id (the translation language), a label and optional locale metadata. */
|
|
10
11
|
interface I18nLanguageOption {
|
|
11
|
-
/**
|
|
12
|
-
id: string;
|
|
12
|
+
/** Translation language id, e.g. `'en'`, `'es'`. Autocompletes the locales declared in `i18n.config`. */
|
|
13
|
+
id: LocaleId | (string & {});
|
|
13
14
|
/** Display label shown in language pickers, e.g. `'English'`, `'Español'`. */
|
|
14
15
|
label: string;
|
|
16
|
+
/**
|
|
17
|
+
* BCP 47 locale for formatting (dates, numbers, currency) and `<html lang>`
|
|
18
|
+
* when it differs from the translation language — e.g. Spanish texts with
|
|
19
|
+
* Puerto Rico formats: `{ id: 'es', locale: 'es-PR' }`. Default: `id`.
|
|
20
|
+
*/
|
|
21
|
+
locale?: string;
|
|
22
|
+
/** Text direction. Default: derived from the locale (`'rtl'` for Arabic, Hebrew, Persian, Urdu…). */
|
|
23
|
+
dir?: 'ltr' | 'rtl';
|
|
15
24
|
}
|
|
25
|
+
/** Where the chosen language is remembered: `'local-storage'` (browser only) or `'cookie'` (also readable by SSR). */
|
|
26
|
+
type I18nLanguagePersistence = 'local-storage' | 'cookie';
|
|
16
27
|
/** Which loader strategy backs Transloco. */
|
|
17
|
-
type I18nLoaderStrategy = 'registry' | 'http';
|
|
28
|
+
type I18nLoaderStrategy = 'registry' | 'lazy' | 'http';
|
|
29
|
+
/** Loads one language's translation tree (a generated module whose default export is the tree). */
|
|
30
|
+
type I18nTranslationLoader = () => Promise<{
|
|
31
|
+
default: Translation;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Per-language loaders — the `TRANSLATIONS` constant `hakistack-i18n generate`
|
|
35
|
+
* writes to `translations.generated.ts`. `root` holds the full tree per
|
|
36
|
+
* language; `scopes` the per-scope trees of `{ scoped: true }` modules (keyed
|
|
37
|
+
* by slash path, as `provideTranslocoScope` requests them).
|
|
38
|
+
*/
|
|
39
|
+
interface I18nTranslationLoaders {
|
|
40
|
+
root: Record<string, I18nTranslationLoader>;
|
|
41
|
+
scopes?: Record<string, Record<string, I18nTranslationLoader>>;
|
|
42
|
+
}
|
|
18
43
|
/** Behavior for missing keys in dev mode. */
|
|
19
44
|
type MissingKeyDevBehavior = 'show' | 'empty';
|
|
20
45
|
/**
|
|
@@ -26,10 +51,21 @@ type MissingKeyDevBehavior = 'show' | 'empty';
|
|
|
26
51
|
interface I18nConfig {
|
|
27
52
|
/**
|
|
28
53
|
* The discovered translation modules — the scanner-generated `ALL_TRANSLATIONS`
|
|
29
|
-
* tuple. Backs the registry loader
|
|
30
|
-
* directive's runtime key object.
|
|
54
|
+
* tuple. Backs the `'registry'` loader (every language in the bundle) and,
|
|
55
|
+
* when `keys` is omitted, the typed directive's runtime key object.
|
|
31
56
|
*/
|
|
32
|
-
registry
|
|
57
|
+
registry?: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[];
|
|
58
|
+
/**
|
|
59
|
+
* The generated `TK` object. Required without `registry`; lets the app use
|
|
60
|
+
* the `'lazy'` or `'http'` loader without importing any `*.i18n.ts` module,
|
|
61
|
+
* so no translation ends up in the initial bundle.
|
|
62
|
+
*/
|
|
63
|
+
keys?: Record<string, unknown>;
|
|
64
|
+
/**
|
|
65
|
+
* The generated `TRANSLATIONS` loaders (`translations.generated.ts`). Enables
|
|
66
|
+
* the `'lazy'` loader: one chunk per language, loaded on demand.
|
|
67
|
+
*/
|
|
68
|
+
translations?: I18nTranslationLoaders;
|
|
33
69
|
/** Available languages with labels. The first is used as default if `defaultLang` is omitted. */
|
|
34
70
|
languages: I18nLanguageOption[];
|
|
35
71
|
/** Active language on bootstrap. Defaults to the first entry in `languages`. */
|
|
@@ -47,20 +83,41 @@ interface I18nConfig {
|
|
|
47
83
|
*/
|
|
48
84
|
missingKeyInDev?: MissingKeyDevBehavior;
|
|
49
85
|
/**
|
|
50
|
-
* Loader strategy:
|
|
51
|
-
* - `'registry'`
|
|
52
|
-
*
|
|
53
|
-
* - `'
|
|
86
|
+
* Loader strategy. Default: `'lazy'` when `translations` is given, otherwise `'registry'`.
|
|
87
|
+
* - `'registry'` → serve translations from the in-memory registry (every
|
|
88
|
+
* language in the bundle; no HTTP, SSR-safe, offline by default).
|
|
89
|
+
* - `'lazy'` → dynamic-import the active language's generated module
|
|
90
|
+
* (one chunk per language; SSR-safe, no HTTP configuration).
|
|
91
|
+
* - `'http'` → fetch `<assetsPath>/<lang>.json` over HTTP (update
|
|
92
|
+
* translations without rebuilding the app).
|
|
54
93
|
*/
|
|
55
94
|
loader?: I18nLoaderStrategy;
|
|
56
95
|
/** Base path the HTTP loader fetches from. Default: `/assets/i18n`. Only used when `loader: 'http'`. */
|
|
57
96
|
assetsPath?: string;
|
|
58
|
-
/**
|
|
59
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Restore the language the user chose on bootstrap (and remember new choices):
|
|
99
|
+
* - `true` / `'local-storage'` → `localStorage` (browser only).
|
|
100
|
+
* - `'cookie'` → a `hakistack_i18n_lang` cookie, which the server also reads
|
|
101
|
+
* during SSR, so the server renders the user's language and hydration
|
|
102
|
+
* shows no language switch.
|
|
103
|
+
* Default: `false`.
|
|
104
|
+
*/
|
|
105
|
+
persistLanguage?: boolean | I18nLanguagePersistence;
|
|
106
|
+
/**
|
|
107
|
+
* When no language is remembered, pick the best match for the user's
|
|
108
|
+
* preferences — the request's `Accept-Language` during SSR, `navigator.languages`
|
|
109
|
+
* in the browser — instead of `defaultLang`. Default: `false`.
|
|
110
|
+
*/
|
|
111
|
+
detectLanguage?: boolean;
|
|
60
112
|
}
|
|
61
113
|
/** Fully-resolved runtime config (defaults applied) shared via DI to loaders/handlers. */
|
|
62
114
|
interface ResolvedI18nRuntimeConfig {
|
|
63
115
|
registry: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[];
|
|
116
|
+
translations: I18nTranslationLoaders | null;
|
|
117
|
+
languages: readonly I18nLanguageOption[];
|
|
118
|
+
/** Where `LanguageService.setLanguage` remembers the choice. */
|
|
119
|
+
persistence: I18nLanguagePersistence;
|
|
120
|
+
detectLanguage: boolean;
|
|
64
121
|
defaultLang: string;
|
|
65
122
|
fallbackLang: string;
|
|
66
123
|
missingKeyInDev: MissingKeyDevBehavior;
|
|
@@ -73,7 +130,7 @@ interface ResolvedI18nRuntimeConfig {
|
|
|
73
130
|
* Collapses what used to be a hand-written `provideTransloco(...)` config plus
|
|
74
131
|
* a list of custom hooks (`provideTranslocoInterceptor`,
|
|
75
132
|
* `provideTranslocoMissingHandler`) into a single provider. Wires:
|
|
76
|
-
* - Transloco with the registry
|
|
133
|
+
* - Transloco with the registry, lazy (per-language chunks) or HTTP loader.
|
|
77
134
|
* - The comment-stripping interceptor + the visible missing-key handler.
|
|
78
135
|
* - `I18N_KEYS` (the runtime `TK` tree) for the translate directive.
|
|
79
136
|
* - `I18N_RUNTIME_CONFIG` (resolved defaults) for the loader/handler.
|
|
@@ -97,6 +154,12 @@ interface ResolvedI18nRuntimeConfig {
|
|
|
97
154
|
* }),
|
|
98
155
|
* ],
|
|
99
156
|
* };
|
|
157
|
+
*
|
|
158
|
+
* @example Lazy: one chunk per language, no translation in the initial bundle
|
|
159
|
+
* import { TK } from './i18n/definitions/keys.generated';
|
|
160
|
+
* import { TRANSLATIONS } from './i18n/definitions/translations.generated';
|
|
161
|
+
*
|
|
162
|
+
* provideI18n({ keys: TK, translations: TRANSLATIONS, languages: [...] })
|
|
100
163
|
*/
|
|
101
164
|
declare function provideI18n(config: I18nConfig): EnvironmentProviders;
|
|
102
165
|
|
|
@@ -159,12 +222,34 @@ declare class TranslocoHttpLoader implements TranslocoLoader {
|
|
|
159
222
|
}
|
|
160
223
|
|
|
161
224
|
/**
|
|
162
|
-
*
|
|
225
|
+
* Translation loader backed by the generated `TRANSLATIONS` loaders: each
|
|
226
|
+
* language (and each scoped scope) is a separate chunk, dynamically imported
|
|
227
|
+
* the first time it is needed.
|
|
228
|
+
*
|
|
229
|
+
* The import runs as an Angular pending task, so server rendering waits for it
|
|
230
|
+
* and serializes translated HTML (no flash or hydration mismatch).
|
|
231
|
+
*/
|
|
232
|
+
declare class TranslocoLazyLoader implements TranslocoLoader {
|
|
233
|
+
private readonly config;
|
|
234
|
+
private readonly pendingTasks;
|
|
235
|
+
getTranslation(input: string): Observable<Translation>;
|
|
236
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoLazyLoader, never>;
|
|
237
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoLazyLoader>;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Strips translator-comment siblings before translations enter Transloco's store.
|
|
163
242
|
*
|
|
164
243
|
* Translator-context comments (added via `t('en', { es: '…' }, { comment })`)
|
|
165
|
-
* are
|
|
166
|
-
*
|
|
167
|
-
*
|
|
244
|
+
* are written into the generated JSON as a `<key>.comment` sibling of their
|
|
245
|
+
* key. The registry loader never emits them; this interceptor removes them
|
|
246
|
+
* when the JSON is served over HTTP.
|
|
247
|
+
*
|
|
248
|
+
* Transloco hands this hook the **flattened** translation, so a real key whose
|
|
249
|
+
* last segment is `comment` (`post.comment`) also ends in `.comment`. The two
|
|
250
|
+
* are told apart structurally: a comment sibling `x.comment` only exists next
|
|
251
|
+
* to a translation `x`, while a real `post.comment` means `post` is a group and
|
|
252
|
+
* therefore never a flat key itself.
|
|
168
253
|
*/
|
|
169
254
|
declare class TranslocoCommentInterceptor implements TranslocoInterceptor {
|
|
170
255
|
preSaveTranslation(translation: Translation): Translation;
|
|
@@ -181,23 +266,38 @@ declare class TranslocoCommentInterceptor implements TranslocoInterceptor {
|
|
|
181
266
|
* `[scope.key]` in dev so missing-key bugs jump out immediately. In production,
|
|
182
267
|
* behavior is always `''`.
|
|
183
268
|
*
|
|
269
|
+
* Providing a handler replaces Transloco's default one, which is where the
|
|
270
|
+
* `missingHandler.logMissingKey` console warning lives — so this handler warns
|
|
271
|
+
* too (once per key, outside prod mode).
|
|
272
|
+
*
|
|
184
273
|
* The build-time validator remains the source of truth for *static* keys; this
|
|
185
274
|
* handler catches dynamic / `selectTranslate(varKey)` / typo cases.
|
|
186
275
|
*/
|
|
187
276
|
declare class TranslocoVisibleMissingHandler implements TranslocoMissingHandler {
|
|
188
277
|
private readonly config;
|
|
189
|
-
|
|
278
|
+
private readonly warned;
|
|
279
|
+
handle(key: string, data: TranslocoMissingHandlerData): string;
|
|
190
280
|
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoVisibleMissingHandler, never>;
|
|
191
281
|
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoVisibleMissingHandler>;
|
|
192
282
|
}
|
|
193
283
|
|
|
194
284
|
/**
|
|
195
|
-
* App-level active-language
|
|
196
|
-
* persistence and `<html lang>` syncing.
|
|
285
|
+
* App-level active-language service on top of `TranslocoService`:
|
|
197
286
|
*
|
|
198
|
-
*
|
|
199
|
-
* (
|
|
200
|
-
*
|
|
287
|
+
* - `language()` — the translation language; `locale()` — its BCP 47
|
|
288
|
+
* formatting locale (`es` + `locale: 'es-PR'`); `dir()` — `'ltr' | 'rtl'`.
|
|
289
|
+
* All follow Transloco's active language however it changes.
|
|
290
|
+
* - `<html lang>` (the locale) and `<html dir>` are kept in sync, on the
|
|
291
|
+
* server too.
|
|
292
|
+
* - {@link setLanguage} loads, activates and remembers a language
|
|
293
|
+
* (`localStorage`, or a cookie that SSR can read — `persistLanguage`).
|
|
294
|
+
* - On bootstrap ({@link initializeLanguage}, wired by `provideI18n` when
|
|
295
|
+
* `persistLanguage` or `detectLanguage` is set): the remembered language,
|
|
296
|
+
* else the best match for `Accept-Language` / `navigator.languages`
|
|
297
|
+
* (`detectLanguage`), else `defaultLang`.
|
|
298
|
+
*
|
|
299
|
+
* One instance per application (per request during SSR): no state is shared
|
|
300
|
+
* between users.
|
|
201
301
|
*
|
|
202
302
|
* ```ts
|
|
203
303
|
* private readonly i18nLang = inject(LanguageService);
|
|
@@ -205,47 +305,100 @@ declare class TranslocoVisibleMissingHandler implements TranslocoMissingHandler
|
|
|
205
305
|
* ```
|
|
206
306
|
*/
|
|
207
307
|
declare class LanguageService {
|
|
208
|
-
private readonly STORAGE_KEY;
|
|
209
308
|
private readonly transloco;
|
|
210
309
|
private readonly document;
|
|
310
|
+
private readonly request;
|
|
311
|
+
private readonly config;
|
|
211
312
|
private readonly defaultLanguage;
|
|
313
|
+
private readonly persistence;
|
|
212
314
|
/** Available languages from Transloco config. */
|
|
213
315
|
readonly availableLanguages: AvailableLangs;
|
|
214
|
-
/** Current active language (
|
|
316
|
+
/** Current active translation language (kept in sync with Transloco). */
|
|
215
317
|
readonly language: i0.WritableSignal<string>;
|
|
318
|
+
/** Formatting locale of the active language (its `locale` option, else its id) — for `Intl.*` and locale pipes. */
|
|
319
|
+
readonly locale: i0.Signal<string>;
|
|
320
|
+
/** Text direction of the active language (its `dir` option, else derived from the locale). */
|
|
321
|
+
readonly dir: i0.Signal<"ltr" | "rtl">;
|
|
322
|
+
constructor();
|
|
323
|
+
/**
|
|
324
|
+
* Bootstrap: the remembered language, else (with `detectLanguage`) the user's
|
|
325
|
+
* preference, else the default. Nothing is written: only an explicit
|
|
326
|
+
* {@link setLanguage} choice is remembered, so detection keeps following the
|
|
327
|
+
* user's preferences until they pick a language.
|
|
328
|
+
*/
|
|
216
329
|
initializeLanguage(): Promise<void>;
|
|
217
|
-
|
|
330
|
+
/**
|
|
331
|
+
* Load and activate `lang`, then remember it. Languages that are not in the
|
|
332
|
+
* configured `languages` list are ignored (with a dev-mode warning) so they
|
|
333
|
+
* can never become the active or stored language.
|
|
334
|
+
*/
|
|
335
|
+
setLanguage(lang: string, options?: {
|
|
336
|
+
remember?: boolean;
|
|
337
|
+
}): Promise<void>;
|
|
218
338
|
/** Is `lang` one of the configured available languages? */
|
|
219
339
|
isAvailable(lang: string): boolean;
|
|
340
|
+
private languageIds;
|
|
341
|
+
private option;
|
|
220
342
|
/**
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
* (stale/garbage localStorage must never become the active `<html lang>`).
|
|
343
|
+
* The remembered language when it is still available — never a stale or
|
|
344
|
+
* garbage value. `null` when nothing (usable) is stored.
|
|
224
345
|
*/
|
|
225
|
-
private
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
private
|
|
346
|
+
private rememberedLanguage;
|
|
347
|
+
/** Best configured language for the request's `Accept-Language` (SSR) or `navigator.languages` (browser). */
|
|
348
|
+
private preferredLanguage;
|
|
349
|
+
private remember;
|
|
350
|
+
/** The request's `Cookie` header during SSR, `document.cookie` in the browser. */
|
|
351
|
+
private readCookie;
|
|
352
|
+
private writeCookie;
|
|
353
|
+
private readLocalStorage;
|
|
354
|
+
private writeLocalStorage;
|
|
229
355
|
static ɵfac: i0.ɵɵFactoryDeclaration<LanguageService, never>;
|
|
230
356
|
static ɵprov: i0.ɵɵInjectableDeclaration<LanguageService>;
|
|
231
357
|
}
|
|
232
358
|
|
|
233
|
-
/**
|
|
234
|
-
|
|
359
|
+
/**
|
|
360
|
+
* Locale helpers built on the standard `Intl` APIs (no locale data shipped).
|
|
361
|
+
*
|
|
362
|
+
* A *language* is what the translations are written in (`es`); a *locale* is
|
|
363
|
+
* the BCP 47 tag that drives formatting and `<html lang>` (`es-PR`). They
|
|
364
|
+
* default to the same tag but are configured separately per language option.
|
|
365
|
+
*/
|
|
366
|
+
/**
|
|
367
|
+
* Text direction of a BCP 47 tag: `'rtl'` for Arabic, Hebrew, Persian, Urdu,
|
|
368
|
+
* Sorani (`ckb`), `pa-Arab`… and `'ltr'` otherwise. Uses `Intl.Locale` text
|
|
369
|
+
* info when the runtime has it, else the likely script (`maximize()`).
|
|
370
|
+
*/
|
|
371
|
+
declare function textDirection(tag: string): 'ltr' | 'rtl';
|
|
372
|
+
/** Language tags of an `Accept-Language` header, most preferred first (`q=0` dropped). */
|
|
373
|
+
declare function parseAcceptLanguage(header: string | null | undefined): string[];
|
|
374
|
+
/**
|
|
375
|
+
* Best available language for a preference list: an exact tag match first
|
|
376
|
+
* (case-insensitive), then the same base language (`fr-CA` → `fr`, `es` → `es-MX`).
|
|
377
|
+
*/
|
|
378
|
+
declare function negotiateLanguage(preferred: readonly string[], available: readonly string[]): string | null;
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* A translation key turned into a callable translate function. Keys whose
|
|
382
|
+
* source text has `{{…}}` placeholders (`P`) require exactly those params.
|
|
383
|
+
*/
|
|
384
|
+
type TranslateFn<P extends string = never> = ([P] extends [never] ? {
|
|
235
385
|
/** Get the translated string. */
|
|
236
386
|
(): string;
|
|
237
387
|
/** Get the translated string with interpolation params. */
|
|
238
388
|
(params: Record<string, unknown>): string;
|
|
389
|
+
} : (params: {
|
|
390
|
+
[N in P]: unknown;
|
|
391
|
+
}) => string) & {
|
|
239
392
|
/** The raw translation key (dev only, for debugging). */
|
|
240
393
|
readonly key: string;
|
|
241
|
-
}
|
|
394
|
+
};
|
|
242
395
|
/**
|
|
243
396
|
* Recursively transforms a keys object (`TK`) shape:
|
|
244
|
-
* - string leaves become {@link TranslateFn}
|
|
397
|
+
* - string leaves become {@link TranslateFn} (typed with the key's params)
|
|
245
398
|
* - nested objects remain nested with transformed children
|
|
246
399
|
*/
|
|
247
400
|
type TranslatedKeys<T> = {
|
|
248
|
-
readonly [K in keyof T]: T[K] extends string ? TranslateFn : T[K] extends Record<string, unknown> ? TranslatedKeys<T[K]> : never;
|
|
401
|
+
readonly [K in keyof T]: T[K] extends string ? TranslateFn<KeyParams<T[K]>> : T[K] extends Record<string, unknown> ? TranslatedKeys<T[K]> : never;
|
|
249
402
|
};
|
|
250
403
|
/**
|
|
251
404
|
* Context exposed to the template by the translate directive.
|
|
@@ -296,10 +449,15 @@ interface TranslateContext<T = Record<string, unknown>> {
|
|
|
296
449
|
* }
|
|
297
450
|
* ```
|
|
298
451
|
*
|
|
299
|
-
* **
|
|
452
|
+
* **Loading:** the directive loads the active language itself (like
|
|
453
|
+
* Transloco's own `*transloco`), creates its view once that load settles, and
|
|
454
|
+
* re-renders after every language switch has loaded — no app initializer or
|
|
455
|
+
* other translation consumer is required.
|
|
456
|
+
*
|
|
457
|
+
* **Optimizations:**
|
|
300
458
|
* - JS Proxy for lazy property access (no upfront tree building).
|
|
301
459
|
* - Proxy objects cached via WeakMap; translate fns cached per key via Map.
|
|
302
|
-
* -
|
|
460
|
+
* - One `markForCheck` per language change; no work in between.
|
|
303
461
|
*/
|
|
304
462
|
declare class TranslateDirectiveBase {
|
|
305
463
|
private readonly transloco;
|
|
@@ -307,12 +465,9 @@ declare class TranslateDirectiveBase {
|
|
|
307
465
|
private readonly templateRef;
|
|
308
466
|
private readonly viewContainer;
|
|
309
467
|
private viewRef;
|
|
310
|
-
/** Tracks the current language (auto-updates on language change). */
|
|
311
|
-
private readonly currentLang;
|
|
312
468
|
private readonly proxyCache;
|
|
313
469
|
private readonly translateFnCache;
|
|
314
470
|
private readonly isDev;
|
|
315
|
-
private readonly warnedKeys;
|
|
316
471
|
/**
|
|
317
472
|
* Optional prefix prepended to every key (for scoped translations).
|
|
318
473
|
*
|
|
@@ -324,11 +479,30 @@ declare class TranslateDirectiveBase {
|
|
|
324
479
|
private withPrefix;
|
|
325
480
|
constructor();
|
|
326
481
|
private createLazyTranslationProxy;
|
|
327
|
-
private
|
|
328
|
-
private setupLangChangeEffect;
|
|
482
|
+
private render;
|
|
329
483
|
static ɵfac: i0.ɵɵFactoryDeclaration<TranslateDirectiveBase, never>;
|
|
330
484
|
static ɵdir: i0.ɵɵDirectiveDeclaration<TranslateDirectiveBase, never, never, {}, {}, never, never, true, never>;
|
|
331
485
|
}
|
|
332
486
|
|
|
333
|
-
|
|
334
|
-
|
|
487
|
+
type TranslocoArgs = Parameters<typeof translateSignal$1>;
|
|
488
|
+
type Key = TranslocoArgs[0];
|
|
489
|
+
type Lang = TranslocoArgs[2];
|
|
490
|
+
type Injector = TranslocoArgs[3];
|
|
491
|
+
type UntypedParams = TranslocoArgs[1];
|
|
492
|
+
/** Params of a key with placeholders: every name required, as a value or as a signal of the whole object. */
|
|
493
|
+
type RequiredParams<K> = ParamsOf<K> | Signal<ParamsOf<K>>;
|
|
494
|
+
/** Remaining arguments: params are required (and checked) when the key declares placeholders. */
|
|
495
|
+
type TranslateSignalArgs<K> = [KeyParams<K>] extends [never] ? [params?: UntypedParams, lang?: Lang, injector?: Injector] : [params: RequiredParams<K>, lang?: Lang, injector?: Injector];
|
|
496
|
+
type TranslateSignalRef<K> = K extends readonly unknown[] | Signal<string[]> ? Signal<string[]> : Signal<string>;
|
|
497
|
+
/**
|
|
498
|
+
* Transloco's `translateSignal`, with interpolation params checked against the
|
|
499
|
+
* key: for `TK.home.welcome` generated from `t('Welcome, {{username}}!')`,
|
|
500
|
+
* `translateSignal(TK.home.welcome)` is a compile error and
|
|
501
|
+
* `translateSignal(TK.home.welcome, { username })` is required. Plain keys,
|
|
502
|
+
* key arrays and key signals keep Transloco's signature. Runtime behavior is
|
|
503
|
+
* Transloco's (same arguments, same reactivity).
|
|
504
|
+
*/
|
|
505
|
+
declare function translateSignal<K extends Key>(key: K, ...args: TranslateSignalArgs<K>): TranslateSignalRef<K>;
|
|
506
|
+
|
|
507
|
+
export { I18N_KEYS, I18N_RUNTIME_CONFIG, LanguageService, TranslateDirectiveBase, TranslocoCommentInterceptor, TranslocoHttpLoader, TranslocoLazyLoader, TranslocoRegistryLoader, TranslocoVisibleMissingHandler, negotiateLanguage, parseAcceptLanguage, provideI18n, textDirection, translateSignal };
|
|
508
|
+
export type { I18nConfig, I18nLanguageOption, I18nLanguagePersistence, I18nLoaderStrategy, I18nTranslationLoader, I18nTranslationLoaders, MissingKeyDevBehavior, ResolvedI18nRuntimeConfig, TranslateContext, TranslateFn, TranslateSignalArgs, TranslatedKeys };
|