@shipi18n/cli 1.1.4 ā 2.0.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/CHANGELOG.md +9 -0
- package/LICENSE +201 -0
- package/NOTICE +16 -0
- package/README.md +26 -568
- package/bin/shipi18n.js +25 -45
- package/package.json +42 -48
- package/src/commands/translate.js +73 -316
- package/src/commands/config.js +0 -94
- package/src/commands/init.js +0 -443
- package/src/commands/keys.js +0 -128
- package/src/lib/api.js +0 -342
- package/src/lib/config.js +0 -74
- package/src/utils/incremental.js +0 -90
- package/src/utils/logger.js +0 -46
package/package.json
CHANGED
|
@@ -1,67 +1,61 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shipi18n/cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "src/index.js",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Open-source, bring-your-own-LLM i18n translation CLI. Translate locale files with your own OpenAI/Anthropic key.",
|
|
6
5
|
"type": "module",
|
|
7
|
-
"files": [
|
|
8
|
-
"bin",
|
|
9
|
-
"src/commands",
|
|
10
|
-
"src/lib",
|
|
11
|
-
"src/utils",
|
|
12
|
-
"src/index.js",
|
|
13
|
-
"README.md"
|
|
14
|
-
],
|
|
15
6
|
"bin": {
|
|
16
7
|
"shipi18n": "./bin/shipi18n.js"
|
|
17
8
|
},
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src",
|
|
12
|
+
"LICENSE",
|
|
13
|
+
"NOTICE",
|
|
14
|
+
"README.md",
|
|
15
|
+
"CHANGELOG.md"
|
|
16
|
+
],
|
|
17
|
+
"publishConfig": {
|
|
18
|
+
"access": "public"
|
|
24
19
|
},
|
|
25
20
|
"keywords": [
|
|
26
21
|
"i18n",
|
|
22
|
+
"cli",
|
|
27
23
|
"translation",
|
|
24
|
+
"llm",
|
|
25
|
+
"openai",
|
|
26
|
+
"anthropic",
|
|
28
27
|
"localization",
|
|
29
|
-
"
|
|
30
|
-
"locale",
|
|
31
|
-
"translate",
|
|
32
|
-
"cli",
|
|
33
|
-
"i18next",
|
|
34
|
-
"react-intl",
|
|
35
|
-
"json-translation",
|
|
36
|
-
"locale-files",
|
|
37
|
-
"multilingual",
|
|
38
|
-
"l10n",
|
|
39
|
-
"ai-translation",
|
|
40
|
-
"shipi18n"
|
|
28
|
+
"bring-your-own-key"
|
|
41
29
|
],
|
|
30
|
+
"license": "Apache-2.0",
|
|
42
31
|
"author": "Shipi18n",
|
|
43
|
-
"
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/Shipi18n/shipi18n.git",
|
|
35
|
+
"directory": "packages/cli"
|
|
36
|
+
},
|
|
44
37
|
"dependencies": {
|
|
45
|
-
"archiver": "^7.0.1",
|
|
46
38
|
"chalk": "^5.3.0",
|
|
47
|
-
"commander": "^
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"ora": "^7.0.1",
|
|
51
|
-
"yaml": "^2.3.4"
|
|
52
|
-
},
|
|
53
|
-
"devDependencies": {
|
|
54
|
-
"jest": "^29.7.0"
|
|
39
|
+
"commander": "^12.0.0",
|
|
40
|
+
"ora": "^8.0.1",
|
|
41
|
+
"@shipi18n/core": "^2.0.0"
|
|
55
42
|
},
|
|
56
|
-
"
|
|
57
|
-
"
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"@anthropic-ai/sdk": ">=0.30.0",
|
|
45
|
+
"openai": ">=4.0.0"
|
|
58
46
|
},
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"@anthropic-ai/sdk": {
|
|
49
|
+
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"openai": {
|
|
52
|
+
"optional": true
|
|
53
|
+
}
|
|
62
54
|
},
|
|
63
|
-
"
|
|
64
|
-
"
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"jest": "^29.7.0"
|
|
65
57
|
},
|
|
66
|
-
"
|
|
67
|
-
|
|
58
|
+
"scripts": {
|
|
59
|
+
"test": "NODE_OPTIONS='--experimental-vm-modules' jest --passWithNoTests"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -1,331 +1,88 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync
|
|
2
|
-
import { join
|
|
3
|
-
import chalk from 'chalk'
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import { flattenObject, unflattenObject, deepMerge, findMissingKeys } from '../utils/incremental.js';
|
|
1
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import chalk from 'chalk'
|
|
4
|
+
import ora from 'ora'
|
|
5
|
+
import { translateJSON } from '@shipi18n/core'
|
|
6
|
+
|
|
7
|
+
const PROVIDER_ENV = { anthropic: 'ANTHROPIC_API_KEY', openai: 'OPENAI_API_KEY' }
|
|
9
8
|
|
|
10
9
|
export function translateCommand(program) {
|
|
11
10
|
program
|
|
12
11
|
.command('translate <input>')
|
|
13
|
-
.description('Translate a JSON locale file to
|
|
12
|
+
.description('Translate a JSON locale file to other languages using your own LLM')
|
|
14
13
|
.option('-t, --target <languages>', 'Target languages (comma-separated)', 'es,fr')
|
|
15
14
|
.option('-s, --source <language>', 'Source language', 'en')
|
|
16
15
|
.option('-o, --output <dir>', 'Output directory', './locales')
|
|
17
|
-
.option('
|
|
18
|
-
.option('--
|
|
19
|
-
.option('--
|
|
20
|
-
.option('--
|
|
21
|
-
.option('--no-regional-fallback', 'Disable regional fallback (e.g., pt-BR -> pt)')
|
|
22
|
-
.option('-i, --incremental', 'Only translate new/missing keys (skip existing translations)')
|
|
23
|
-
.option('--skip-keys <keys>', 'Keys to skip from translation (comma-separated exact paths)')
|
|
24
|
-
.option('--skip-paths <patterns>', 'Paths to skip using wildcards (comma-separated, e.g., "states.*,config.*.secret")')
|
|
25
|
-
.option('--context-file <path>', 'JSON file with context annotations for disambiguation (e.g., {"close": "button - dismiss"})')
|
|
26
|
-
.option('--zip [filename]', 'Output translations as ZIP file (default: translations.zip)')
|
|
16
|
+
.option('-p, --provider <name>', 'LLM provider: anthropic | openai', 'anthropic')
|
|
17
|
+
.option('--api-key <key>', 'LLM API key (else read from provider env var)')
|
|
18
|
+
.option('--model <model>', 'Override the provider default model')
|
|
19
|
+
.option('-i, --incremental', 'Only translate new/missing keys (reuse existing output files)')
|
|
27
20
|
.action(async (input, options) => {
|
|
28
|
-
const
|
|
29
|
-
|
|
21
|
+
const provider = options.provider
|
|
22
|
+
if (!PROVIDER_ENV[provider]) {
|
|
23
|
+
console.error(chalk.red(`Unknown provider '${provider}'. Use 'anthropic' or 'openai'.`))
|
|
24
|
+
process.exit(1)
|
|
25
|
+
}
|
|
26
|
+
const apiKey = options.apiKey || process.env[PROVIDER_ENV[provider]]
|
|
27
|
+
if (!apiKey) {
|
|
28
|
+
console.error(chalk.red(`No API key. Set ${chalk.yellow(PROVIDER_ENV[provider])} or pass --api-key.`))
|
|
29
|
+
console.error(chalk.gray(`This tool uses YOUR own ${provider} key ā no account or Shipi18n key needed.`))
|
|
30
|
+
process.exit(1)
|
|
31
|
+
}
|
|
32
|
+
if (!existsSync(input)) {
|
|
33
|
+
console.error(chalk.red(`Input file not found: ${input}`))
|
|
34
|
+
process.exit(1)
|
|
35
|
+
}
|
|
36
|
+
let content
|
|
30
37
|
try {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
spinner.fail();
|
|
37
|
-
logger.error('API key not found');
|
|
38
|
-
logger.info('Set your API key:');
|
|
39
|
-
logger.log(` ${chalk.yellow('shipi18n config set apiKey YOUR_KEY')}`);
|
|
40
|
-
logger.log(` ${chalk.gray('Get your free key at https://shipi18n.com')}`);
|
|
41
|
-
process.exit(1);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Read input file
|
|
45
|
-
if (!existsSync(input)) {
|
|
46
|
-
spinner.fail();
|
|
47
|
-
logger.error(`Input file not found: ${input}`);
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
38
|
+
content = JSON.parse(readFileSync(input, 'utf8'))
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.error(chalk.red(`Invalid JSON in ${input}: ${e.message}`))
|
|
41
|
+
process.exit(1)
|
|
42
|
+
}
|
|
50
43
|
|
|
51
|
-
|
|
52
|
-
|
|
44
|
+
const targets = options.target.split(',').map((s) => s.trim()).filter(Boolean)
|
|
45
|
+
mkdirSync(options.output, { recursive: true })
|
|
46
|
+
console.log(chalk.cyan(`\nš Translating ${input} (${options.source} ā ${targets.join(', ')}) via ${provider}\n`))
|
|
47
|
+
|
|
48
|
+
let hadWarnings = false
|
|
49
|
+
for (const to of targets) {
|
|
50
|
+
const spinner = ora(`Translating to ${to}...`).start()
|
|
51
|
+
const outPath = join(options.output, `${to}.json`)
|
|
52
|
+
const existing =
|
|
53
|
+
options.incremental && existsSync(outPath)
|
|
54
|
+
? JSON.parse(readFileSync(outPath, 'utf8'))
|
|
55
|
+
: undefined
|
|
53
56
|
try {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
: [];
|
|
74
|
-
|
|
75
|
-
if (skipKeys.length > 0 || skipPaths.length > 0) {
|
|
76
|
-
logger.info(`Skipping ${skipKeys.length + skipPaths.length} key/pattern(s) from translation`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Parse context annotations file
|
|
80
|
-
let contextAnnotations = {};
|
|
81
|
-
if (options.contextFile) {
|
|
82
|
-
if (!existsSync(options.contextFile)) {
|
|
83
|
-
spinner.fail();
|
|
84
|
-
logger.error(`Context file not found: ${options.contextFile}`);
|
|
85
|
-
process.exit(1);
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
const contextContent = readFileSync(options.contextFile, 'utf8');
|
|
89
|
-
contextAnnotations = JSON.parse(contextContent);
|
|
90
|
-
const contextCount = Object.keys(contextAnnotations).length;
|
|
91
|
-
logger.info(`Loaded ${contextCount} context annotation(s) from ${options.contextFile}`);
|
|
92
|
-
} catch (error) {
|
|
93
|
-
spinner.fail();
|
|
94
|
-
logger.error(`Invalid JSON in context file: ${error.message}`);
|
|
95
|
-
process.exit(1);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Incremental mode: load existing translations and find missing keys
|
|
100
|
-
let jsonToTranslate = json;
|
|
101
|
-
const existingTranslations = {};
|
|
102
|
-
let incrementalStats = { total: 0, existing: 0, toTranslate: 0 };
|
|
103
|
-
|
|
104
|
-
if (options.incremental) {
|
|
105
|
-
spinner.text = 'Checking existing translations...';
|
|
106
|
-
|
|
107
|
-
const sourceKeyCount = Object.keys(flattenObject(json)).length;
|
|
108
|
-
incrementalStats.total = sourceKeyCount;
|
|
109
|
-
|
|
110
|
-
// Load existing translations for each target language
|
|
111
|
-
for (const lang of targetLanguages) {
|
|
112
|
-
const targetFile = join(outputDir, lang, `${inputFileName}.json`);
|
|
113
|
-
const altTargetFile = join(outputDir, `${lang}.json`);
|
|
114
|
-
|
|
115
|
-
let existingFile = null;
|
|
116
|
-
if (existsSync(targetFile)) {
|
|
117
|
-
existingFile = targetFile;
|
|
118
|
-
} else if (existsSync(altTargetFile)) {
|
|
119
|
-
existingFile = altTargetFile;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (existingFile) {
|
|
123
|
-
try {
|
|
124
|
-
const existingContent = readFileSync(existingFile, 'utf8');
|
|
125
|
-
existingTranslations[lang] = JSON.parse(existingContent);
|
|
126
|
-
} catch (e) {
|
|
127
|
-
logger.warn(`Could not parse ${existingFile}, will re-translate`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// Find keys that need translation (missing from ANY target language)
|
|
133
|
-
const allMissingKeys = {};
|
|
134
|
-
for (const lang of targetLanguages) {
|
|
135
|
-
const existing = existingTranslations[lang] || {};
|
|
136
|
-
const missing = findMissingKeys(json, existing);
|
|
137
|
-
const missingFlat = flattenObject(missing);
|
|
138
|
-
|
|
139
|
-
for (const [key, value] of Object.entries(missingFlat)) {
|
|
140
|
-
if (!(key in allMissingKeys)) {
|
|
141
|
-
allMissingKeys[key] = value;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
57
|
+
const { result, stats } = await translateJSON({
|
|
58
|
+
content,
|
|
59
|
+
from: options.source,
|
|
60
|
+
to,
|
|
61
|
+
provider,
|
|
62
|
+
apiKey,
|
|
63
|
+
model: options.model,
|
|
64
|
+
existing,
|
|
65
|
+
})
|
|
66
|
+
writeFileSync(outPath, JSON.stringify(result, null, 2) + '\n', 'utf8')
|
|
67
|
+
const warn = stats.placeholderWarnings.length
|
|
68
|
+
if (warn) hadWarnings = true
|
|
69
|
+
spinner.succeed(
|
|
70
|
+
`${to} ā ${chalk.green(outPath)} ` +
|
|
71
|
+
chalk.gray(`(${stats.translated} translated, ${stats.reused} reused` +
|
|
72
|
+
(warn ? chalk.yellow(`, ${warn} placeholder warnings`) : '') + ')')
|
|
73
|
+
)
|
|
74
|
+
for (const w of stats.placeholderWarnings) {
|
|
75
|
+
console.log(chalk.yellow(` ā ${w.path}: placeholder drift ā missing ${JSON.stringify(w.missing)}`))
|
|
144
76
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
incrementalStats.toTranslate = missingKeyCount;
|
|
149
|
-
|
|
150
|
-
if (missingKeyCount === 0) {
|
|
151
|
-
spinner.succeed(chalk.green('All translations up to date!'));
|
|
152
|
-
logger.log('');
|
|
153
|
-
logger.log(chalk.gray(` ${sourceKeyCount} key${sourceKeyCount !== 1 ? 's' : ''} already translated`));
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
jsonToTranslate = unflattenObject(allMissingKeys);
|
|
158
|
-
spinner.text = `Translating ${missingKeyCount} new key${missingKeyCount !== 1 ? 's' : ''} to ${targetLanguages.length} language${targetLanguages.length > 1 ? 's' : ''}...`;
|
|
159
|
-
logger.log('');
|
|
160
|
-
logger.info(`Incremental mode: ${chalk.cyan(missingKeyCount)} new key${missingKeyCount !== 1 ? 's' : ''} to translate (${incrementalStats.existing} already exist)`);
|
|
161
|
-
} else {
|
|
162
|
-
spinner.text = `Translating to ${targetLanguages.length} language${targetLanguages.length > 1 ? 's' : ''}...`;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Translate with fallback support
|
|
166
|
-
const api = new Shipi18nAPI(apiKey);
|
|
167
|
-
const translations = await api.translateJSON({
|
|
168
|
-
json: jsonToTranslate,
|
|
169
|
-
sourceLanguage,
|
|
170
|
-
targetLanguages,
|
|
171
|
-
preservePlaceholders: options.preservePlaceholders,
|
|
172
|
-
htmlHandling: options.htmlHandling,
|
|
173
|
-
fallback: {
|
|
174
|
-
fallbackToSource: options.fallback !== false,
|
|
175
|
-
regionalFallback: options.regionalFallback !== false,
|
|
176
|
-
},
|
|
177
|
-
skipKeys,
|
|
178
|
-
skipPaths,
|
|
179
|
-
contextAnnotations,
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
const keyCount = Object.keys(flattenObject(jsonToTranslate)).length;
|
|
183
|
-
spinner.succeed(chalk.green(`Translated ${keyCount} key${keyCount !== 1 ? 's' : ''} to ${targetLanguages.length} language${targetLanguages.length > 1 ? 's' : ''}!`));
|
|
184
|
-
|
|
185
|
-
// Save translated files
|
|
186
|
-
if (!existsSync(outputDir)) {
|
|
187
|
-
mkdirSync(outputDir, { recursive: true });
|
|
77
|
+
} catch (e) {
|
|
78
|
+
spinner.fail(`${to}: ${e.message}`)
|
|
79
|
+
process.exitCode = 1
|
|
188
80
|
}
|
|
189
|
-
|
|
190
|
-
let savedCount = 0;
|
|
191
|
-
|
|
192
|
-
// Prepare translations for output (filter metadata, apply merging)
|
|
193
|
-
const outputTranslations = {};
|
|
194
|
-
for (const [langCode, content] of Object.entries(translations)) {
|
|
195
|
-
if (langCode === 'warnings' || langCode === 'fallbackInfo' || langCode === 'namespaceInfo' || langCode === 'skipped' || langCode === 'contextEnhanced') continue;
|
|
196
|
-
|
|
197
|
-
let finalContent = content;
|
|
198
|
-
if (options.incremental && existingTranslations[langCode]) {
|
|
199
|
-
finalContent = deepMerge(existingTranslations[langCode], content);
|
|
200
|
-
}
|
|
201
|
-
outputTranslations[langCode] = finalContent;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (options.zip) {
|
|
205
|
-
// ZIP output mode
|
|
206
|
-
const zipFileName = typeof options.zip === 'string' ? options.zip : 'translations.zip';
|
|
207
|
-
const zipPath = join(outputDir, zipFileName);
|
|
208
|
-
|
|
209
|
-
await new Promise((resolve, reject) => {
|
|
210
|
-
const output = createWriteStream(zipPath);
|
|
211
|
-
const archive = archiver('zip', { zlib: { level: 9 } });
|
|
212
|
-
|
|
213
|
-
output.on('close', resolve);
|
|
214
|
-
archive.on('error', reject);
|
|
215
|
-
|
|
216
|
-
archive.pipe(output);
|
|
217
|
-
|
|
218
|
-
for (const [langCode, content] of Object.entries(outputTranslations)) {
|
|
219
|
-
archive.append(JSON.stringify(content, null, 2), { name: `${langCode}.json` });
|
|
220
|
-
savedCount++;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
archive.finalize();
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
logger.success(`Saved: ${chalk.cyan(zipPath)} (${savedCount} file${savedCount !== 1 ? 's' : ''})`);
|
|
227
|
-
} else {
|
|
228
|
-
// Individual files mode
|
|
229
|
-
for (const [langCode, content] of Object.entries(outputTranslations)) {
|
|
230
|
-
const outputFile = join(outputDir, `${langCode}.json`);
|
|
231
|
-
writeFileSync(outputFile, JSON.stringify(content, null, 2), 'utf8');
|
|
232
|
-
logger.success(`Saved: ${chalk.cyan(outputFile)}${options.incremental ? chalk.gray(' (merged)') : ''}`);
|
|
233
|
-
savedCount++;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Show fallback info if any fallbacks were used
|
|
238
|
-
if (translations.fallbackInfo && translations.fallbackInfo.used) {
|
|
239
|
-
const fallbackInfo = translations.fallbackInfo;
|
|
240
|
-
logger.log('');
|
|
241
|
-
logger.info('Fallback information:');
|
|
242
|
-
|
|
243
|
-
// Regional fallbacks
|
|
244
|
-
if (Object.keys(fallbackInfo.regionalFallbacks).length > 0) {
|
|
245
|
-
for (const [lang, baseLang] of Object.entries(fallbackInfo.regionalFallbacks)) {
|
|
246
|
-
logger.log(` ${chalk.blue('ā¢')} ${lang} ā ${baseLang} ${chalk.gray('(regional fallback)')}`);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Languages that fell back to source
|
|
251
|
-
if (fallbackInfo.languagesFallbackToSource.length > 0) {
|
|
252
|
-
for (const lang of fallbackInfo.languagesFallbackToSource) {
|
|
253
|
-
logger.log(` ${chalk.yellow('ā¢')} ${lang} ā ${sourceLanguage} ${chalk.gray('(source fallback)')}`);
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Keys that used fallback
|
|
258
|
-
if (Object.keys(fallbackInfo.keysFallback).length > 0) {
|
|
259
|
-
for (const [lang, keys] of Object.entries(fallbackInfo.keysFallback)) {
|
|
260
|
-
logger.log(` ${chalk.yellow('ā¢')} ${lang}: ${keys.length} key${keys.length > 1 ? 's' : ''} used fallback`);
|
|
261
|
-
if (keys.length <= 5) {
|
|
262
|
-
keys.forEach(key => {
|
|
263
|
-
logger.log(` ${chalk.gray('- ' + key)}`);
|
|
264
|
-
});
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Show skipped keys info if any
|
|
271
|
-
if (translations.skipped && translations.skipped.count > 0) {
|
|
272
|
-
logger.log('');
|
|
273
|
-
logger.info(`Skipped ${translations.skipped.count} key${translations.skipped.count > 1 ? 's' : ''} from translation:`);
|
|
274
|
-
const keysToShow = translations.skipped.keys.slice(0, 10);
|
|
275
|
-
keysToShow.forEach(key => {
|
|
276
|
-
logger.log(` ${chalk.gray('ā¢')} ${key}`);
|
|
277
|
-
});
|
|
278
|
-
if (translations.skipped.keys.length > 10) {
|
|
279
|
-
logger.log(` ${chalk.gray(`... and ${translations.skipped.keys.length - 10} more`)}`);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Show context-enhanced keys info if any
|
|
284
|
-
if (translations.contextEnhanced && translations.contextEnhanced.count > 0) {
|
|
285
|
-
logger.log('');
|
|
286
|
-
logger.info(`${chalk.cyan('šÆ')} ${translations.contextEnhanced.count} key${translations.contextEnhanced.count > 1 ? 's' : ''} translated with context annotations:`);
|
|
287
|
-
const keysToShow = translations.contextEnhanced.keys.slice(0, 10);
|
|
288
|
-
keysToShow.forEach(key => {
|
|
289
|
-
logger.log(` ${chalk.cyan('ā¢')} ${key}`);
|
|
290
|
-
});
|
|
291
|
-
if (translations.contextEnhanced.keys.length > 10) {
|
|
292
|
-
logger.log(` ${chalk.gray(`... and ${translations.contextEnhanced.keys.length - 10} more`)}`);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Show legal content warning with key details
|
|
297
|
-
const legalWarning = translations.warnings?.find(w => w.type === 'legal_content');
|
|
298
|
-
if (legalWarning?.details?.keys?.length > 0) {
|
|
299
|
-
logger.log('');
|
|
300
|
-
logger.warn(`${chalk.yellow('ā ļø')} Legal content detected - review these keys:`);
|
|
301
|
-
legalWarning.details.keys.forEach(key => {
|
|
302
|
-
logger.log(` ${chalk.yellow('ā¢')} ${key}`);
|
|
303
|
-
});
|
|
304
|
-
if (legalWarning.details.count > 10) {
|
|
305
|
-
logger.log(` ${chalk.gray(`... and ${legalWarning.details.count - 10} more`)}`);
|
|
306
|
-
}
|
|
307
|
-
logger.log(` ${chalk.gray('Machine-translated legal text may not be legally binding.')}`);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
// Show other warnings if any (exclude legal_content since we showed it above)
|
|
311
|
-
const otherWarnings = translations.warnings?.filter(w => w.type !== 'legal_content') || [];
|
|
312
|
-
if (otherWarnings.length > 0) {
|
|
313
|
-
logger.log('');
|
|
314
|
-
logger.warn('Warnings:');
|
|
315
|
-
otherWarnings.forEach(warning => {
|
|
316
|
-
logger.log(` ${chalk.yellow('ā¢')} ${warning.message}`);
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
logger.log('');
|
|
321
|
-
logger.log(chalk.green(`⨠Successfully translated ${savedCount} file${savedCount > 1 ? 's' : ''}!`));
|
|
322
|
-
logger.log(chalk.gray(` Output: ${outputDir}`));
|
|
323
|
-
|
|
324
|
-
} catch (error) {
|
|
325
|
-
spinner.fail();
|
|
326
|
-
logger.log('');
|
|
327
|
-
logger.log(formatError(error));
|
|
328
|
-
process.exit(1);
|
|
329
81
|
}
|
|
330
|
-
|
|
82
|
+
console.log(
|
|
83
|
+
hadWarnings
|
|
84
|
+
? chalk.yellow('\nDone, with placeholder warnings ā review the flagged keys.')
|
|
85
|
+
: chalk.green('\nā Done.')
|
|
86
|
+
)
|
|
87
|
+
})
|
|
331
88
|
}
|
package/src/commands/config.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import { getConfig, setConfigValue, saveConfig } from '../lib/config.js';
|
|
3
|
-
import { logger } from '../utils/logger.js';
|
|
4
|
-
|
|
5
|
-
export function configCommand(program) {
|
|
6
|
-
const config = program.command('config')
|
|
7
|
-
.description('Manage CLI configuration');
|
|
8
|
-
|
|
9
|
-
// Get config
|
|
10
|
-
config
|
|
11
|
-
.command('get [key]')
|
|
12
|
-
.description('Get configuration value(s)')
|
|
13
|
-
.action((key) => {
|
|
14
|
-
const currentConfig = getConfig();
|
|
15
|
-
|
|
16
|
-
if (key) {
|
|
17
|
-
const value = currentConfig[key];
|
|
18
|
-
if (value !== undefined && value !== null) {
|
|
19
|
-
logger.log(`${chalk.cyan(key)}: ${value}`);
|
|
20
|
-
} else {
|
|
21
|
-
logger.warn(`Config key "${key}" not found`);
|
|
22
|
-
}
|
|
23
|
-
} else {
|
|
24
|
-
logger.log(chalk.cyan('Current configuration:'));
|
|
25
|
-
logger.log('');
|
|
26
|
-
Object.entries(currentConfig).forEach(([k, v]) => {
|
|
27
|
-
if (v !== undefined && v !== null) {
|
|
28
|
-
// Mask API key for security
|
|
29
|
-
if (k === 'apiKey' && v) {
|
|
30
|
-
logger.log(` ${chalk.yellow(k)}: ${v.substring(0, 12)}...`);
|
|
31
|
-
} else {
|
|
32
|
-
logger.log(` ${chalk.yellow(k)}: ${v}`);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
logger.log('');
|
|
37
|
-
logger.log(chalk.gray('Config file: ~/.shipi18n/config.yml'));
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
// Set config
|
|
42
|
-
config
|
|
43
|
-
.command('set <key> <value>')
|
|
44
|
-
.description('Set configuration value')
|
|
45
|
-
.action((key, value) => {
|
|
46
|
-
try {
|
|
47
|
-
// Parse value if it's a boolean or array
|
|
48
|
-
let parsedValue = value;
|
|
49
|
-
if (value === 'true') parsedValue = true;
|
|
50
|
-
if (value === 'false') parsedValue = false;
|
|
51
|
-
if (value.includes(',')) parsedValue = value.split(',').map(v => v.trim());
|
|
52
|
-
|
|
53
|
-
setConfigValue(key, parsedValue);
|
|
54
|
-
logger.success(`Set ${chalk.cyan(key)} = ${parsedValue}`);
|
|
55
|
-
|
|
56
|
-
// Show next steps for API key
|
|
57
|
-
if (key === 'apiKey') {
|
|
58
|
-
logger.log('');
|
|
59
|
-
logger.info('API key saved! Try translating a file:');
|
|
60
|
-
logger.log(` ${chalk.yellow('shipi18n translate en.json --target es,fr')}`);
|
|
61
|
-
}
|
|
62
|
-
} catch (error) {
|
|
63
|
-
logger.error(`Failed to set config: ${error.message}`);
|
|
64
|
-
process.exit(1);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Init config
|
|
69
|
-
config
|
|
70
|
-
.command('init')
|
|
71
|
-
.description('Initialize configuration file with defaults')
|
|
72
|
-
.action(() => {
|
|
73
|
-
try {
|
|
74
|
-
const defaultConfig = {
|
|
75
|
-
apiKey: '',
|
|
76
|
-
sourceLanguage: 'en',
|
|
77
|
-
targetLanguages: ['es', 'fr', 'de'],
|
|
78
|
-
outputDir: './locales',
|
|
79
|
-
saveKeys: true,
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
saveConfig(defaultConfig);
|
|
83
|
-
logger.success('Created config file: ~/.shipi18n/config.yml');
|
|
84
|
-
logger.log('');
|
|
85
|
-
logger.info('Next steps:');
|
|
86
|
-
logger.log(` 1. Get your API key at ${chalk.cyan('https://shipi18n.com')}`);
|
|
87
|
-
logger.log(` 2. Set your API key: ${chalk.yellow('shipi18n config set apiKey YOUR_KEY')}`);
|
|
88
|
-
logger.log(` 3. Translate: ${chalk.yellow('shipi18n translate en.json --target es,fr')}`);
|
|
89
|
-
} catch (error) {
|
|
90
|
-
logger.error(`Failed to initialize config: ${error.message}`);
|
|
91
|
-
process.exit(1);
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|