@hakistack/i18n 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +302 -2
- package/cli/bin.cjs +1411 -0
- package/fesm2022/hakistack-i18n-engine.mjs +394 -0
- package/fesm2022/hakistack-i18n-engine.mjs.map +1 -0
- package/fesm2022/hakistack-i18n.mjs +685 -0
- package/fesm2022/hakistack-i18n.mjs.map +1 -0
- package/package.json +68 -4
- package/schematics/collection.json +15 -0
- package/schematics/ng-add/index.cjs +276 -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 +324 -0
- package/types/hakistack-i18n.d.ts +508 -0
|
@@ -0,0 +1,508 @@
|
|
|
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
|
+
import * as i0 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';
|
|
7
|
+
import * as rxjs from 'rxjs';
|
|
8
|
+
import { Observable } from 'rxjs';
|
|
9
|
+
|
|
10
|
+
/** A selectable language: its id (the translation language), a label and optional locale metadata. */
|
|
11
|
+
interface I18nLanguageOption {
|
|
12
|
+
/** Translation language id, e.g. `'en'`, `'es'`. Autocompletes the locales declared in `i18n.config`. */
|
|
13
|
+
id: LocaleId | (string & {});
|
|
14
|
+
/** Display label shown in language pickers, e.g. `'English'`, `'Español'`. */
|
|
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';
|
|
24
|
+
}
|
|
25
|
+
/** Where the chosen language is remembered: `'local-storage'` (browser only) or `'cookie'` (also readable by SSR). */
|
|
26
|
+
type I18nLanguagePersistence = 'local-storage' | 'cookie';
|
|
27
|
+
/** Which loader strategy backs Transloco. */
|
|
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
|
+
}
|
|
43
|
+
/** Behavior for missing keys in dev mode. */
|
|
44
|
+
type MissingKeyDevBehavior = 'show' | 'empty';
|
|
45
|
+
/**
|
|
46
|
+
* Consumer-facing configuration for {@link provideI18n}.
|
|
47
|
+
*
|
|
48
|
+
* Collapses what used to be a hand-written `provideTransloco(...)` config plus
|
|
49
|
+
* a list of custom hooks into one object.
|
|
50
|
+
*/
|
|
51
|
+
interface I18nConfig {
|
|
52
|
+
/**
|
|
53
|
+
* The discovered translation modules — the scanner-generated `ALL_TRANSLATIONS`
|
|
54
|
+
* tuple. Backs the `'registry'` loader (every language in the bundle) and,
|
|
55
|
+
* when `keys` is omitted, the typed directive's runtime key object.
|
|
56
|
+
*/
|
|
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;
|
|
69
|
+
/** Available languages with labels. The first is used as default if `defaultLang` is omitted. */
|
|
70
|
+
languages: I18nLanguageOption[];
|
|
71
|
+
/** Active language on bootstrap. Defaults to the first entry in `languages`. */
|
|
72
|
+
defaultLang?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Locale every other locale falls back to when a key is untranslated.
|
|
75
|
+
* Defaults to `'en'` (the source locale). Mirrors the build-time generator.
|
|
76
|
+
*/
|
|
77
|
+
fallbackLang?: string;
|
|
78
|
+
/**
|
|
79
|
+
* Missing-key rendering in dev mode:
|
|
80
|
+
* - `'show'` (default) → renders `[scope.key]` so typos jump out.
|
|
81
|
+
* - `'empty'` → renders `''` (Transloco's default).
|
|
82
|
+
* Production always renders `''`.
|
|
83
|
+
*/
|
|
84
|
+
missingKeyInDev?: MissingKeyDevBehavior;
|
|
85
|
+
/**
|
|
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).
|
|
93
|
+
*/
|
|
94
|
+
loader?: I18nLoaderStrategy;
|
|
95
|
+
/** Base path the HTTP loader fetches from. Default: `/assets/i18n`. Only used when `loader: 'http'`. */
|
|
96
|
+
assetsPath?: string;
|
|
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;
|
|
112
|
+
}
|
|
113
|
+
/** Fully-resolved runtime config (defaults applied) shared via DI to loaders/handlers. */
|
|
114
|
+
interface ResolvedI18nRuntimeConfig {
|
|
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;
|
|
121
|
+
defaultLang: string;
|
|
122
|
+
fallbackLang: string;
|
|
123
|
+
missingKeyInDev: MissingKeyDevBehavior;
|
|
124
|
+
assetsPath: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* One-call i18n setup for a consumer app.
|
|
129
|
+
*
|
|
130
|
+
* Collapses what used to be a hand-written `provideTransloco(...)` config plus
|
|
131
|
+
* a list of custom hooks (`provideTranslocoInterceptor`,
|
|
132
|
+
* `provideTranslocoMissingHandler`) into a single provider. Wires:
|
|
133
|
+
* - Transloco with the registry, lazy (per-language chunks) or HTTP loader.
|
|
134
|
+
* - The comment-stripping interceptor + the visible missing-key handler.
|
|
135
|
+
* - `I18N_KEYS` (the runtime `TK` tree) for the translate directive.
|
|
136
|
+
* - `I18N_RUNTIME_CONFIG` (resolved defaults) for the loader/handler.
|
|
137
|
+
* - Optional `LanguageService` localStorage restore on bootstrap.
|
|
138
|
+
*
|
|
139
|
+
* The consumer still imports the generated typed directive and
|
|
140
|
+
* `translateSignal`/`TK` from their local barrel.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* export const appConfig: ApplicationConfig = {
|
|
144
|
+
* providers: [
|
|
145
|
+
* provideI18n({
|
|
146
|
+
* registry: ALL_TRANSLATIONS,
|
|
147
|
+
* languages: [
|
|
148
|
+
* { id: 'en', label: 'English' },
|
|
149
|
+
* { id: 'es', label: 'Español' },
|
|
150
|
+
* ],
|
|
151
|
+
* defaultLang: 'es',
|
|
152
|
+
* fallbackLang: 'en',
|
|
153
|
+
* persistLanguage: true,
|
|
154
|
+
* }),
|
|
155
|
+
* ],
|
|
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: [...] })
|
|
163
|
+
*/
|
|
164
|
+
declare function provideI18n(config: I18nConfig): EnvironmentProviders;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The merged keys tree (`TK`) provided by {@link provideI18n}.
|
|
168
|
+
*
|
|
169
|
+
* Holds the consumer's nested object of dot-path key strings — the exact shape
|
|
170
|
+
* the scanner emits as the typed `TK` literal. `TranslateDirectiveBase` injects
|
|
171
|
+
* this instead of importing `TK` directly, so the library never depends on a
|
|
172
|
+
* consumer-generated symbol. Casing of keys is irrelevant at runtime: the
|
|
173
|
+
* directive reads string leaves and calls `transloco.translate(leaf)`.
|
|
174
|
+
*/
|
|
175
|
+
declare const I18N_KEYS: InjectionToken<Record<string, unknown>>;
|
|
176
|
+
/** Fully-resolved runtime config, shared with the loader and missing handler. */
|
|
177
|
+
declare const I18N_RUNTIME_CONFIG: InjectionToken<ResolvedI18nRuntimeConfig>;
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Translation loader backed by the in-memory module registry.
|
|
181
|
+
*
|
|
182
|
+
* Reads directly from the registry passed to {@link provideI18n} and assembles
|
|
183
|
+
* a per-language tree on demand. No HTTP request, no `assets/i18n/<lang>.json`
|
|
184
|
+
* fetch at runtime — those JSON files are translator-facing build artifacts only.
|
|
185
|
+
*
|
|
186
|
+
* Wins:
|
|
187
|
+
* - Synchronous-after-import. No race between bootstrap and translation load.
|
|
188
|
+
* - Single source of truth at runtime — the "JSON drifted from TS" failure
|
|
189
|
+
* mode becomes structurally impossible.
|
|
190
|
+
* - Works offline; no service-worker config needed for i18n.
|
|
191
|
+
* - SSR-safe: no absolute-URL gotchas.
|
|
192
|
+
*/
|
|
193
|
+
declare class TranslocoRegistryLoader implements TranslocoLoader {
|
|
194
|
+
private readonly config;
|
|
195
|
+
getTranslation(input: string): rxjs.Observable<Translation>;
|
|
196
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoRegistryLoader, never>;
|
|
197
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoRegistryLoader>;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* HTTP-based translation loader — fetches `<assetsPath>/<lang>.json` over HTTP.
|
|
202
|
+
*
|
|
203
|
+
* Selected via `provideI18n({ loader: 'http' })`. Consumes the same JSON files
|
|
204
|
+
* the generator emits, so switching loaders requires no other change.
|
|
205
|
+
*
|
|
206
|
+
* **Prefer the registry loader unless ANY of these are true:**
|
|
207
|
+
* - Translation payload exceeds ~50KB per language (JS bundle bloat matters).
|
|
208
|
+
* - The translation team needs to push updates without a code deploy.
|
|
209
|
+
* - 5+ locales where CDN-cached per-locale loading matters.
|
|
210
|
+
* - `provideTranslocoScope` lazy loading is adopted per route.
|
|
211
|
+
*
|
|
212
|
+
* **Costs:** reintroduces the runtime HTTP fetch (network race, offline issues),
|
|
213
|
+
* SSR needs absolute URLs, and the "JSON drifted from TS" mode returns unless
|
|
214
|
+
* the generator runs in the deploy pipeline.
|
|
215
|
+
*/
|
|
216
|
+
declare class TranslocoHttpLoader implements TranslocoLoader {
|
|
217
|
+
private readonly http;
|
|
218
|
+
private readonly config;
|
|
219
|
+
getTranslation(lang: string): rxjs.Observable<Translation>;
|
|
220
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoHttpLoader, never>;
|
|
221
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoHttpLoader>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
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.
|
|
242
|
+
*
|
|
243
|
+
* Translator-context comments (added via `t('en', { es: '…' }, { comment })`)
|
|
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.
|
|
253
|
+
*/
|
|
254
|
+
declare class TranslocoCommentInterceptor implements TranslocoInterceptor {
|
|
255
|
+
preSaveTranslation(translation: Translation): Translation;
|
|
256
|
+
preSaveTranslationKey(_key: string, value: string): string;
|
|
257
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoCommentInterceptor, never>;
|
|
258
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoCommentInterceptor>;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Missing-key handler that surfaces failures in dev mode.
|
|
263
|
+
*
|
|
264
|
+
* Default Transloco behavior returns `''` for missing keys, making typos
|
|
265
|
+
* invisible in the UI. With `missingKeyInDev: 'show'` (the default) this returns
|
|
266
|
+
* `[scope.key]` in dev so missing-key bugs jump out immediately. In production,
|
|
267
|
+
* behavior is always `''`.
|
|
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
|
+
*
|
|
273
|
+
* The build-time validator remains the source of truth for *static* keys; this
|
|
274
|
+
* handler catches dynamic / `selectTranslate(varKey)` / typo cases.
|
|
275
|
+
*/
|
|
276
|
+
declare class TranslocoVisibleMissingHandler implements TranslocoMissingHandler {
|
|
277
|
+
private readonly config;
|
|
278
|
+
private readonly warned;
|
|
279
|
+
handle(key: string, data: TranslocoMissingHandlerData): string;
|
|
280
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslocoVisibleMissingHandler, never>;
|
|
281
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<TranslocoVisibleMissingHandler>;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* App-level active-language service on top of `TranslocoService`:
|
|
286
|
+
*
|
|
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.
|
|
301
|
+
*
|
|
302
|
+
* ```ts
|
|
303
|
+
* private readonly i18nLang = inject(LanguageService);
|
|
304
|
+
* switch(lang: string) { this.i18nLang.setLanguage(lang); }
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
declare class LanguageService {
|
|
308
|
+
private readonly transloco;
|
|
309
|
+
private readonly document;
|
|
310
|
+
private readonly request;
|
|
311
|
+
private readonly config;
|
|
312
|
+
private readonly defaultLanguage;
|
|
313
|
+
private readonly persistence;
|
|
314
|
+
/** Available languages from Transloco config. */
|
|
315
|
+
readonly availableLanguages: AvailableLangs;
|
|
316
|
+
/** Current active translation language (kept in sync with Transloco). */
|
|
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
|
+
*/
|
|
329
|
+
initializeLanguage(): Promise<void>;
|
|
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>;
|
|
338
|
+
/** Is `lang` one of the configured available languages? */
|
|
339
|
+
isAvailable(lang: string): boolean;
|
|
340
|
+
private languageIds;
|
|
341
|
+
private option;
|
|
342
|
+
/**
|
|
343
|
+
* The remembered language when it is still available — never a stale or
|
|
344
|
+
* garbage value. `null` when nothing (usable) is stored.
|
|
345
|
+
*/
|
|
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;
|
|
355
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LanguageService, never>;
|
|
356
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<LanguageService>;
|
|
357
|
+
}
|
|
358
|
+
|
|
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] ? {
|
|
385
|
+
/** Get the translated string. */
|
|
386
|
+
(): string;
|
|
387
|
+
/** Get the translated string with interpolation params. */
|
|
388
|
+
(params: Record<string, unknown>): string;
|
|
389
|
+
} : (params: {
|
|
390
|
+
[N in P]: unknown;
|
|
391
|
+
}) => string) & {
|
|
392
|
+
/** The raw translation key (dev only, for debugging). */
|
|
393
|
+
readonly key: string;
|
|
394
|
+
};
|
|
395
|
+
/**
|
|
396
|
+
* Recursively transforms a keys object (`TK`) shape:
|
|
397
|
+
* - string leaves become {@link TranslateFn} (typed with the key's params)
|
|
398
|
+
* - nested objects remain nested with transformed children
|
|
399
|
+
*/
|
|
400
|
+
type TranslatedKeys<T> = {
|
|
401
|
+
readonly [K in keyof T]: T[K] extends string ? TranslateFn<KeyParams<T[K]>> : T[K] extends Record<string, unknown> ? TranslatedKeys<T[K]> : never;
|
|
402
|
+
};
|
|
403
|
+
/**
|
|
404
|
+
* Context exposed to the template by the translate directive.
|
|
405
|
+
*
|
|
406
|
+
* Generic over the consumer's `TK` shape. The library ships the untyped base
|
|
407
|
+
* (`TranslateContext<Record<string, unknown>>`); the scanner-generated consumer
|
|
408
|
+
* wrapper pins `T = typeof TK` so templates get full autocomplete.
|
|
409
|
+
*/
|
|
410
|
+
interface TranslateContext<T = Record<string, unknown>> {
|
|
411
|
+
/**
|
|
412
|
+
* Typed translation object with full autocomplete.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* {{ t.common.buttons.save() }}
|
|
416
|
+
* {{ t.common.validation.minLength({ min: 3 }) }}
|
|
417
|
+
*/
|
|
418
|
+
$implicit: TranslatedKeys<T>;
|
|
419
|
+
/** Current active language code. */
|
|
420
|
+
lang: string;
|
|
421
|
+
/** Raw translate function for dynamic keys (escape hatch). */
|
|
422
|
+
translate: (key: string, params?: Record<string, unknown>) => string;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Untyped/generic core of the structural translate directive.
|
|
426
|
+
*
|
|
427
|
+
* The library can't reference a consumer-generated `TK`, so this base injects
|
|
428
|
+
* the keys object via the {@link I18N_KEYS} token (provided by `provideI18n`)
|
|
429
|
+
* and builds its lazy proxy over it. Casing is irrelevant — it reads string
|
|
430
|
+
* leaves and calls `transloco.translate(leaf)`.
|
|
431
|
+
*
|
|
432
|
+
* Consumers don't use this directly. The codegen emits a small wrapper that
|
|
433
|
+
* extends this base, declares the `<selector>Prefix` input (Angular's
|
|
434
|
+
* microsyntax binds `*appTranslate="let t; prefix: 'x'"` to `appTranslatePrefix`,
|
|
435
|
+
* so the input's name must follow the configured selector), and pins the
|
|
436
|
+
* template context type to *their* `typeof TK`:
|
|
437
|
+
*
|
|
438
|
+
* ```ts
|
|
439
|
+
* import { TK } from './definitions';
|
|
440
|
+
* @Directive({ selector: '[appTranslate]' })
|
|
441
|
+
* export class AppTranslateDirective extends TranslateDirectiveBase {
|
|
442
|
+
* readonly appTranslatePrefix = input<string | undefined>(undefined);
|
|
443
|
+
* protected override prefix(): string | undefined {
|
|
444
|
+
* return this.appTranslatePrefix();
|
|
445
|
+
* }
|
|
446
|
+
* static ngTemplateContextGuard(_d: AppTranslateDirective, ctx: unknown): ctx is TranslateContext<typeof TK> {
|
|
447
|
+
* return true;
|
|
448
|
+
* }
|
|
449
|
+
* }
|
|
450
|
+
* ```
|
|
451
|
+
*
|
|
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:**
|
|
458
|
+
* - JS Proxy for lazy property access (no upfront tree building).
|
|
459
|
+
* - Proxy objects cached via WeakMap; translate fns cached per key via Map.
|
|
460
|
+
* - One `markForCheck` per language change; no work in between.
|
|
461
|
+
*/
|
|
462
|
+
declare class TranslateDirectiveBase {
|
|
463
|
+
private readonly transloco;
|
|
464
|
+
private readonly keys;
|
|
465
|
+
private readonly templateRef;
|
|
466
|
+
private readonly viewContainer;
|
|
467
|
+
private viewRef;
|
|
468
|
+
private readonly proxyCache;
|
|
469
|
+
private readonly translateFnCache;
|
|
470
|
+
private readonly isDev;
|
|
471
|
+
/**
|
|
472
|
+
* Optional prefix prepended to every key (for scoped translations).
|
|
473
|
+
*
|
|
474
|
+
* The base has no input of its own: the microsyntax binding name depends on
|
|
475
|
+
* the selector, so the generated wrapper declares `<selector>Prefix` and
|
|
476
|
+
* overrides this hook to return it. Read lazily (never during construction).
|
|
477
|
+
*/
|
|
478
|
+
protected prefix(): string | undefined;
|
|
479
|
+
private withPrefix;
|
|
480
|
+
constructor();
|
|
481
|
+
private createLazyTranslationProxy;
|
|
482
|
+
private render;
|
|
483
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<TranslateDirectiveBase, never>;
|
|
484
|
+
static ɵdir: i0.ɵɵDirectiveDeclaration<TranslateDirectiveBase, never, never, {}, {}, never, never, true, never>;
|
|
485
|
+
}
|
|
486
|
+
|
|
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 };
|