poly-lexis 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/README.md +280 -0
- package/dist/cli/translations.d.ts +2 -0
- package/dist/cli/translations.js +1364 -0
- package/dist/cli/translations.js.map +1 -0
- package/dist/index.d.ts +244 -0
- package/dist/index.js +1016 -0
- package/dist/index.js.map +1 -0
- package/dist/translations/core/translations-config.schema.json +94 -0
- package/package.json +65 -0
package/README.md
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# Lexis
|
|
2
|
+
|
|
3
|
+
A powerful CLI and library for managing i18n translations with validation, auto-translation, and TypeScript type generation.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Lexis provides a complete solution for managing internationalization (i18n) in your applications. It offers smart translation management with automatic validation, Google Translate integration for auto-filling missing translations, and TypeScript type generation for type-safe translations.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install lexis
|
|
13
|
+
# or
|
|
14
|
+
yarn add lexis
|
|
15
|
+
# or
|
|
16
|
+
pnpm add lexis
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# Initialize translations in your project
|
|
23
|
+
npx lexis
|
|
24
|
+
|
|
25
|
+
# Add a new translation key
|
|
26
|
+
npx lexis add
|
|
27
|
+
|
|
28
|
+
# Auto-fill missing translations
|
|
29
|
+
export GOOGLE_TRANSLATE_API_KEY=your_key
|
|
30
|
+
npx lexis --auto-fill
|
|
31
|
+
|
|
32
|
+
# Validate and generate types
|
|
33
|
+
npx lexis
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Features
|
|
37
|
+
|
|
38
|
+
- ✅ **Smart translation management** - Automatic initialization and validation
|
|
39
|
+
- ✅ **Interactive CLI** - User-friendly prompts for all operations
|
|
40
|
+
- ✅ **Auto-translation** - Google Translate integration for missing translations
|
|
41
|
+
- ✅ **Type generation** - Generate TypeScript types for type-safe translations
|
|
42
|
+
- ✅ **Validation** - Detect missing or empty translation keys
|
|
43
|
+
- ✅ **Multiple namespaces** - Organize translations by feature/domain
|
|
44
|
+
- ✅ **Programmatic API** - Use as a library in your Node.js code
|
|
45
|
+
|
|
46
|
+
## CLI Usage
|
|
47
|
+
|
|
48
|
+
### Smart Mode (Default)
|
|
49
|
+
|
|
50
|
+
Run without any command to validate, auto-fill (optional), and generate types:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
# Basic validation and type generation
|
|
54
|
+
lexis
|
|
55
|
+
|
|
56
|
+
# With auto-translation
|
|
57
|
+
lexis --auto-fill
|
|
58
|
+
|
|
59
|
+
# Auto-fill only specific language
|
|
60
|
+
lexis --auto-fill --language fr
|
|
61
|
+
|
|
62
|
+
# Dry run to preview changes
|
|
63
|
+
lexis --auto-fill --dry-run
|
|
64
|
+
|
|
65
|
+
# Skip type generation
|
|
66
|
+
lexis --skip-types
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Add Translation Keys
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
# Interactive mode
|
|
73
|
+
lexis add
|
|
74
|
+
|
|
75
|
+
# With flags
|
|
76
|
+
lexis add --namespace common --key HELLO --value "Hello"
|
|
77
|
+
|
|
78
|
+
# With auto-translation
|
|
79
|
+
lexis add -n common -k WELCOME -v "Welcome" --auto-fill
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### CLI Options
|
|
83
|
+
|
|
84
|
+
**Smart Mode:**
|
|
85
|
+
- `-a, --auto-fill` - Auto-fill missing translations with Google Translate
|
|
86
|
+
- `--api-key <key>` - Google Translate API key (or set GOOGLE_TRANSLATE_API_KEY env var)
|
|
87
|
+
- `-l, --language <lang>` - Process only this language
|
|
88
|
+
- `--limit <number>` - Max translations to process (default: 1000)
|
|
89
|
+
- `--skip-types` - Skip TypeScript type generation
|
|
90
|
+
- `-d, --dry-run` - Preview changes without saving
|
|
91
|
+
|
|
92
|
+
**Add Mode:**
|
|
93
|
+
- `-n, --namespace <name>` - Namespace for the translation
|
|
94
|
+
- `-k, --key <key>` - Translation key
|
|
95
|
+
- `-v, --value <value>` - Translation value in source language
|
|
96
|
+
- `-a, --auto-fill` - Auto-translate to all languages
|
|
97
|
+
|
|
98
|
+
## Configuration
|
|
99
|
+
|
|
100
|
+
Lexis uses a `.translationsrc.json` file in your project root for configuration:
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"translationsPath": "public/static/locales",
|
|
105
|
+
"languages": ["en", "es", "fr", "de"],
|
|
106
|
+
"sourceLanguage": "en",
|
|
107
|
+
"typesOutputPath": "src/types/i18nTypes.ts"
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Configuration Options
|
|
112
|
+
|
|
113
|
+
- `translationsPath` - Path to the translations directory (default: `public/static/locales`)
|
|
114
|
+
- `languages` - Array of language codes to support (default: `["en"]`)
|
|
115
|
+
- `sourceLanguage` - Source language for translations (default: `"en"`)
|
|
116
|
+
- `typesOutputPath` - Path to output TypeScript types (default: `src/types/i18nTypes.ts`)
|
|
117
|
+
|
|
118
|
+
### Environment Variables
|
|
119
|
+
|
|
120
|
+
- `GOOGLE_TRANSLATE_API_KEY` - Google Translate API key for auto-translation
|
|
121
|
+
|
|
122
|
+
### Directory Structure
|
|
123
|
+
|
|
124
|
+
After initialization, your project will have this structure:
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
your-app/
|
|
128
|
+
├── .translationsrc.json # Configuration file
|
|
129
|
+
├── public/
|
|
130
|
+
│ └── static/
|
|
131
|
+
│ └── locales/
|
|
132
|
+
│ ├── en/
|
|
133
|
+
│ │ ├── common.json
|
|
134
|
+
│ │ └── errors.json
|
|
135
|
+
│ ├── es/
|
|
136
|
+
│ │ ├── common.json
|
|
137
|
+
│ │ └── errors.json
|
|
138
|
+
│ └── fr/
|
|
139
|
+
│ ├── common.json
|
|
140
|
+
│ └── errors.json
|
|
141
|
+
└── src/
|
|
142
|
+
└── types/
|
|
143
|
+
└── i18nTypes.ts # Generated TypeScript types
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Translation File Format
|
|
147
|
+
|
|
148
|
+
Translation files are organized by namespace and language:
|
|
149
|
+
|
|
150
|
+
```json
|
|
151
|
+
{
|
|
152
|
+
"HELLO": "Hello",
|
|
153
|
+
"WELCOME": "Welcome to our app",
|
|
154
|
+
"GOODBYE": "Goodbye"
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## Programmatic API
|
|
159
|
+
|
|
160
|
+
Lexis can be used as a library in your Node.js code:
|
|
161
|
+
|
|
162
|
+
### Initialize Translations
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import { initTranslationsInteractive } from 'lexis';
|
|
166
|
+
|
|
167
|
+
await initTranslationsInteractive(process.cwd());
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Add Translation Key
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
import { addTranslationKey } from 'lexis';
|
|
174
|
+
|
|
175
|
+
await addTranslationKey(process.cwd(), {
|
|
176
|
+
namespace: 'common',
|
|
177
|
+
key: 'HELLO',
|
|
178
|
+
value: 'Hello',
|
|
179
|
+
autoTranslate: true,
|
|
180
|
+
apiKey: process.env.GOOGLE_TRANSLATE_API_KEY,
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Validate Translations
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { validateTranslations } from 'lexis';
|
|
188
|
+
|
|
189
|
+
const result = await validateTranslations(
|
|
190
|
+
'/path/to/translations',
|
|
191
|
+
['en', 'es', 'fr'],
|
|
192
|
+
'en'
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
if (!result.valid) {
|
|
196
|
+
console.log('Missing translations:', result.missing);
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Generate TypeScript Types
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { generateTranslationTypes } from 'lexis';
|
|
204
|
+
|
|
205
|
+
generateTranslationTypes(process.cwd());
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Auto-fill Missing Translations
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
import { autoFillTranslations } from 'lexis';
|
|
212
|
+
|
|
213
|
+
await autoFillTranslations({
|
|
214
|
+
translationsPath: '/path/to/translations',
|
|
215
|
+
languages: ['es', 'fr'],
|
|
216
|
+
sourceLanguage: 'en',
|
|
217
|
+
apiKey: process.env.GOOGLE_TRANSLATE_API_KEY,
|
|
218
|
+
limit: 1000,
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Development
|
|
223
|
+
|
|
224
|
+
The package uses TypeScript and tsup for building:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
# Install dependencies
|
|
228
|
+
npm install
|
|
229
|
+
|
|
230
|
+
# Build the package
|
|
231
|
+
npm run build
|
|
232
|
+
|
|
233
|
+
# Watch mode for type checking
|
|
234
|
+
npm run dev
|
|
235
|
+
|
|
236
|
+
# Lint and format
|
|
237
|
+
npm run lint
|
|
238
|
+
npm run format
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### Project Structure
|
|
242
|
+
|
|
243
|
+
```
|
|
244
|
+
src/
|
|
245
|
+
├── index.ts # Main library export
|
|
246
|
+
├── cli/
|
|
247
|
+
│ └── translations.ts # CLI entry point
|
|
248
|
+
└── translations/
|
|
249
|
+
├── cli/ # CLI command implementations
|
|
250
|
+
│ ├── init.ts
|
|
251
|
+
│ ├── init-interactive.ts
|
|
252
|
+
│ ├── add-key.ts
|
|
253
|
+
│ ├── validate.ts
|
|
254
|
+
│ ├── auto-fill.ts
|
|
255
|
+
│ ├── generate-types.ts
|
|
256
|
+
│ └── manage.ts
|
|
257
|
+
├── core/ # Core types and schemas
|
|
258
|
+
│ ├── types.ts
|
|
259
|
+
│ ├── schema.ts
|
|
260
|
+
│ └── translations-config.schema.json
|
|
261
|
+
└── utils/ # Utility functions
|
|
262
|
+
├── utils.ts
|
|
263
|
+
└── translator.ts
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## Requirements
|
|
267
|
+
|
|
268
|
+
- Node.js 18+
|
|
269
|
+
- (Optional) Google Translate API key for auto-translation
|
|
270
|
+
|
|
271
|
+
## How It Works
|
|
272
|
+
|
|
273
|
+
1. **Initialization**: Creates `.translationsrc.json` and translation directory structure
|
|
274
|
+
2. **Validation**: Compares all language files against source language to find missing keys
|
|
275
|
+
3. **Auto-translation**: Uses Google Translate API to fill missing translations
|
|
276
|
+
4. **Type Generation**: Creates TypeScript types from translation keys for autocomplete and type safety
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
MIT
|