@awsless/i18n 0.0.10 → 0.0.12

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.cjs CHANGED
@@ -31,10 +31,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  ai: () => ai,
34
- i18n: () => createI18nPlugin
34
+ i18n: () => i18n
35
35
  });
36
36
  module.exports = __toCommonJS(index_exports);
37
37
 
38
+ // src/vite.ts
39
+ var import_magic_string = __toESM(require("magic-string"), 1);
40
+
38
41
  // src/cache.ts
39
42
  var import_promises = require("fs/promises");
40
43
  var import_path = require("path");
@@ -59,7 +62,7 @@ var Cache = class {
59
62
  if (!this.data[source]) {
60
63
  this.data[source] = {};
61
64
  }
62
- if (!this.data[source][locale]) {
65
+ if (typeof this.data[source][locale] === "undefined") {
63
66
  this.data[source][locale] = translation;
64
67
  }
65
68
  }
@@ -70,7 +73,7 @@ var Cache = class {
70
73
  return typeof this.get(source, locale) === "string";
71
74
  }
72
75
  delete(source, locale) {
73
- if (this.data[source]?.[locale]) {
76
+ if (typeof this.data[source]?.[locale] !== "undefined") {
74
77
  delete this.data[source][locale];
75
78
  }
76
79
  if (this.data[source] && Object.keys(this.data[source]).length === 0) {
@@ -185,7 +188,8 @@ var findTranslatable = async (cwd) => {
185
188
  if (file.endsWith(".svelte")) {
186
189
  found.push(...findSvelteTranslatable(code));
187
190
  } else {
188
- found.push(...await findTypescriptTranslatable(code));
191
+ const entries = await findTypescriptTranslatable(code);
192
+ found.push(...entries);
189
193
  }
190
194
  }
191
195
  }
@@ -193,7 +197,7 @@ var findTranslatable = async (cwd) => {
193
197
  };
194
198
 
195
199
  // src/vite.ts
196
- var createI18nPlugin = (props) => {
200
+ var i18n = (props) => {
197
201
  let cache;
198
202
  return {
199
203
  name: "awsless/i18n",
@@ -217,23 +221,28 @@ var createI18nPlugin = (props) => {
217
221
  this.info(`Translating done.`);
218
222
  },
219
223
  transform(code) {
220
- let replaced = false;
221
224
  if (code.includes("lang.t`")) {
225
+ const transformedCode = new import_magic_string.default(code);
222
226
  for (const item of cache.entries()) {
223
- code = code.replaceAll(`lang.t\`${item.source}\``, () => {
224
- replaced = true;
225
- return `lang.t.get(\`${item.source}\`, {${props.locales.map((locale) => {
226
- return `"${locale}":\`${cache.get(item.source, locale)}\``;
227
- }).join(",")}})`;
228
- });
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
+ );
229
237
  }
238
+ return {
239
+ code: transformedCode.toString(),
240
+ map: transformedCode.generateMap({
241
+ hires: true
242
+ })
243
+ };
230
244
  }
231
- if (!replaced) {
232
- return;
233
- }
234
- return {
235
- code
236
- };
245
+ return;
237
246
  }
238
247
  };
239
248
  };
package/dist/index.d.cts CHANGED
@@ -20,7 +20,7 @@ type I18nPluginProps = {
20
20
  /** Function that performs the translation of a given text. */
21
21
  translate: Translator;
22
22
  };
23
- declare const createI18nPlugin: (props: I18nPluginProps) => Plugin;
23
+ declare const i18n: (props: I18nPluginProps) => Plugin;
24
24
 
25
25
  type AiTranslationProps = {
26
26
  /** The maximum number of tokens allowed in the AI's response. */
@@ -36,4 +36,4 @@ type AiTranslationProps = {
36
36
  };
37
37
  declare const ai: (props: AiTranslationProps) => Translator;
38
38
 
39
- export { type I18nPluginProps, type Translator, ai, createI18nPlugin as i18n };
39
+ export { type AiTranslationProps, type I18nPluginProps, type Translator, ai, i18n };
package/dist/index.d.ts CHANGED
@@ -20,7 +20,7 @@ type I18nPluginProps = {
20
20
  /** Function that performs the translation of a given text. */
21
21
  translate: Translator;
22
22
  };
23
- declare const createI18nPlugin: (props: I18nPluginProps) => Plugin;
23
+ declare const i18n: (props: I18nPluginProps) => Plugin;
24
24
 
25
25
  type AiTranslationProps = {
26
26
  /** The maximum number of tokens allowed in the AI's response. */
@@ -36,4 +36,4 @@ type AiTranslationProps = {
36
36
  };
37
37
  declare const ai: (props: AiTranslationProps) => Translator;
38
38
 
39
- export { type I18nPluginProps, type Translator, ai, createI18nPlugin as i18n };
39
+ export { type AiTranslationProps, type I18nPluginProps, type Translator, ai, i18n };
package/dist/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ // src/vite.ts
2
+ import MagicString from "magic-string";
3
+
1
4
  // src/cache.ts
2
5
  import { readFile, stat, writeFile } from "fs/promises";
3
6
  import { join } from "path";
@@ -22,7 +25,7 @@ var Cache = class {
22
25
  if (!this.data[source]) {
23
26
  this.data[source] = {};
24
27
  }
25
- if (!this.data[source][locale]) {
28
+ if (typeof this.data[source][locale] === "undefined") {
26
29
  this.data[source][locale] = translation;
27
30
  }
28
31
  }
@@ -33,7 +36,7 @@ var Cache = class {
33
36
  return typeof this.get(source, locale) === "string";
34
37
  }
35
38
  delete(source, locale) {
36
- if (this.data[source]?.[locale]) {
39
+ if (typeof this.data[source]?.[locale] !== "undefined") {
37
40
  delete this.data[source][locale];
38
41
  }
39
42
  if (this.data[source] && Object.keys(this.data[source]).length === 0) {
@@ -148,7 +151,8 @@ var findTranslatable = async (cwd) => {
148
151
  if (file.endsWith(".svelte")) {
149
152
  found.push(...findSvelteTranslatable(code));
150
153
  } else {
151
- found.push(...await findTypescriptTranslatable(code));
154
+ const entries = await findTypescriptTranslatable(code);
155
+ found.push(...entries);
152
156
  }
153
157
  }
154
158
  }
@@ -156,7 +160,7 @@ var findTranslatable = async (cwd) => {
156
160
  };
157
161
 
158
162
  // src/vite.ts
159
- var createI18nPlugin = (props) => {
163
+ var i18n = (props) => {
160
164
  let cache;
161
165
  return {
162
166
  name: "awsless/i18n",
@@ -180,23 +184,28 @@ var createI18nPlugin = (props) => {
180
184
  this.info(`Translating done.`);
181
185
  },
182
186
  transform(code) {
183
- let replaced = false;
184
187
  if (code.includes("lang.t`")) {
188
+ const transformedCode = new MagicString(code);
185
189
  for (const item of cache.entries()) {
186
- code = code.replaceAll(`lang.t\`${item.source}\``, () => {
187
- replaced = true;
188
- return `lang.t.get(\`${item.source}\`, {${props.locales.map((locale) => {
189
- return `"${locale}":\`${cache.get(item.source, locale)}\``;
190
- }).join(",")}})`;
191
- });
190
+ transformedCode.replaceAll(
191
+ `lang.t\`${item.source}\``,
192
+ `lang.t.get(\`${item.source}\`, {${props.locales.map((locale) => {
193
+ const translation = cache.get(item.source, locale);
194
+ if (translation === item.source) {
195
+ return;
196
+ }
197
+ return `"${locale}":\`${translation}\``;
198
+ }).filter((v) => !!v).join(",")}})`
199
+ );
192
200
  }
201
+ return {
202
+ code: transformedCode.toString(),
203
+ map: transformedCode.generateMap({
204
+ hires: true
205
+ })
206
+ };
193
207
  }
194
- if (!replaced) {
195
- return;
196
- }
197
- return {
198
- code
199
- };
208
+ return;
200
209
  }
201
210
  };
202
211
  };
@@ -237,5 +246,5 @@ var ai = (props) => {
237
246
  };
238
247
  export {
239
248
  ai,
240
- createI18nPlugin as i18n
249
+ i18n
241
250
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@awsless/i18n",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -61,6 +61,7 @@
61
61
  "estree-walker": "^3.0.3",
62
62
  "glob": "^10.3.9",
63
63
  "line-column": "^1.0.2",
64
+ "magic-string": "^0.30.18",
64
65
  "swc-walk": "1.0.0-rc.2",
65
66
  "zod": "^3.21.4"
66
67
  },