vite-plugin-localization-ai 1.0.2 → 1.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-localization-ai",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Vite plugin to generate localization jsons using AI",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -18,7 +18,8 @@
18
18
  }
19
19
  },
20
20
  "files": [
21
- "dist"
21
+ "dist",
22
+ "src"
22
23
  ],
23
24
  "scripts": {
24
25
  "build": "tsc",
package/src/index.ts ADDED
@@ -0,0 +1,47 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { parse as parseJsonc } from "jsonc-parser";
4
+ import { translate } from "./providers/gemini";
5
+
6
+ interface LocalizationAIOptions {
7
+ source: string;
8
+ targets: string[];
9
+ outputDir?: string;
10
+ apiKey?: string;
11
+ extraPromt?: string;
12
+ }
13
+
14
+ export default function localizationAI(options: LocalizationAIOptions) {
15
+ const {
16
+ source,
17
+ targets,
18
+ extraPromt,
19
+ outputDir = path.dirname(source),
20
+ apiKey = process.env.LOCALIZATION_AI_API_KEY,
21
+ } = options;
22
+
23
+ return {
24
+ name: "vite-plugin-localization-ai",
25
+ apply: "build",
26
+ buildStart: async () => {
27
+ console.log("[localization-ai] starting localization...");
28
+
29
+ const sourceContent = parseJsonc(fs.readFileSync(source, "utf-8"));
30
+
31
+ for (const lang of targets) {
32
+ console.log(`[localization-ai] Translating to ${lang}...`);
33
+ const translated = await translate(sourceContent, lang, apiKey);
34
+
35
+ const outputPath = path.join(outputDir, `${lang}.json`);
36
+ fs.writeFileSync(
37
+ outputPath,
38
+ JSON.stringify(translated, null, 2),
39
+ "utf-8"
40
+ );
41
+ console.log(`[localization-ai] ${lang}.json saved!`);
42
+ }
43
+
44
+ console.log("[localization-ai] done ✅");
45
+ },
46
+ };
47
+ }
@@ -0,0 +1,35 @@
1
+ import { GoogleGenAI } from "@google/genai";
2
+
3
+ export async function translate(
4
+ sourceJson: Record<string, any>,
5
+ targetLang: string,
6
+ apiKey?: string,
7
+ extraPromt?: string
8
+ ) {
9
+ const client = new GoogleGenAI({
10
+ apiKey: apiKey,
11
+ });
12
+
13
+ const text = JSON.stringify(sourceJson, null, 2);
14
+
15
+ const response = await client.models.generateContent({
16
+ model: "gemini-2.5-flash",
17
+ contents: `You are a professional application interface translator.
18
+ Translate the JSON.
19
+ JSON:
20
+ ${text}
21
+
22
+ Target language is "${targetLang}". ${extraPromt || ""}
23
+ `,
24
+ config: {
25
+ systemInstruction:
26
+ "Translate JSON values exactly, keep keys and structure. The response must be plain text — no Markdown",
27
+ temperature: 0,
28
+ seed: 42,
29
+ responseMimeType: "application/json",
30
+ },
31
+ });
32
+
33
+ const translatedText = response.text?.trim() || "{}";
34
+ return JSON.parse(translatedText || "{}");
35
+ }