@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.
@@ -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 results = await Promise.all(
123
- batch.entries.map(async (entry) => {
124
- const translatedText = await translate({
125
- text: entry.text,
126
- lang: batch.sourceLang,
127
- targetLang: batch.targetLang,
128
- apiKey: batch.apiKey,
129
- model: batch.model
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
- return new Map(results);
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
- var translate = async ({ text, lang, targetLang, apiKey, model }) => {
124
- const token = apiKey || process.env.OPENROUTER_API_KEY;
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. Your main task is to translate text from "${lang}" to "${targetLang}".
136
+ You are a professional translator.
137
+ Task: Translate the values of the JSON object from "${batch.sourceLang}" to "${batch.targetLang}".
133
138
 
134
- IMPORTANT RULES:
135
- 1. Return ONLY the translated text. No explanations, no quotes around the output.
136
- 2. Do NOT translate text inside curly braces, e.g., "{name}", "{count}". These are variables and must remain exactly as they are.
137
- 3. Maintain the original tone and context.
138
- 4. If the text is a single word or short phrase, translate it directly.
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: `Text to translate: "${text}"`,
148
+ prompt: JSON.stringify(inputPayload, null, 2),
148
149
  output: import_ai.Output.object({
149
- schema: import_zod.default.object({
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.translatedText;
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.1";
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-5QO2MVWV.mjs";
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.1";
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 results = await Promise.all(
164
- batch.entries.map(async (entry) => {
165
- const translatedText = await translate({
166
- text: entry.text,
167
- lang: batch.sourceLang,
168
- targetLang: batch.targetLang,
169
- apiKey: batch.apiKey,
170
- model: batch.model
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
- return new Map(results);
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
@@ -5,7 +5,7 @@ import {
5
5
  translateBatch,
6
6
  updateLock,
7
7
  withLockFile
8
- } from "./chunk-5QO2MVWV.mjs";
8
+ } from "./chunk-OQMB7A4T.mjs";
9
9
  export {
10
10
  TranslationService,
11
11
  analyzeLocale,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ccgp/i18n-ai",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "AI-powered i18n translation and synchronization library",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",