@ccgp/i18n-ai 0.2.1 → 0.2.2
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/dist/{chunk-5QO2MVWV.mjs → chunk-OQMB7A4T.mjs} +28 -12
- package/dist/cli.js +18 -35
- package/dist/cli.mjs +2 -2
- package/dist/index.js +28 -12
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
|
@@ -119,19 +119,35 @@ var translate = async ({ text, lang, targetLang, apiKey, model }) => {
|
|
|
119
119
|
};
|
|
120
120
|
async function translateBatch(batch) {
|
|
121
121
|
if (batch.entries.length === 0) return /* @__PURE__ */ new Map();
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return [entry.key, translatedText];
|
|
132
|
-
})
|
|
122
|
+
const token = batch.apiKey || process.env.OPENROUTER_API_KEY;
|
|
123
|
+
if (!token) {
|
|
124
|
+
throw new Error("Missing API Key. Please provide apiKey or set OPENROUTER_API_KEY environment variable.");
|
|
125
|
+
}
|
|
126
|
+
const openrouter = createOpenRouter({
|
|
127
|
+
apiKey: token
|
|
128
|
+
});
|
|
129
|
+
const inputPayload = Object.fromEntries(
|
|
130
|
+
batch.entries.map((e) => [e.key, e.text])
|
|
133
131
|
);
|
|
134
|
-
|
|
132
|
+
const system = `
|
|
133
|
+
You are a professional translator.
|
|
134
|
+
Task: Translate the values of the JSON object from "${batch.sourceLang}" to "${batch.targetLang}".
|
|
135
|
+
|
|
136
|
+
Rules:
|
|
137
|
+
- Keep the keys exactly the same.
|
|
138
|
+
- Translate only the values.
|
|
139
|
+
- Do not translate variables inside curly braces like "{name}".
|
|
140
|
+
- Return a valid JSON object.
|
|
141
|
+
`;
|
|
142
|
+
const { output } = await generateText({
|
|
143
|
+
model: openrouter.languageModel(batch.model || "google/gemini-2.5-flash"),
|
|
144
|
+
system,
|
|
145
|
+
prompt: JSON.stringify(inputPayload, null, 2),
|
|
146
|
+
output: Output.object({
|
|
147
|
+
schema: z.record(z.string(), z.string())
|
|
148
|
+
})
|
|
149
|
+
});
|
|
150
|
+
return new Map(Object.entries(output));
|
|
135
151
|
}
|
|
136
152
|
|
|
137
153
|
// src/core/sync.ts
|
package/dist/cli.js
CHANGED
|
@@ -120,54 +120,37 @@ function updateLock(lock, locale, base, final) {
|
|
|
120
120
|
var import_ai_sdk_provider = require("@openrouter/ai-sdk-provider");
|
|
121
121
|
var import_ai = require("ai");
|
|
122
122
|
var import_zod = __toESM(require("zod"));
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
async function translateBatch(batch) {
|
|
124
|
+
if (batch.entries.length === 0) return /* @__PURE__ */ new Map();
|
|
125
|
+
const token = batch.apiKey || process.env.OPENROUTER_API_KEY;
|
|
125
126
|
if (!token) {
|
|
126
127
|
throw new Error("Missing API Key. Please provide apiKey or set OPENROUTER_API_KEY environment variable.");
|
|
127
128
|
}
|
|
128
129
|
const openrouter = (0, import_ai_sdk_provider.createOpenRouter)({
|
|
129
130
|
apiKey: token
|
|
130
131
|
});
|
|
132
|
+
const inputPayload = Object.fromEntries(
|
|
133
|
+
batch.entries.map((e) => [e.key, e.text])
|
|
134
|
+
);
|
|
131
135
|
const system = `
|
|
132
|
-
You are a professional translator.
|
|
136
|
+
You are a professional translator.
|
|
137
|
+
Task: Translate the values of the JSON object from "${batch.sourceLang}" to "${batch.targetLang}".
|
|
133
138
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
Examples:
|
|
141
|
-
- Input: "Hello {name}, welcome back!" -> Output (ES): "Hola {name}, \xA1bienvenido de nuevo!"
|
|
142
|
-
- Input: "Contact us calling to {number}" -> Output (FR): "Contactez-nous en appelant le {number}"
|
|
139
|
+
Rules:
|
|
140
|
+
- Keep the keys exactly the same.
|
|
141
|
+
- Translate only the values.
|
|
142
|
+
- Do not translate variables inside curly braces like "{name}".
|
|
143
|
+
- Return a valid JSON object.
|
|
143
144
|
`;
|
|
144
145
|
const { output } = await (0, import_ai.generateText)({
|
|
145
|
-
model: openrouter.languageModel(model || "google/gemini-2.5-flash"),
|
|
146
|
+
model: openrouter.languageModel(batch.model || "google/gemini-2.5-flash"),
|
|
146
147
|
system,
|
|
147
|
-
prompt:
|
|
148
|
+
prompt: JSON.stringify(inputPayload, null, 2),
|
|
148
149
|
output: import_ai.Output.object({
|
|
149
|
-
schema: import_zod.default.
|
|
150
|
-
translatedText: import_zod.default.string()
|
|
151
|
-
})
|
|
150
|
+
schema: import_zod.default.record(import_zod.default.string(), import_zod.default.string())
|
|
152
151
|
})
|
|
153
152
|
});
|
|
154
|
-
return output
|
|
155
|
-
};
|
|
156
|
-
async function translateBatch(batch) {
|
|
157
|
-
if (batch.entries.length === 0) return /* @__PURE__ */ new Map();
|
|
158
|
-
const results = await Promise.all(
|
|
159
|
-
batch.entries.map(async (entry) => {
|
|
160
|
-
const translatedText = await translate({
|
|
161
|
-
text: entry.text,
|
|
162
|
-
lang: batch.sourceLang,
|
|
163
|
-
targetLang: batch.targetLang,
|
|
164
|
-
apiKey: batch.apiKey,
|
|
165
|
-
model: batch.model
|
|
166
|
-
});
|
|
167
|
-
return [entry.key, translatedText];
|
|
168
|
-
})
|
|
169
|
-
);
|
|
170
|
-
return new Map(results);
|
|
153
|
+
return new Map(Object.entries(output));
|
|
171
154
|
}
|
|
172
155
|
|
|
173
156
|
// src/core/sync.ts
|
|
@@ -290,7 +273,7 @@ var TranslationService = class {
|
|
|
290
273
|
var import_prompts = __toESM(require("prompts"));
|
|
291
274
|
var import_chalk = __toESM(require("chalk"));
|
|
292
275
|
(0, import_dotenv.config)({ path: (0, import_node_path2.resolve)(process.cwd(), ".env") });
|
|
293
|
-
var VERSION = "0.2.
|
|
276
|
+
var VERSION = "0.2.2";
|
|
294
277
|
var program = new import_commander.Command();
|
|
295
278
|
program.name("i18n-ai").description("AI-powered translation CLI").version(VERSION);
|
|
296
279
|
var CONFIG_FILE = "i18n.config.json";
|
package/dist/cli.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import {
|
|
3
3
|
TranslationService
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-OQMB7A4T.mjs";
|
|
5
5
|
|
|
6
6
|
// src/cli.ts
|
|
7
7
|
import { config as loadEnv } from "dotenv";
|
|
@@ -11,7 +11,7 @@ import { readFile, writeFile, mkdir } from "fs/promises";
|
|
|
11
11
|
import prompts from "prompts";
|
|
12
12
|
import chalk from "chalk";
|
|
13
13
|
loadEnv({ path: resolve(process.cwd(), ".env") });
|
|
14
|
-
var VERSION = "0.2.
|
|
14
|
+
var VERSION = "0.2.2";
|
|
15
15
|
var program = new Command();
|
|
16
16
|
program.name("i18n-ai").description("AI-powered translation CLI").version(VERSION);
|
|
17
17
|
var CONFIG_FILE = "i18n.config.json";
|
package/dist/index.js
CHANGED
|
@@ -160,19 +160,35 @@ var translate = async ({ text, lang, targetLang, apiKey, model }) => {
|
|
|
160
160
|
};
|
|
161
161
|
async function translateBatch(batch) {
|
|
162
162
|
if (batch.entries.length === 0) return /* @__PURE__ */ new Map();
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
return [entry.key, translatedText];
|
|
173
|
-
})
|
|
163
|
+
const token = batch.apiKey || process.env.OPENROUTER_API_KEY;
|
|
164
|
+
if (!token) {
|
|
165
|
+
throw new Error("Missing API Key. Please provide apiKey or set OPENROUTER_API_KEY environment variable.");
|
|
166
|
+
}
|
|
167
|
+
const openrouter = (0, import_ai_sdk_provider.createOpenRouter)({
|
|
168
|
+
apiKey: token
|
|
169
|
+
});
|
|
170
|
+
const inputPayload = Object.fromEntries(
|
|
171
|
+
batch.entries.map((e) => [e.key, e.text])
|
|
174
172
|
);
|
|
175
|
-
|
|
173
|
+
const system = `
|
|
174
|
+
You are a professional translator.
|
|
175
|
+
Task: Translate the values of the JSON object from "${batch.sourceLang}" to "${batch.targetLang}".
|
|
176
|
+
|
|
177
|
+
Rules:
|
|
178
|
+
- Keep the keys exactly the same.
|
|
179
|
+
- Translate only the values.
|
|
180
|
+
- Do not translate variables inside curly braces like "{name}".
|
|
181
|
+
- Return a valid JSON object.
|
|
182
|
+
`;
|
|
183
|
+
const { output } = await (0, import_ai.generateText)({
|
|
184
|
+
model: openrouter.languageModel(batch.model || "google/gemini-2.5-flash"),
|
|
185
|
+
system,
|
|
186
|
+
prompt: JSON.stringify(inputPayload, null, 2),
|
|
187
|
+
output: import_ai.Output.object({
|
|
188
|
+
schema: import_zod.default.record(import_zod.default.string(), import_zod.default.string())
|
|
189
|
+
})
|
|
190
|
+
});
|
|
191
|
+
return new Map(Object.entries(output));
|
|
176
192
|
}
|
|
177
193
|
|
|
178
194
|
// src/core/sync.ts
|
package/dist/index.mjs
CHANGED