@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,301 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ngAdd = ngAdd;
|
|
4
|
+
const schematics_1 = require("@angular-devkit/schematics");
|
|
5
|
+
const tasks_1 = require("@angular-devkit/schematics/tasks");
|
|
6
|
+
const fs_1 = require("fs");
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const DEFAULT_TRANSLOCO_RANGE = '^8.2.0';
|
|
9
|
+
function ngAdd(options) {
|
|
10
|
+
return (tree, context) => {
|
|
11
|
+
const setup = resolveSetup(tree, options);
|
|
12
|
+
context.logger.info(`\n@hakistack/i18n setup → project "${setup.projectName}" (locales: ${setup.locales.join(', ')})`);
|
|
13
|
+
return (0, schematics_1.chain)([
|
|
14
|
+
addTranslocoDependency(),
|
|
15
|
+
createConfig(setup),
|
|
16
|
+
addNpmScripts(options, setup),
|
|
17
|
+
createLocaleTypes(options, setup),
|
|
18
|
+
createSampleModule(options, setup),
|
|
19
|
+
addGitignore(setup),
|
|
20
|
+
printNextSteps(setup),
|
|
21
|
+
scheduleInstall(options),
|
|
22
|
+
]);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
// =============================================================================
|
|
26
|
+
// Dependencies
|
|
27
|
+
// =============================================================================
|
|
28
|
+
function addTranslocoDependency() {
|
|
29
|
+
return (tree, context) => {
|
|
30
|
+
const pkg = readJson(tree, '/package.json');
|
|
31
|
+
if (!pkg)
|
|
32
|
+
throw new Error('Could not find package.json at the workspace root.');
|
|
33
|
+
pkg.dependencies = pkg.dependencies ?? {};
|
|
34
|
+
pkg.devDependencies = pkg.devDependencies ?? {};
|
|
35
|
+
const range = readLibraryPeerDependencies()['@jsverse/transloco'] ?? DEFAULT_TRANSLOCO_RANGE;
|
|
36
|
+
if (pkg.dependencies['@jsverse/transloco'] || pkg.devDependencies['@jsverse/transloco']) {
|
|
37
|
+
context.logger.info(' @jsverse/transloco already present — skipped');
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
pkg.dependencies['@jsverse/transloco'] = range;
|
|
41
|
+
context.logger.info(` Added @jsverse/transloco@${range} to dependencies`);
|
|
42
|
+
writeJson(tree, '/package.json', pkg);
|
|
43
|
+
}
|
|
44
|
+
return tree;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// =============================================================================
|
|
48
|
+
// i18n.config.json
|
|
49
|
+
// =============================================================================
|
|
50
|
+
function createConfig(setup) {
|
|
51
|
+
return (tree, context) => {
|
|
52
|
+
const path = '/i18n.config.json';
|
|
53
|
+
if (tree.exists(path)) {
|
|
54
|
+
context.logger.info(' i18n.config.json already exists — skipped');
|
|
55
|
+
return tree;
|
|
56
|
+
}
|
|
57
|
+
const config = {
|
|
58
|
+
srcRoot: setup.sourceRoot,
|
|
59
|
+
definitionsDir: setup.definitionsDir,
|
|
60
|
+
outputDir: setup.outputDir,
|
|
61
|
+
locales: setup.locales,
|
|
62
|
+
};
|
|
63
|
+
tree.create(path, JSON.stringify(config, null, 2) + '\n');
|
|
64
|
+
context.logger.info(' Created i18n.config.json');
|
|
65
|
+
return tree;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// =============================================================================
|
|
69
|
+
// npm scripts
|
|
70
|
+
// =============================================================================
|
|
71
|
+
function addNpmScripts(options, _setup) {
|
|
72
|
+
return (tree, context) => {
|
|
73
|
+
if (options.skipScripts)
|
|
74
|
+
return tree;
|
|
75
|
+
const pkg = readJson(tree, '/package.json');
|
|
76
|
+
if (!pkg)
|
|
77
|
+
return tree;
|
|
78
|
+
const scripts = (pkg.scripts ??= {});
|
|
79
|
+
let mutated = false;
|
|
80
|
+
const set = (name, value) => {
|
|
81
|
+
if (scripts[name] === undefined) {
|
|
82
|
+
scripts[name] = value;
|
|
83
|
+
mutated = true;
|
|
84
|
+
context.logger.info(` Added script "${name}"`);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
set('i18n:scan', 'hakistack-i18n scan');
|
|
88
|
+
set('i18n:generate', 'hakistack-i18n generate');
|
|
89
|
+
set('i18n:validate', 'hakistack-i18n validate');
|
|
90
|
+
// Chain codegen before serve/build. Append (don't clobber) existing hooks.
|
|
91
|
+
mutated = appendToScript(scripts, 'prestart', 'npm run i18n:scan', context) || mutated;
|
|
92
|
+
mutated =
|
|
93
|
+
appendToScript(scripts, 'prebuild', 'npm run i18n:scan && npm run i18n:generate && npm run i18n:validate', context) || mutated;
|
|
94
|
+
if (mutated)
|
|
95
|
+
writeJson(tree, '/package.json', pkg);
|
|
96
|
+
return tree;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function appendToScript(scripts, name, command, context) {
|
|
100
|
+
const existing = scripts[name];
|
|
101
|
+
if (existing === undefined) {
|
|
102
|
+
scripts[name] = command;
|
|
103
|
+
context.logger.info(` Added "${name}": ${command}`);
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
if (existing.includes('hakistack-i18n') || existing.includes('i18n:scan')) {
|
|
107
|
+
context.logger.info(` "${name}" already runs i18n codegen — skipped`);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
scripts[name] = `${existing} && ${command}`;
|
|
111
|
+
context.logger.info(` Appended i18n codegen to "${name}"`);
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
// =============================================================================
|
|
115
|
+
// Locale type augmentation
|
|
116
|
+
// =============================================================================
|
|
117
|
+
function createLocaleTypes(options, setup) {
|
|
118
|
+
return (tree, context) => {
|
|
119
|
+
if (options.skipSample)
|
|
120
|
+
return tree;
|
|
121
|
+
const path = `/${setup.sourceRoot}/i18n-locales.d.ts`;
|
|
122
|
+
if (tree.exists(path)) {
|
|
123
|
+
context.logger.info(` ${rel(path)} already exists — skipped`);
|
|
124
|
+
return tree;
|
|
125
|
+
}
|
|
126
|
+
const others = setup.locales.filter((l) => l !== 'en');
|
|
127
|
+
const fields = ['en', ...others].map((l) => ` ${l}: string;`).join('\n');
|
|
128
|
+
const content = `// Declares this project's locale set so \`t()\` autocompletes the right locales.
|
|
129
|
+
import '@hakistack/i18n/engine';
|
|
130
|
+
|
|
131
|
+
declare module '@hakistack/i18n/engine' {
|
|
132
|
+
interface I18nLocales {
|
|
133
|
+
${fields}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
`;
|
|
137
|
+
tree.create(path, content);
|
|
138
|
+
context.logger.info(` Created ${rel(path)}`);
|
|
139
|
+
return tree;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
// =============================================================================
|
|
143
|
+
// Sample module
|
|
144
|
+
// =============================================================================
|
|
145
|
+
function createSampleModule(options, setup) {
|
|
146
|
+
return (tree, context) => {
|
|
147
|
+
if (options.skipSample)
|
|
148
|
+
return tree;
|
|
149
|
+
if (hasExistingI18nModule(tree, setup.sourceRoot)) {
|
|
150
|
+
context.logger.info(' Existing *.i18n.ts found — skipped sample');
|
|
151
|
+
return tree;
|
|
152
|
+
}
|
|
153
|
+
const path = `/${setup.sourceRoot}/app/app.i18n.ts`;
|
|
154
|
+
if (tree.exists(path))
|
|
155
|
+
return tree;
|
|
156
|
+
const esLine = setup.locales.includes('es') ? ", { es: 'Mi App' }" : '';
|
|
157
|
+
const content = `import { defineTranslations, t } from '@hakistack/i18n/engine';
|
|
158
|
+
|
|
159
|
+
export const APP = defineTranslations('app', {
|
|
160
|
+
title: t('My App'${esLine}),
|
|
161
|
+
});
|
|
162
|
+
`;
|
|
163
|
+
tree.create(path, content);
|
|
164
|
+
context.logger.info(` Created sample ${rel(path)}`);
|
|
165
|
+
return tree;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function hasExistingI18nModule(tree, sourceRoot) {
|
|
169
|
+
const dir = tree.getDir(`/${sourceRoot}`);
|
|
170
|
+
let found = false;
|
|
171
|
+
dir.visit((filePath) => {
|
|
172
|
+
if (filePath.endsWith('.i18n.ts'))
|
|
173
|
+
found = true;
|
|
174
|
+
});
|
|
175
|
+
return found;
|
|
176
|
+
}
|
|
177
|
+
// =============================================================================
|
|
178
|
+
// .gitignore
|
|
179
|
+
// =============================================================================
|
|
180
|
+
function addGitignore(setup) {
|
|
181
|
+
return (tree, context) => {
|
|
182
|
+
const path = '/.gitignore';
|
|
183
|
+
const marker = '# @hakistack/i18n generated artifacts';
|
|
184
|
+
const lines = [
|
|
185
|
+
marker,
|
|
186
|
+
`${setup.definitionsDir}/index.generated.ts`,
|
|
187
|
+
`${setup.definitionsDir}/keys.generated.ts`,
|
|
188
|
+
`${setup.definitionsDir}/*.directive.ts`,
|
|
189
|
+
];
|
|
190
|
+
const block = '\n' + lines.join('\n') + '\n';
|
|
191
|
+
if (!tree.exists(path)) {
|
|
192
|
+
tree.create(path, block.trimStart());
|
|
193
|
+
context.logger.info(' Created .gitignore with generated-artifact entries');
|
|
194
|
+
return tree;
|
|
195
|
+
}
|
|
196
|
+
const current = tree.read(path).toString('utf-8');
|
|
197
|
+
if (current.includes(marker)) {
|
|
198
|
+
context.logger.info(' .gitignore already has i18n entries — skipped');
|
|
199
|
+
return tree;
|
|
200
|
+
}
|
|
201
|
+
tree.overwrite(path, current.replace(/\s*$/, '') + '\n' + block);
|
|
202
|
+
context.logger.info(' Added generated-artifact entries to .gitignore');
|
|
203
|
+
return tree;
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
// =============================================================================
|
|
207
|
+
// Next-steps message
|
|
208
|
+
// =============================================================================
|
|
209
|
+
function printNextSteps(setup) {
|
|
210
|
+
return (_tree, context) => {
|
|
211
|
+
context.logger.info([
|
|
212
|
+
'',
|
|
213
|
+
'@hakistack/i18n is set up. Next steps:',
|
|
214
|
+
' 1. Run the first codegen: npm run i18n:scan',
|
|
215
|
+
' 2. Add provideI18n to your app config:',
|
|
216
|
+
'',
|
|
217
|
+
` import { provideI18n } from '@hakistack/i18n';`,
|
|
218
|
+
` import { ALL_TRANSLATIONS } from './${trimLeadingSrc(setup.definitionsDir, setup.sourceRoot)}/index.generated';`,
|
|
219
|
+
'',
|
|
220
|
+
' providers: [',
|
|
221
|
+
' provideI18n({',
|
|
222
|
+
' registry: ALL_TRANSLATIONS,',
|
|
223
|
+
` languages: [${setup.locales.map((l) => `{ id: '${l}', label: '${l}' }`).join(', ')}],`,
|
|
224
|
+
` defaultLang: '${setup.locales[0]}',`,
|
|
225
|
+
' persistLanguage: true,',
|
|
226
|
+
' }),',
|
|
227
|
+
' ],',
|
|
228
|
+
'',
|
|
229
|
+
' 3. Author translations in *.i18n.ts and import the generated AppTranslateDirective in templates.',
|
|
230
|
+
'',
|
|
231
|
+
].join('\n'));
|
|
232
|
+
return _tree;
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
function scheduleInstall(options) {
|
|
236
|
+
return (_tree, context) => {
|
|
237
|
+
if (options.skipInstall)
|
|
238
|
+
return;
|
|
239
|
+
context.addTask(new tasks_1.NodePackageInstallTask());
|
|
240
|
+
context.logger.info(' Scheduled package install');
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
// =============================================================================
|
|
244
|
+
// Helpers
|
|
245
|
+
// =============================================================================
|
|
246
|
+
function resolveSetup(tree, options) {
|
|
247
|
+
const angularJson = readJson(tree, '/angular.json');
|
|
248
|
+
const projects = angularJson?.projects ?? {};
|
|
249
|
+
const projectName = resolveProjectName(projects, options.project);
|
|
250
|
+
if (!projectName)
|
|
251
|
+
throw new Error('Could not resolve a target project from angular.json.');
|
|
252
|
+
const project = projects[projectName];
|
|
253
|
+
const sourceRoot = (project?.sourceRoot ?? (project?.root ? `${project.root}/src` : 'src')).replace(/^\/+/, '');
|
|
254
|
+
// `en` is the engine's fixed source locale — always part of the set.
|
|
255
|
+
const requested = (options.locales ?? 'en,es')
|
|
256
|
+
.split(',')
|
|
257
|
+
.map((s) => s.trim())
|
|
258
|
+
.filter(Boolean);
|
|
259
|
+
const locales = requested.includes('en') ? requested : ['en', ...requested];
|
|
260
|
+
return {
|
|
261
|
+
projectName,
|
|
262
|
+
sourceRoot,
|
|
263
|
+
definitionsDir: `${sourceRoot}/i18n/definitions`,
|
|
264
|
+
outputDir: `${sourceRoot}/assets/i18n`,
|
|
265
|
+
locales,
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function resolveProjectName(projects, explicit) {
|
|
269
|
+
if (explicit && projects[explicit])
|
|
270
|
+
return explicit;
|
|
271
|
+
for (const [name, project] of Object.entries(projects)) {
|
|
272
|
+
if (project?.projectType === 'application')
|
|
273
|
+
return name;
|
|
274
|
+
}
|
|
275
|
+
return Object.keys(projects)[0] ?? null;
|
|
276
|
+
}
|
|
277
|
+
function readJson(tree, path) {
|
|
278
|
+
const buffer = tree.read(path);
|
|
279
|
+
return buffer ? JSON.parse(buffer.toString('utf-8')) : null;
|
|
280
|
+
}
|
|
281
|
+
function writeJson(tree, path, value) {
|
|
282
|
+
tree.overwrite(path, JSON.stringify(value, null, 2) + '\n');
|
|
283
|
+
}
|
|
284
|
+
function readLibraryPeerDependencies() {
|
|
285
|
+
try {
|
|
286
|
+
const pkgPath = (0, path_1.join)(__dirname, '..', '..', 'package.json');
|
|
287
|
+
const parsed = JSON.parse((0, fs_1.readFileSync)(pkgPath, 'utf-8'));
|
|
288
|
+
return parsed.peerDependencies ?? {};
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
return {};
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function rel(path) {
|
|
295
|
+
return path.replace(/^\//, '');
|
|
296
|
+
}
|
|
297
|
+
/** Turn `src/i18n/definitions` + sourceRoot `src` into `i18n/definitions` (for an import relative to app). */
|
|
298
|
+
function trimLeadingSrc(definitionsDir, sourceRoot) {
|
|
299
|
+
return definitionsDir.startsWith(`${sourceRoot}/`) ? definitionsDir.slice(sourceRoot.length + 1) : definitionsDir;
|
|
300
|
+
}
|
|
301
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../projects/hakistack/i18n/schematics/ng-add/index.ts"],"names":[],"mappings":";;AAuBA,sBAgBC;AAvCD,2DAAiF;AACjF,4DAA0E;AAC1E,2BAAkC;AAClC,+BAA4B;AAG5B,MAAM,uBAAuB,GAAG,QAAQ,CAAC;AAiBzC,SAAgB,KAAK,CAAC,OAAe;IACnC,OAAO,CAAC,IAAU,EAAE,OAAyB,EAAE,EAAE;QAC/C,MAAM,KAAK,GAAG,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;QAC1C,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,sCAAsC,KAAK,CAAC,WAAW,eAAe,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAEvH,OAAO,IAAA,kBAAK,EAAC;YACX,sBAAsB,EAAE;YACxB,YAAY,CAAC,KAAK,CAAC;YACnB,aAAa,CAAC,OAAO,EAAE,KAAK,CAAC;YAC7B,iBAAiB,CAAC,OAAO,EAAE,KAAK,CAAC;YACjC,kBAAkB,CAAC,OAAO,EAAE,KAAK,CAAC;YAClC,YAAY,CAAC,KAAK,CAAC;YACnB,cAAc,CAA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+
"$schema": "http://json-schema.org/draft-07/schema",
|
|
3
|
+
"$id": "HakistackI18nNgAdd",
|
|
4
|
+
"title": "Hakistack i18n setup schematic",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"project": {
|
|
8
|
+
"type": "string",
|
|
9
|
+
"description": "Target project in angular.json. Defaults to the first application project.",
|
|
10
|
+
"$default": {
|
|
11
|
+
"$source": "projectName"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"locales": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"description": "Comma-separated locale ids. `en` is always the source locale and is included automatically.",
|
|
17
|
+
"default": "en,es",
|
|
18
|
+
"x-prompt": "Which locales do you want to support? (comma-separated, e.g. en,es)"
|
|
19
|
+
},
|
|
20
|
+
"skipInstall": {
|
|
21
|
+
"type": "boolean",
|
|
22
|
+
"default": false,
|
|
23
|
+
"description": "Skip running the package install after adding @jsverse/transloco."
|
|
24
|
+
},
|
|
25
|
+
"skipScripts": {
|
|
26
|
+
"type": "boolean",
|
|
27
|
+
"default": false,
|
|
28
|
+
"description": "Skip adding the i18n:* npm scripts and prestart/prebuild chain."
|
|
29
|
+
},
|
|
30
|
+
"skipSample": {
|
|
31
|
+
"type": "boolean",
|
|
32
|
+
"default": false,
|
|
33
|
+
"description": "Skip creating the sample *.i18n.ts module and locale .d.ts."
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"required": []
|
|
37
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-safe translation definitions — the authoring heart of `@hakistack/i18n`.
|
|
3
|
+
*
|
|
4
|
+
* Consumers author translations in `.ts` files (`*.i18n.ts`) using
|
|
5
|
+
* `defineTranslations()` + `t()`. English is the required source locale; every
|
|
6
|
+
* other locale is optional. The companion `keys` object gives type-safe
|
|
7
|
+
* dot-path access at the value level, and the scanner derives a merged `TK`
|
|
8
|
+
* literal from these modules.
|
|
9
|
+
*
|
|
10
|
+
* Unlike the original app-local engine, locales here are **not hardcoded**.
|
|
11
|
+
* Consumers declare their locale union by augmenting the {@link I18nLocales}
|
|
12
|
+
* interface (the scanner / schematic can emit this augmentation), so `t()` gets
|
|
13
|
+
* autocomplete on exactly the locales the project supports.
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* The set of locales a project supports, as a type.
|
|
17
|
+
*
|
|
18
|
+
* `en` is the always-present source locale (English is the source of truth).
|
|
19
|
+
* Consumers add their other locales by declaration-merging this interface:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* declare module '@hakistack/i18n/engine' {
|
|
23
|
+
* interface I18nLocales {
|
|
24
|
+
* es: string;
|
|
25
|
+
* fr: string;
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* After augmentation, `t('Hello', { es: '…', fr: '…' })` autocompletes `es`/`fr`
|
|
31
|
+
* and `Language` resolves to `'en' | 'es' | 'fr'`.
|
|
32
|
+
*/
|
|
33
|
+
interface I18nLocales {
|
|
34
|
+
/** Source locale — always required on every translation value. */
|
|
35
|
+
en: string;
|
|
36
|
+
}
|
|
37
|
+
/** Union of every declared locale id (e.g. `'en' | 'es'`). */
|
|
38
|
+
type LocaleId = keyof I18nLocales & string;
|
|
39
|
+
/**
|
|
40
|
+
* The source locale. `t()` stores its first argument here, every other locale
|
|
41
|
+
* is optional, and build-time + runtime fallbacks end here. This is fixed by
|
|
42
|
+
* the engine's types (`TranslationValue.en`), so it is not configurable.
|
|
43
|
+
*/
|
|
44
|
+
declare const SOURCE_LANG = "en";
|
|
45
|
+
type SourceLang = typeof SOURCE_LANG;
|
|
46
|
+
/**
|
|
47
|
+
* Runtime brand stamped (non-enumerably) on every `TranslationValue` produced
|
|
48
|
+
* by `t()` or by bare-string normalization.
|
|
49
|
+
*
|
|
50
|
+
* Without a brand, a *group* whose child happens to be named `en` — e.g.
|
|
51
|
+
* `languages: { en: 'English', es: 'Español' }` in a language picker — is
|
|
52
|
+
* indistinguishable from a leaf. `Symbol.for` (global registry) is used so the
|
|
53
|
+
* codegen CLI (which bundles its own copy of the engine) recognizes values
|
|
54
|
+
* created by the consumer's installed copy.
|
|
55
|
+
*/
|
|
56
|
+
declare const TRANSLATION_BRAND: unique symbol;
|
|
57
|
+
/** Alias kept for ergonomics — the active/target language id. */
|
|
58
|
+
type Language = LocaleId;
|
|
59
|
+
/**
|
|
60
|
+
* A single translation value across all declared locales.
|
|
61
|
+
*
|
|
62
|
+
* English (`en`) is required; every other declared locale is optional and falls
|
|
63
|
+
* back to English at build time and runtime. The optional `__comment` field
|
|
64
|
+
* stores translator-facing context — it is emitted as a sibling `<key>.comment`
|
|
65
|
+
* entry in the JSON and stripped by the runtime interceptor so it never renders.
|
|
66
|
+
*/
|
|
67
|
+
type TranslationValue = OtherLocales & {
|
|
68
|
+
en: string;
|
|
69
|
+
/** Internal — translator-context comment, attached via `t(en, others, { comment })`. */
|
|
70
|
+
__comment?: string;
|
|
71
|
+
/** Internal — runtime brand; only `t()` / normalization create branded values. */
|
|
72
|
+
readonly [TRANSLATION_BRAND]: true;
|
|
73
|
+
};
|
|
74
|
+
/** Optional non-source locale fields, derived from the augmented locale set. */
|
|
75
|
+
type OtherLocales = {
|
|
76
|
+
[K in Exclude<LocaleId, 'en'>]?: string;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Define a single translation value.
|
|
80
|
+
*
|
|
81
|
+
* @param en - English (source) translation — required.
|
|
82
|
+
* @param translations - Other-locale translations — optional, autocompletes the
|
|
83
|
+
* consumer's declared locales.
|
|
84
|
+
* @param options.comment - Translator-facing context. Emitted as a sibling
|
|
85
|
+
* `<key>.comment` entry in the JSON for translator review; stripped at runtime
|
|
86
|
+
* by the comment interceptor so it never renders.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* t('Hello', { es: 'Hola' })
|
|
90
|
+
* t('Save') // English only — other locales fall back to English
|
|
91
|
+
* t('Welcome', { es: '¡Bienvenido!' }, { comment: 'Greeting on home dashboard' })
|
|
92
|
+
*/
|
|
93
|
+
declare function t(en: string, translations?: OtherLocales, options?: {
|
|
94
|
+
comment?: string;
|
|
95
|
+
}): TranslationValue;
|
|
96
|
+
/**
|
|
97
|
+
* Authored translation node — what callers write inside `defineTranslations()`.
|
|
98
|
+
* Accepts:
|
|
99
|
+
* - `TranslationValue` (full form, from `t()`)
|
|
100
|
+
* - `string` (shorthand for English-only — equivalent to `t(value)`)
|
|
101
|
+
* - nested object of either
|
|
102
|
+
*
|
|
103
|
+
* Only `t()` produces a leaf. A hand-written `{ en: '…', es: '…' }` object is a
|
|
104
|
+
* *group* with children named `en`/`es` — which is what a language picker wants.
|
|
105
|
+
*
|
|
106
|
+
* Strings are normalized to `TranslationValue` at module construction so the
|
|
107
|
+
* rest of the pipeline (generator, validator, keys builder) only ever sees the
|
|
108
|
+
* canonical `{ en, … }` shape.
|
|
109
|
+
*
|
|
110
|
+
* Note: any leaf string containing `{{…}}` placeholders MUST use `t()` — the
|
|
111
|
+
* validator rejects bare placeholder strings because parity across locales
|
|
112
|
+
* cannot be expressed with the shorthand.
|
|
113
|
+
*/
|
|
114
|
+
type AuthoredTranslationNode = TranslationValue | string | {
|
|
115
|
+
[key: string]: AuthoredTranslationNode;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Recursively builds dot-notation key strings from the authored object shape.
|
|
119
|
+
* Treats both `TranslationValue` and `string` as leaves (string is shorthand).
|
|
120
|
+
*/
|
|
121
|
+
type BuildKeyObject<T, Prefix extends string = ''> = {
|
|
122
|
+
readonly [K in keyof T]: K extends string ? T[K] extends TranslationValue | string ? Prefix extends '' ? K : `${Prefix}.${K}` : T[K] extends Record<string, unknown> ? BuildKeyObject<T[K], Prefix extends '' ? K : `${Prefix}.${K}`> : never : never;
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Translation module returned by {@link defineTranslations}.
|
|
126
|
+
*
|
|
127
|
+
* `translations` stores the normalized form (every bare string promoted to
|
|
128
|
+
* `{ en: value }`); `keys` is the type-derived companion of dot-paths.
|
|
129
|
+
*/
|
|
130
|
+
interface TranslationModule<T extends Record<string, AuthoredTranslationNode> = Record<string, AuthoredTranslationNode>> {
|
|
131
|
+
/** Scope/prefix for these translations (e.g. `'common'`, `'administration.users'`). */
|
|
132
|
+
scope: string;
|
|
133
|
+
/** Normalized translation definitions (strings promoted to `{ en: value }`). */
|
|
134
|
+
translations: T;
|
|
135
|
+
/** Auto-generated keys object for type-safe usage. */
|
|
136
|
+
keys: BuildKeyObject<T>;
|
|
137
|
+
/**
|
|
138
|
+
* If `true`, the generator emits this module's data to a subdirectory matching
|
|
139
|
+
* its scope path (`administration.users` → `administration/users/<lang>.json`)
|
|
140
|
+
* rather than merging into the root `<lang>.json`. Future-proofs
|
|
141
|
+
* `provideTranslocoScope`-based lazy loading. Default: `false` (eager).
|
|
142
|
+
*/
|
|
143
|
+
scoped: boolean;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Define a translation module with auto-generated keys.
|
|
147
|
+
*
|
|
148
|
+
* @param scope - The scope/prefix for these translations.
|
|
149
|
+
* @param translations - The translation definitions (full `t()` form or
|
|
150
|
+
* bare-string shorthand, nested freely).
|
|
151
|
+
* @param options.scoped - Emit to a per-scope JSON file (see {@link TranslationModule.scoped}).
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* export const COMMON = defineTranslations('common', {
|
|
155
|
+
* buttons: {
|
|
156
|
+
* save: t('Save', { es: 'Guardar' }),
|
|
157
|
+
* cancel: t('Cancel', { es: 'Cancelar' }),
|
|
158
|
+
* reset: 'Reset', // shorthand
|
|
159
|
+
* },
|
|
160
|
+
* });
|
|
161
|
+
* // COMMON.keys.buttons.save === 'common.buttons.save'
|
|
162
|
+
*/
|
|
163
|
+
declare function defineTranslations<T extends Record<string, AuthoredTranslationNode>>(scope: string, translations: T, options?: {
|
|
164
|
+
scoped?: boolean;
|
|
165
|
+
}): TranslationModule<T>;
|
|
166
|
+
/** Type guard: is this value a branded `TranslationValue` (created by `t()` or normalization)? */
|
|
167
|
+
declare function isTranslationValue(value: unknown): value is TranslationValue;
|
|
168
|
+
/**
|
|
169
|
+
* Build a merged keys tree from a list of translation modules by placing each
|
|
170
|
+
* module's `keys` object at its scope path and deep-merging.
|
|
171
|
+
*
|
|
172
|
+
* This is the **runtime** value used by the directive (via the `I18N_KEYS`
|
|
173
|
+
* token) — it produces the exact nested shape the scanner emits as the typed
|
|
174
|
+
* `TK` literal. The scanner emits a concrete literal for *compile-time*
|
|
175
|
+
* autocomplete; this function produces the identical structure at runtime, so
|
|
176
|
+
* the two never diverge.
|
|
177
|
+
*
|
|
178
|
+
* Modules sharing a scope are merged (the scanner emits the same spread for the
|
|
179
|
+
* `TK` literal). Throws on scalar/scope conflicts.
|
|
180
|
+
*/
|
|
181
|
+
declare function buildKeysTree(modules: readonly TranslationModule[]): Record<string, unknown>;
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Build a per-language translation tree from the in-memory module registry.
|
|
185
|
+
*
|
|
186
|
+
* Shared by:
|
|
187
|
+
* - the generator CLI — writes the tree as JSON to `<outputDir>/<lang>.json`
|
|
188
|
+
* for translator-facing tooling (build artifacts, not a runtime dependency).
|
|
189
|
+
* - the runtime `TranslocoRegistryLoader` — returns the tree synchronously to
|
|
190
|
+
* Transloco, eliminating the HTTP fetch.
|
|
191
|
+
*
|
|
192
|
+
* Producing the tree from the registry guarantees the runtime sees the exact
|
|
193
|
+
* shape the generator emits — there's no "JSON drifted from TS" failure mode.
|
|
194
|
+
*
|
|
195
|
+
* Locale handling is parameterized: every function takes the `defaultLang`
|
|
196
|
+
* (source/fallback locale) explicitly rather than reading a hardcoded constant,
|
|
197
|
+
* so the engine is reusable across consumers with different locale sets.
|
|
198
|
+
*/
|
|
199
|
+
|
|
200
|
+
/** A nested string-tree as Transloco / the JSON files consume it. */
|
|
201
|
+
interface LanguageTree {
|
|
202
|
+
[key: string]: string | LanguageTree;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Resolve a single `TranslationValue` for a given language.
|
|
206
|
+
* Falls back to `defaultLang` for any locale that didn't supply a translation —
|
|
207
|
+
* mirrors `useFallbackTranslation: true` in the runtime Transloco config.
|
|
208
|
+
*/
|
|
209
|
+
declare function getTranslation(value: TranslationValue, lang: string, defaultLang: string): string;
|
|
210
|
+
/**
|
|
211
|
+
* Walk a normalized translation subtree (all leaves are `TranslationValue`s) and
|
|
212
|
+
* emit a string-only tree for the given language.
|
|
213
|
+
*
|
|
214
|
+
* If a leaf carries a `__comment` (set via `t(en, others, { comment })`), a
|
|
215
|
+
* sibling entry `<key>.comment` is added at the same level. Translators see it;
|
|
216
|
+
* the runtime interceptor strips it before it reaches the store.
|
|
217
|
+
*/
|
|
218
|
+
declare function extractLanguage(translations: Record<string, unknown>, lang: string, defaultLang: string): LanguageTree;
|
|
219
|
+
/**
|
|
220
|
+
* Place a subtree at a dot-delimited scope path in the target tree, creating
|
|
221
|
+
* intermediate objects as needed. If the path already holds an object, the new
|
|
222
|
+
* subtree's leaves are merged into it.
|
|
223
|
+
*/
|
|
224
|
+
declare function setAtScope(target: LanguageTree, scope: string, subtree: LanguageTree): void;
|
|
225
|
+
/**
|
|
226
|
+
* Build the complete translation tree for a single language from the registry.
|
|
227
|
+
* The returned tree matches the JSON file layout exactly — same keys, nesting,
|
|
228
|
+
* and fallbacks — so a consumer cannot tell whether the data came from the
|
|
229
|
+
* registry or from disk.
|
|
230
|
+
*/
|
|
231
|
+
declare function buildLanguageTree(registry: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[], lang: string, defaultLang: string): LanguageTree;
|
|
232
|
+
/**
|
|
233
|
+
* Build the eager (root) tree — only modules declared without `{ scoped: true }`.
|
|
234
|
+
* Used by the generator when route-level scope providers are adopted.
|
|
235
|
+
*/
|
|
236
|
+
declare function buildEagerTree(registry: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[], lang: string, defaultLang: string): LanguageTree;
|
|
237
|
+
/**
|
|
238
|
+
* Build the per-scope tree for a single scoped module — returned unwrapped
|
|
239
|
+
* (no scope key around it), matching the layout `provideTranslocoScope` expects
|
|
240
|
+
* to load from `<scope-with-slashes>/<lang>.json`.
|
|
241
|
+
*
|
|
242
|
+
* `scope` may be given in dotted (`administration.users`) or slash form
|
|
243
|
+
* (`administration/users` — what Transloco hands a loader); both match.
|
|
244
|
+
*
|
|
245
|
+
* Returns `null` if no module with the given scope exists, or if it's eager.
|
|
246
|
+
*/
|
|
247
|
+
declare function buildScopedTree(registry: readonly TranslationModule<Record<string, AuthoredTranslationNode>>[], lang: string, defaultLang: string, scope: string): LanguageTree | null;
|
|
248
|
+
/**
|
|
249
|
+
* Convert a dot-separated scope to its filesystem path.
|
|
250
|
+
* 'administration.users' → 'administration/users'
|
|
251
|
+
*/
|
|
252
|
+
declare function scopeToFilesystemPath(scope: string): string;
|
|
253
|
+
/**
|
|
254
|
+
* Canonical dotted form of a scope, accepting either separator.
|
|
255
|
+
* 'administration/users' → 'administration.users'
|
|
256
|
+
*/
|
|
257
|
+
declare function normalizeScope(scope: string): string;
|
|
258
|
+
|
|
259
|
+
export { SOURCE_LANG, TRANSLATION_BRAND, buildEagerTree, buildKeysTree, buildLanguageTree, buildScopedTree, defineTranslations, extractLanguage, getTranslation, isTranslationValue, normalizeScope, scopeToFilesystemPath, setAtScope, t };
|
|
260
|
+
export type { AuthoredTranslationNode, I18nLocales, Language, LanguageTree, LocaleId, OtherLocales, SourceLang, TranslationModule, TranslationValue };
|