@awsless/i18n 0.0.12 → 0.0.14

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/index.d.ts CHANGED
@@ -23,8 +23,8 @@ type I18nPluginProps = {
23
23
  declare const i18n: (props: I18nPluginProps) => Plugin;
24
24
 
25
25
  type AiTranslationProps = {
26
- /** The maximum number of tokens allowed in the AI's response. */
27
- maxTokens: number;
26
+ /** The maximum number of output tokens allowed in the AI's response. */
27
+ maxOutputTokens: number;
28
28
  /** The language model to use for translations (e.g., gpt-4, gpt-3.5-turbo). */
29
29
  model: LanguageModel;
30
30
  /** Number of text entries to translate in a single batch.
package/dist/index.js CHANGED
@@ -221,7 +221,7 @@ var ai = (props) => {
221
221
  batches.map(async (texts2) => {
222
222
  const result = await generateObject({
223
223
  model: props.model,
224
- maxTokens: props.maxTokens,
224
+ maxOutputTokens: props.maxOutputTokens,
225
225
  schema: z.object({
226
226
  translations: z.object({
227
227
  source: z.string(),
@@ -1,4 +1,4 @@
1
- // src/framework/svelte.svelte.ts
1
+ // src/framework/svelte-5.svelte.ts
2
2
  var locale = $state("en");
3
3
  var t = $derived.by(() => {
4
4
  const api = (template, ...args) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/i18n",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -9,7 +9,8 @@
9
9
  "internationalization",
10
10
  "i18n",
11
11
  "svelte",
12
- "sveltekit"
12
+ "sveltekit",
13
+ "ai"
13
14
  ],
14
15
  "repository": {
15
16
  "type": "git",
@@ -21,19 +22,16 @@
21
22
  "files": [
22
23
  "dist"
23
24
  ],
24
- "main": "./dist/index.cjs",
25
25
  "module": "./dist/index.js",
26
26
  "types": "./dist/index.d.ts",
27
27
  "exports": {
28
28
  ".": {
29
- "require": "./dist/index.cjs",
30
29
  "import": "./dist/index.js",
31
30
  "types": "./dist/index.d.ts"
32
31
  },
33
32
  "./svelte": {
34
- "require": "./dist/svelte.svelte.cjs",
35
- "import": "./dist/svelte.svelte.js",
36
- "types": "./dist/svelte.svelte.d.ts"
33
+ "svelte": "./dist/svelte-5.svelte.js",
34
+ "types": "./dist/svelte-5.svelte.d.ts"
37
35
  }
38
36
  },
39
37
  "vitest": {
@@ -42,33 +40,33 @@
42
40
  ]
43
41
  },
44
42
  "devDependencies": {
45
- "@ai-sdk/openai": "^1.3.23",
43
+ "@ai-sdk/openai": "^2.0.42",
46
44
  "@sveltejs/vite-plugin-svelte": "^3.1.2",
47
45
  "svelte": "^4.2.19",
48
46
  "svelte-preprocess": "^6.0.2",
49
- "vite": "^5.4.2"
47
+ "vite": "^7.1.9"
50
48
  },
51
49
  "peerDependencies": {
52
50
  "svelte": "^4.2.19",
53
- "vite": "^5.4.2"
51
+ "vite": "^7.1.9"
54
52
  },
55
53
  "dependencies": {
56
54
  "@swc/core": "^1.3.70",
57
55
  "@types/chunk": "^0.0.0",
58
56
  "@types/line-column": "^1.0.2",
59
- "ai": "^4.3.19",
57
+ "ai": "^5.0.60",
60
58
  "chunk": "^0.0.3",
61
59
  "estree-walker": "^3.0.3",
62
60
  "glob": "^10.3.9",
63
61
  "line-column": "^1.0.2",
64
62
  "magic-string": "^0.30.18",
65
63
  "swc-walk": "1.0.0-rc.2",
66
- "zod": "^3.21.4"
64
+ "zod": "^4.1.11"
67
65
  },
68
66
  "scripts": {
69
67
  "test": "pnpm code test",
70
- "build": "pnpm tsup src/index.ts --format cjs,esm --dts --clean",
71
- "build-svelte": "pnpm tsup src/framework/svelte.svelte.ts --format cjs,esm --dts",
68
+ "build": "pnpm tsup src/index.ts --format esm --dts --clean",
69
+ "build-svelte": "pnpm tsup src/framework/svelte-5.svelte.ts --format esm --dts",
72
70
  "prepublish": "if pnpm test; then pnpm build; pnpm build-svelte; else exit; fi"
73
71
  }
74
72
  }
package/dist/index.cjs DELETED
@@ -1,288 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var index_exports = {};
32
- __export(index_exports, {
33
- ai: () => ai,
34
- i18n: () => i18n
35
- });
36
- module.exports = __toCommonJS(index_exports);
37
-
38
- // src/vite.ts
39
- var import_magic_string = __toESM(require("magic-string"), 1);
40
-
41
- // src/cache.ts
42
- var import_promises = require("fs/promises");
43
- var import_path = require("path");
44
- var loadCache = async (cwd) => {
45
- const file = (0, import_path.join)(cwd, "i18n.json");
46
- try {
47
- await (0, import_promises.stat)(file);
48
- } catch (error) {
49
- return new Cache();
50
- }
51
- const data = await (0, import_promises.readFile)(file, "utf8");
52
- return new Cache(JSON.parse(data));
53
- };
54
- var saveCache = async (cwd, cache) => {
55
- await (0, import_promises.writeFile)((0, import_path.join)(cwd, "i18n.json"), JSON.stringify(cache, void 0, 2));
56
- };
57
- var Cache = class {
58
- constructor(data = {}) {
59
- this.data = data;
60
- }
61
- set(source, locale, translation) {
62
- if (!this.data[source]) {
63
- this.data[source] = {};
64
- }
65
- if (typeof this.data[source][locale] === "undefined") {
66
- this.data[source][locale] = translation;
67
- }
68
- }
69
- get(source, locale) {
70
- return this.data[source]?.[locale];
71
- }
72
- has(source, locale) {
73
- return typeof this.get(source, locale) === "string";
74
- }
75
- delete(source, locale) {
76
- if (typeof this.data[source]?.[locale] !== "undefined") {
77
- delete this.data[source][locale];
78
- }
79
- if (this.data[source] && Object.keys(this.data[source]).length === 0) {
80
- delete this.data[source];
81
- }
82
- }
83
- *entries() {
84
- for (const [source, locales] of Object.entries(this.data)) {
85
- for (const [locale, translation] of Object.entries(locales)) {
86
- yield { source, locale, translation };
87
- }
88
- }
89
- }
90
- toJSON() {
91
- return this.data;
92
- }
93
- };
94
-
95
- // src/diff.ts
96
- var findNewTranslations = (cache, sources, locales) => {
97
- const list = [];
98
- for (const source of sources) {
99
- for (const locale of locales) {
100
- if (!cache.has(source, locale)) {
101
- list.push({ source, locale });
102
- }
103
- }
104
- }
105
- return list;
106
- };
107
- var removeUnusedTranslations = (cache, sources, locales) => {
108
- for (const item of cache.entries()) {
109
- if (!locales.includes(item.locale) || !sources.includes(item.source)) {
110
- cache.delete(item.source, item.locale);
111
- }
112
- }
113
- };
114
-
115
- // src/find.ts
116
- var import_promises2 = require("fs/promises");
117
- var import_glob = require("glob");
118
- var import_path2 = require("path");
119
-
120
- // src/find/svelte.ts
121
- var import_estree_walker = require("estree-walker");
122
- var import_line_column = __toESM(require("line-column"), 1);
123
- var import_compiler = require("svelte/compiler");
124
- var findSvelteTranslatable = (code) => {
125
- const found = [];
126
- const origin = (0, import_line_column.default)(code);
127
- const ast = (0, import_compiler.parse)(code, {
128
- css: false
129
- });
130
- const enter = (node) => {
131
- if (node.type === "TaggedTemplateExpression" && node.tag.type === "MemberExpression" && node.tag.object.type === "Identifier" && node.tag.object.name === "lang" && node.tag.property.type === "Identifier" && node.tag.property.name === "t" && node.quasi.type === "TemplateLiteral" && node.quasi.loc) {
132
- const start = node.quasi.loc.start;
133
- const end = node.quasi.loc.end;
134
- const content = code.substring(
135
- origin.toIndex(start.line, start.column) + 2,
136
- origin.toIndex(end.line, end.column)
137
- );
138
- found.push(content);
139
- }
140
- };
141
- (0, import_estree_walker.walk)(ast.html, { enter });
142
- if (ast.instance) {
143
- (0, import_estree_walker.walk)(ast.instance.content, { enter });
144
- }
145
- if (ast.module) {
146
- (0, import_estree_walker.walk)(ast.module.content, { enter });
147
- }
148
- return found;
149
- };
150
-
151
- // src/find/typescript.ts
152
- var import_core = require("@swc/core");
153
- var import_swc_walk = require("swc-walk");
154
- var findTypescriptTranslatable = async (code) => {
155
- const found = [];
156
- const ast = await (0, import_core.parse)(code, {
157
- syntax: "typescript"
158
- });
159
- (0, import_swc_walk.simple)(ast, {
160
- TaggedTemplateExpression(node) {
161
- if (node.tag.type === "MemberExpression" && node.tag.object.type === "Identifier" && node.tag.object.value === "lang" && node.tag.property.type === "Identifier" && node.tag.property.value === "t") {
162
- const content = code.substring(
163
- node.template.span.start - ast.span.start + 1,
164
- node.template.span.end - ast.span.start - 1
165
- );
166
- found.push(content);
167
- }
168
- }
169
- });
170
- return found;
171
- };
172
-
173
- // src/find.ts
174
- var findTranslatable = async (cwd) => {
175
- const files = await (0, import_glob.glob)("**/*.{js,ts,svelte}", {
176
- cwd,
177
- ignore: [
178
- //
179
- "**/node_modules/**",
180
- "**/.svelte-kit/**",
181
- "**/.*/**"
182
- ]
183
- });
184
- const found = [];
185
- for (const file of files) {
186
- const code = await (0, import_promises2.readFile)((0, import_path2.join)(cwd, file), "utf8");
187
- if (code.includes("lang.t`")) {
188
- if (file.endsWith(".svelte")) {
189
- found.push(...findSvelteTranslatable(code));
190
- } else {
191
- const entries = await findTypescriptTranslatable(code);
192
- found.push(...entries);
193
- }
194
- }
195
- }
196
- return found;
197
- };
198
-
199
- // src/vite.ts
200
- var i18n = (props) => {
201
- let cache;
202
- return {
203
- name: "awsless/i18n",
204
- enforce: "pre",
205
- async buildStart() {
206
- const cwd = process.cwd();
207
- this.info("Finding all translatable text...");
208
- const sourceTexts = await findTranslatable(cwd);
209
- cache = await loadCache(cwd);
210
- removeUnusedTranslations(cache, sourceTexts, props.locales);
211
- const newSourceTexts = findNewTranslations(cache, sourceTexts, props.locales);
212
- if (newSourceTexts.length > 0) {
213
- this.info(`Translating ${newSourceTexts.length} new texts.`);
214
- const translations = await props.translate(props.default ?? "en", newSourceTexts);
215
- this.info(`Translated ${translations.length} texts.`);
216
- for (const item of translations) {
217
- cache.set(item.source, item.locale, item.translation);
218
- }
219
- }
220
- await saveCache(cwd, cache);
221
- this.info(`Translating done.`);
222
- },
223
- transform(code) {
224
- if (code.includes("lang.t`")) {
225
- const transformedCode = new import_magic_string.default(code);
226
- for (const item of cache.entries()) {
227
- transformedCode.replaceAll(
228
- `lang.t\`${item.source}\``,
229
- `lang.t.get(\`${item.source}\`, {${props.locales.map((locale) => {
230
- const translation = cache.get(item.source, locale);
231
- if (translation === item.source) {
232
- return;
233
- }
234
- return `"${locale}":\`${translation}\``;
235
- }).filter((v) => !!v).join(",")}})`
236
- );
237
- }
238
- return {
239
- code: transformedCode.toString(),
240
- map: transformedCode.generateMap({
241
- hires: true
242
- })
243
- };
244
- }
245
- return;
246
- }
247
- };
248
- };
249
-
250
- // src/translate/ai.ts
251
- var import_ai = require("ai");
252
- var import_chunk = __toESM(require("chunk"), 1);
253
- var import_zod = require("zod");
254
- var ai = (props) => {
255
- return async (originalLocale, texts) => {
256
- const batches = (0, import_chunk.default)(texts, props.batchSize ?? 1e3);
257
- const translations = await Promise.all(
258
- batches.map(async (texts2) => {
259
- const result = await (0, import_ai.generateObject)({
260
- model: props.model,
261
- maxTokens: props.maxTokens,
262
- schema: import_zod.z.object({
263
- translations: import_zod.z.object({
264
- source: import_zod.z.string(),
265
- locale: import_zod.z.string(),
266
- translation: import_zod.z.string()
267
- }).array()
268
- }),
269
- prompt: [
270
- `You have to translate the text inside the JSON file below from "${originalLocale}" to the provided locale.`,
271
- ...props?.rules ?? [],
272
- "",
273
- `JSON FILE:`,
274
- JSON.stringify(texts2)
275
- ].join("\n"),
276
- system: "You are a helpful translator."
277
- });
278
- return result.object.translations;
279
- })
280
- );
281
- return translations.flat(3);
282
- };
283
- };
284
- // Annotate the CommonJS export names for ESM import in node:
285
- 0 && (module.exports = {
286
- ai,
287
- i18n
288
- });
package/dist/index.d.cts DELETED
@@ -1,39 +0,0 @@
1
- import { Plugin } from 'vite';
2
- import { LanguageModel } from 'ai';
3
-
4
- type Translator = (defaultLocale: string, list: {
5
- source: string;
6
- locale: string;
7
- }[]) => TranslationResponse[] | Promise<TranslationResponse[]>;
8
- type TranslationResponse = {
9
- source: string;
10
- locale: string;
11
- translation: string;
12
- };
13
- type I18nPluginProps = {
14
- /** The original language your source text is written in.
15
- * @default "en"
16
- */
17
- default?: string;
18
- /** The list of target locales to translate your text into. */
19
- locales: string[];
20
- /** Function that performs the translation of a given text. */
21
- translate: Translator;
22
- };
23
- declare const i18n: (props: I18nPluginProps) => Plugin;
24
-
25
- type AiTranslationProps = {
26
- /** The maximum number of tokens allowed in the AI's response. */
27
- maxTokens: number;
28
- /** The language model to use for translations (e.g., gpt-4, gpt-3.5-turbo). */
29
- model: LanguageModel;
30
- /** Number of text entries to translate in a single batch.
31
- * @default 1000
32
- */
33
- batchSize?: number;
34
- /** Custom translation guidelines for the AI. These are injected into the prompt. */
35
- rules?: string[];
36
- };
37
- declare const ai: (props: AiTranslationProps) => Translator;
38
-
39
- export { type AiTranslationProps, type I18nPluginProps, type Translator, ai, i18n };
@@ -1,67 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/framework/svelte.svelte.ts
21
- var svelte_svelte_exports = {};
22
- __export(svelte_svelte_exports, {
23
- lang: () => lang
24
- });
25
- module.exports = __toCommonJS(svelte_svelte_exports);
26
- var locale = $state("en");
27
- var t = $derived.by(() => {
28
- const api = (template, ...args) => {
29
- return String.raw({ raw: template.raw }, ...args);
30
- };
31
- api.get = (og, translations) => {
32
- return translations[locale] ?? og;
33
- };
34
- return api;
35
- });
36
- var lang = {
37
- /** Get the current locale.
38
- *
39
- * @example
40
- * console.log(lang.locale)
41
- */
42
- get locale() {
43
- return locale;
44
- },
45
- /** To change the locale that is being rendered simply change this property.
46
- *
47
- * @example
48
- * lang.locale = 'jp'
49
- */
50
- set locale(v) {
51
- locale = v;
52
- },
53
- /** Translate helper for translating template strings.
54
- * The i18n Vite plugin will find all instances where you want text
55
- * to be translated and automatically translate your text during build time.
56
- *
57
- * @example
58
- * lang.t`Hello world!`
59
- */
60
- get t() {
61
- return t;
62
- }
63
- };
64
- // Annotate the CommonJS export names for ESM import in node:
65
- 0 && (module.exports = {
66
- lang
67
- });
@@ -1,20 +0,0 @@
1
- declare const lang: {
2
- /** Get the current locale.
3
- *
4
- * @example
5
- * console.log(lang.locale)
6
- */
7
- locale: string;
8
- /** Translate helper for translating template strings.
9
- * The i18n Vite plugin will find all instances where you want text
10
- * to be translated and automatically translate your text during build time.
11
- *
12
- * @example
13
- * lang.t`Hello world!`
14
- */
15
- readonly t: (template: TemplateStringsArray, ...args: Array<string | number | {
16
- toString(): string;
17
- }>) => string;
18
- };
19
-
20
- export { lang };