@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hakistack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,152 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hakistack/i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
TypeScript-first, build-time-generated, **type-safe i18n for Angular** on top of
|
|
4
|
+
[Transloco](https://jsverse.github.io/transloco/). Author translations in `.ts`
|
|
5
|
+
files; the codegen derives the JSON files, the typed key object (`TK`), and a
|
|
6
|
+
typed template directive. There is no "JSON drifted from code" failure mode —
|
|
7
|
+
JSON is an output, not an input.
|
|
8
|
+
|
|
9
|
+
> Transloco is the runtime translation engine (active language, interpolation,
|
|
10
|
+
> fallback). Everything you interact with — authoring, key generation,
|
|
11
|
+
> validation, autocomplete — is this library's layer on top.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm i @hakistack/i18n @jsverse/transloco
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`@jsverse/transloco` is a peer dependency (you control its version). The CLI
|
|
20
|
+
depends on `jiti` (installed automatically) and, for the `watch` command only,
|
|
21
|
+
on `chokidar` (an optional dependency).
|
|
22
|
+
|
|
23
|
+
## 1. Author translations (`*.i18n.ts`)
|
|
24
|
+
|
|
25
|
+
Author against the Angular-free `/engine` entry point:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// src/app/features/home/home.i18n.ts
|
|
29
|
+
import { defineTranslations, t } from '@hakistack/i18n/engine';
|
|
30
|
+
|
|
31
|
+
export const HOME = defineTranslations('home', {
|
|
32
|
+
welcome: t('Welcome, Admin!', { es: '¡Bienvenido, Administrador!' }),
|
|
33
|
+
}, { scoped: true });
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- `t(en, others?, { comment? })` — English (source) required, other locales
|
|
37
|
+
optional, optional translator comment.
|
|
38
|
+
- Bare strings are shorthand for English-only (`reset: 'Reset'` ≡ `t('Reset')`).
|
|
39
|
+
- Only `t()` and bare strings are leaves. A hand-written object is always a
|
|
40
|
+
*group*, so `languages: { en: 'English', es: 'Español' }` yields the two keys
|
|
41
|
+
`languages.en` and `languages.es` — handy for language pickers.
|
|
42
|
+
- `en` is the fixed source locale: it is what `t()`'s first argument fills and
|
|
43
|
+
what every other locale falls back to at build time and at runtime.
|
|
44
|
+
- `{ scoped: true }` emits a per-scope JSON file (forward-compat for
|
|
45
|
+
`provideTranslocoScope` lazy loading); eager modules merge into the root JSON.
|
|
46
|
+
|
|
47
|
+
Declare your locale set once so `t()` autocompletes the right locales:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// src/app/i18n-locales.d.ts
|
|
51
|
+
import '@hakistack/i18n/engine';
|
|
52
|
+
declare module '@hakistack/i18n/engine' {
|
|
53
|
+
interface I18nLocales {
|
|
54
|
+
es: string;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## 2. Configure the codegen (`i18n.config.json`)
|
|
60
|
+
|
|
61
|
+
```jsonc
|
|
62
|
+
{
|
|
63
|
+
"srcRoot": "src",
|
|
64
|
+
"definitionsDir": "src/i18n/definitions",
|
|
65
|
+
"outputDir": "src/assets/i18n",
|
|
66
|
+
"locales": ["en", "es"]
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
(A `.ts`/`.js` config with a default export works too. `en` is always part of
|
|
71
|
+
`locales`; optional `directiveSelector` / `directiveClassName` rename the
|
|
72
|
+
generated directive.)
|
|
73
|
+
|
|
74
|
+
## 3. Run the codegen
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
npx hakistack-i18n scan # emit barrel + TK literal + typed directive
|
|
78
|
+
npx hakistack-i18n generate # write JSON translation files
|
|
79
|
+
npx hakistack-i18n validate # gate on missing / placeholder-mismatched strings
|
|
80
|
+
npx hakistack-i18n watch # re-run scan on *.i18n.ts changes (during ng serve)
|
|
81
|
+
npx hakistack-i18n find-unused # report defined keys with no references
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Typical `package.json` wiring:
|
|
85
|
+
|
|
86
|
+
```jsonc
|
|
87
|
+
{
|
|
88
|
+
"scripts": {
|
|
89
|
+
"i18n:scan": "hakistack-i18n scan",
|
|
90
|
+
"i18n:generate": "hakistack-i18n generate",
|
|
91
|
+
"i18n:validate": "hakistack-i18n validate",
|
|
92
|
+
"prestart": "npm run i18n:scan",
|
|
93
|
+
"prebuild": "npm run i18n:scan && npm run i18n:generate && npm run i18n:validate"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The scan emits three files into `definitionsDir` (never hand-edited):
|
|
99
|
+
`index.generated.ts` (the `ALL_TRANSLATIONS` registry), `keys.generated.ts`
|
|
100
|
+
(the flat camelCase `TK` literal — `TK.home.welcome === 'home.welcome'`), and
|
|
101
|
+
`appTranslate.directive.ts` (the typed template directive).
|
|
102
|
+
|
|
103
|
+
## 4. Wire it up (one call)
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
// app.config.ts
|
|
107
|
+
import { provideI18n } from '@hakistack/i18n';
|
|
108
|
+
import { ALL_TRANSLATIONS } from './i18n/definitions/index.generated';
|
|
109
|
+
|
|
110
|
+
export const appConfig: ApplicationConfig = {
|
|
111
|
+
providers: [
|
|
112
|
+
provideI18n({
|
|
113
|
+
registry: ALL_TRANSLATIONS,
|
|
114
|
+
languages: [
|
|
115
|
+
{ id: 'en', label: 'English' },
|
|
116
|
+
{ id: 'es', label: 'Español' },
|
|
117
|
+
],
|
|
118
|
+
defaultLang: 'es', // active language on bootstrap
|
|
119
|
+
fallbackLang: 'en', // untranslated keys fall back here
|
|
120
|
+
persistLanguage: true, // restore last language from localStorage
|
|
121
|
+
}),
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
No HTTP fetch by default — the registry loader serves translations from memory
|
|
127
|
+
(SSR-safe, offline-friendly). Pass `loader: 'http'` to fetch `*.json` instead.
|
|
128
|
+
|
|
129
|
+
## 5. Use translations
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
// component
|
|
133
|
+
import { translateSignal } from '@hakistack/i18n';
|
|
134
|
+
import { TK } from '../../i18n/definitions/keys.generated';
|
|
135
|
+
|
|
136
|
+
protected readonly welcome = translateSignal(TK.home.welcome);
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
```html
|
|
140
|
+
<!-- template — import the generated AppTranslateDirective, fully typed -->
|
|
141
|
+
<div *appTranslate="let t">{{ t.home.welcome() }}</div>
|
|
142
|
+
<p>{{ t.common.validation.minLength({ min: 3 }) }}</p>
|
|
143
|
+
<!-- optional key prefix (the input is named <selector>Prefix) -->
|
|
144
|
+
<div *appTranslate="let t; prefix: 'admin'">{{ t.users.title() }}</div>
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Entry points
|
|
148
|
+
|
|
149
|
+
| Import | Contains |
|
|
150
|
+
|--------|----------|
|
|
151
|
+
| `@hakistack/i18n` | Runtime: `provideI18n`, `LanguageService`, `translateSignal`, `TranslateDirectiveBase`, loaders/hooks. (Re-exports the engine too.) |
|
|
152
|
+
| `@hakistack/i18n/engine` | Angular-free authoring/tree engine: `defineTranslations`, `t`, build-tree helpers. Loads in Node — this is what the codegen executes. |
|