@awsless/i18n 0.0.8 → 0.0.10
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/README.MD +16 -18
- package/dist/index.cjs +37 -53
- package/dist/index.d.cts +14 -12
- package/dist/index.d.ts +14 -12
- package/dist/index.js +36 -51
- package/dist/svelte.svelte.cjs +20 -3
- package/dist/svelte.svelte.d.cts +12 -0
- package/dist/svelte.svelte.d.ts +12 -0
- package/dist/svelte.svelte.js +20 -3
- package/package.json +1 -1
package/README.MD
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
# AI-generated internationalization made easy
|
|
2
2
|
|
|
3
|
-
The `@awsless/i18n` package is a Vite plugin that automatically translates your text
|
|
3
|
+
The `@awsless/i18n` package is a Vite plugin that automatically translates your text during build time using AI or any other tool you prefer. The plugin will inline the translations so you don't have to worry about loading the translations at the right time. This means, switching the locale will instantly switch all your translated text on the page.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- Automatic text translation
|
|
8
8
|
- Inlines translations
|
|
9
|
-
- Extremely lightweight (
|
|
10
|
-
-
|
|
11
|
-
|
|
12
|
-
## Todo's
|
|
13
|
-
|
|
14
|
-
- Create a CLI command to find the text that needs to be translated & translate it, so that we can remove that part from the vite plugin.
|
|
9
|
+
- Extremely lightweight (431 bytes uncompressed 🔥)
|
|
10
|
+
- Instant locale switch
|
|
11
|
+
- Svelte 5 support
|
|
15
12
|
|
|
16
13
|
## Setup
|
|
17
14
|
|
|
@@ -24,15 +21,17 @@ npm i @awsless/i18n
|
|
|
24
21
|
## Vite installation
|
|
25
22
|
|
|
26
23
|
```ts
|
|
27
|
-
import { i18n,
|
|
24
|
+
import { i18n, ai } from '@awsless/i18n'
|
|
25
|
+
import { openai } from '@ai-sdk/openai'
|
|
28
26
|
|
|
29
27
|
export defineConfig({
|
|
30
28
|
plugins: [
|
|
31
29
|
i18n({
|
|
32
30
|
default: 'en',
|
|
33
31
|
locales: ['es', 'jp'],
|
|
34
|
-
translate:
|
|
35
|
-
|
|
32
|
+
translate: ai({
|
|
33
|
+
maxTokens: 32_000,
|
|
34
|
+
model: openai('gpt-4.1'),
|
|
36
35
|
})
|
|
37
36
|
})
|
|
38
37
|
]
|
|
@@ -42,11 +41,11 @@ export defineConfig({
|
|
|
42
41
|
## Svelte example
|
|
43
42
|
|
|
44
43
|
```ts
|
|
45
|
-
import {
|
|
44
|
+
import { lang } from '@awsless/i18n/svelte'
|
|
46
45
|
|
|
47
46
|
const count = 1
|
|
48
47
|
|
|
49
|
-
|
|
48
|
+
lang.t`${count} count`
|
|
50
49
|
```
|
|
51
50
|
|
|
52
51
|
The plugin will find all instances where you want text to be translated.
|
|
@@ -54,23 +53,22 @@ The text is translated automatically during build time to produce
|
|
|
54
53
|
a bundled output something like this:
|
|
55
54
|
|
|
56
55
|
```ts
|
|
57
|
-
import {
|
|
56
|
+
import { lang } from '@awsless/i18n/svelte'
|
|
58
57
|
|
|
59
58
|
const count = 1
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
en: `${count} count`,
|
|
60
|
+
lang.t.get(`${count} count`, {
|
|
63
61
|
es: `${count} contar`,
|
|
64
62
|
jp: `${count} カウント`,
|
|
65
63
|
})
|
|
66
64
|
```
|
|
67
65
|
|
|
68
|
-
To change the locale that is being rendered simply
|
|
66
|
+
To change the locale that is being rendered simply change the `lang.locale` property.
|
|
69
67
|
|
|
70
68
|
```ts
|
|
71
|
-
import {
|
|
69
|
+
import { lang } from '@awsless/i18n/svelte'
|
|
72
70
|
|
|
73
|
-
locale
|
|
71
|
+
lang.locale = 'jp'
|
|
74
72
|
```
|
|
75
73
|
|
|
76
74
|
## Changing the AI-generated text
|
package/dist/index.cjs
CHANGED
|
@@ -31,8 +31,7 @@ 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
|
|
35
|
-
mock: () => mock
|
|
34
|
+
i18n: () => createI18nPlugin
|
|
36
35
|
});
|
|
37
36
|
module.exports = __toCommonJS(index_exports);
|
|
38
37
|
|
|
@@ -56,32 +55,32 @@ var Cache = class {
|
|
|
56
55
|
constructor(data = {}) {
|
|
57
56
|
this.data = data;
|
|
58
57
|
}
|
|
59
|
-
set(
|
|
60
|
-
if (!this.data[
|
|
61
|
-
this.data[
|
|
58
|
+
set(source, locale, translation) {
|
|
59
|
+
if (!this.data[source]) {
|
|
60
|
+
this.data[source] = {};
|
|
62
61
|
}
|
|
63
|
-
if (!this.data[
|
|
64
|
-
this.data[
|
|
62
|
+
if (!this.data[source][locale]) {
|
|
63
|
+
this.data[source][locale] = translation;
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
|
-
get(
|
|
68
|
-
return this.data[
|
|
66
|
+
get(source, locale) {
|
|
67
|
+
return this.data[source]?.[locale];
|
|
69
68
|
}
|
|
70
|
-
has(
|
|
71
|
-
return typeof this.get(
|
|
69
|
+
has(source, locale) {
|
|
70
|
+
return typeof this.get(source, locale) === "string";
|
|
72
71
|
}
|
|
73
|
-
delete(
|
|
74
|
-
if (this.data[
|
|
75
|
-
delete this.data[
|
|
72
|
+
delete(source, locale) {
|
|
73
|
+
if (this.data[source]?.[locale]) {
|
|
74
|
+
delete this.data[source][locale];
|
|
76
75
|
}
|
|
77
|
-
if (this.data[
|
|
78
|
-
delete this.data[
|
|
76
|
+
if (this.data[source] && Object.keys(this.data[source]).length === 0) {
|
|
77
|
+
delete this.data[source];
|
|
79
78
|
}
|
|
80
79
|
}
|
|
81
80
|
*entries() {
|
|
82
|
-
for (const [
|
|
81
|
+
for (const [source, locales] of Object.entries(this.data)) {
|
|
83
82
|
for (const [locale, translation] of Object.entries(locales)) {
|
|
84
|
-
yield {
|
|
83
|
+
yield { source, locale, translation };
|
|
85
84
|
}
|
|
86
85
|
}
|
|
87
86
|
}
|
|
@@ -91,21 +90,21 @@ var Cache = class {
|
|
|
91
90
|
};
|
|
92
91
|
|
|
93
92
|
// src/diff.ts
|
|
94
|
-
var findNewTranslations = (cache,
|
|
93
|
+
var findNewTranslations = (cache, sources, locales) => {
|
|
95
94
|
const list = [];
|
|
96
|
-
for (const
|
|
95
|
+
for (const source of sources) {
|
|
97
96
|
for (const locale of locales) {
|
|
98
|
-
if (!cache.has(
|
|
99
|
-
list.push({
|
|
97
|
+
if (!cache.has(source, locale)) {
|
|
98
|
+
list.push({ source, locale });
|
|
100
99
|
}
|
|
101
100
|
}
|
|
102
101
|
}
|
|
103
102
|
return list;
|
|
104
103
|
};
|
|
105
|
-
var removeUnusedTranslations = (cache,
|
|
104
|
+
var removeUnusedTranslations = (cache, sources, locales) => {
|
|
106
105
|
for (const item of cache.entries()) {
|
|
107
|
-
if (!locales.includes(item.locale) || !
|
|
108
|
-
cache.delete(item.
|
|
106
|
+
if (!locales.includes(item.locale) || !sources.includes(item.source)) {
|
|
107
|
+
cache.delete(item.source, item.locale);
|
|
109
108
|
}
|
|
110
109
|
}
|
|
111
110
|
};
|
|
@@ -126,10 +125,7 @@ var findSvelteTranslatable = (code) => {
|
|
|
126
125
|
css: false
|
|
127
126
|
});
|
|
128
127
|
const enter = (node) => {
|
|
129
|
-
if (
|
|
130
|
-
//
|
|
131
|
-
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
|
-
) {
|
|
128
|
+
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) {
|
|
133
129
|
const start = node.quasi.loc.start;
|
|
134
130
|
const end = node.quasi.loc.end;
|
|
135
131
|
const content = code.substring(
|
|
@@ -205,16 +201,16 @@ var createI18nPlugin = (props) => {
|
|
|
205
201
|
async buildStart() {
|
|
206
202
|
const cwd = process.cwd();
|
|
207
203
|
this.info("Finding all translatable text...");
|
|
208
|
-
const
|
|
204
|
+
const sourceTexts = await findTranslatable(cwd);
|
|
209
205
|
cache = await loadCache(cwd);
|
|
210
|
-
removeUnusedTranslations(cache,
|
|
211
|
-
const
|
|
212
|
-
if (
|
|
213
|
-
this.info(`Translating ${
|
|
214
|
-
const translations = await props.translate(props.default ?? "en",
|
|
206
|
+
removeUnusedTranslations(cache, sourceTexts, props.locales);
|
|
207
|
+
const newSourceTexts = findNewTranslations(cache, sourceTexts, props.locales);
|
|
208
|
+
if (newSourceTexts.length > 0) {
|
|
209
|
+
this.info(`Translating ${newSourceTexts.length} new texts.`);
|
|
210
|
+
const translations = await props.translate(props.default ?? "en", newSourceTexts);
|
|
215
211
|
this.info(`Translated ${translations.length} texts.`);
|
|
216
212
|
for (const item of translations) {
|
|
217
|
-
cache.set(item.
|
|
213
|
+
cache.set(item.source, item.locale, item.translation);
|
|
218
214
|
}
|
|
219
215
|
}
|
|
220
216
|
await saveCache(cwd, cache);
|
|
@@ -224,10 +220,10 @@ var createI18nPlugin = (props) => {
|
|
|
224
220
|
let replaced = false;
|
|
225
221
|
if (code.includes("lang.t`")) {
|
|
226
222
|
for (const item of cache.entries()) {
|
|
227
|
-
code = code.replaceAll(`lang.t\`${item.
|
|
223
|
+
code = code.replaceAll(`lang.t\`${item.source}\``, () => {
|
|
228
224
|
replaced = true;
|
|
229
|
-
return `lang.t.get(\`${item.
|
|
230
|
-
return `"${locale}":\`${cache.get(item.
|
|
225
|
+
return `lang.t.get(\`${item.source}\`, {${props.locales.map((locale) => {
|
|
226
|
+
return `"${locale}":\`${cache.get(item.source, locale)}\``;
|
|
231
227
|
}).join(",")}})`;
|
|
232
228
|
});
|
|
233
229
|
}
|
|
@@ -256,7 +252,7 @@ var ai = (props) => {
|
|
|
256
252
|
maxTokens: props.maxTokens,
|
|
257
253
|
schema: import_zod.z.object({
|
|
258
254
|
translations: import_zod.z.object({
|
|
259
|
-
|
|
255
|
+
source: import_zod.z.string(),
|
|
260
256
|
locale: import_zod.z.string(),
|
|
261
257
|
translation: import_zod.z.string()
|
|
262
258
|
}).array()
|
|
@@ -276,20 +272,8 @@ var ai = (props) => {
|
|
|
276
272
|
return translations.flat(3);
|
|
277
273
|
};
|
|
278
274
|
};
|
|
279
|
-
|
|
280
|
-
// src/translate/mock.ts
|
|
281
|
-
var mock = (translation = "REPLACED") => {
|
|
282
|
-
return (_, originals) => {
|
|
283
|
-
const response = [];
|
|
284
|
-
for (const original of originals) {
|
|
285
|
-
response.push({ ...original, translation });
|
|
286
|
-
}
|
|
287
|
-
return response;
|
|
288
|
-
};
|
|
289
|
-
};
|
|
290
275
|
// Annotate the CommonJS export names for ESM import in node:
|
|
291
276
|
0 && (module.exports = {
|
|
292
277
|
ai,
|
|
293
|
-
i18n
|
|
294
|
-
mock
|
|
278
|
+
i18n
|
|
295
279
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -2,36 +2,38 @@ import { Plugin } from 'vite';
|
|
|
2
2
|
import { LanguageModel } from 'ai';
|
|
3
3
|
|
|
4
4
|
type Translator = (defaultLocale: string, list: {
|
|
5
|
-
|
|
5
|
+
source: string;
|
|
6
6
|
locale: string;
|
|
7
7
|
}[]) => TranslationResponse[] | Promise<TranslationResponse[]>;
|
|
8
8
|
type TranslationResponse = {
|
|
9
|
-
|
|
9
|
+
source: string;
|
|
10
10
|
locale: string;
|
|
11
11
|
translation: string;
|
|
12
12
|
};
|
|
13
13
|
type I18nPluginProps = {
|
|
14
|
-
/** The
|
|
14
|
+
/** The original language your source text is written in.
|
|
15
|
+
* @default "en"
|
|
16
|
+
*/
|
|
15
17
|
default?: string;
|
|
16
|
-
/**
|
|
18
|
+
/** The list of target locales to translate your text into. */
|
|
17
19
|
locales: string[];
|
|
18
|
-
/**
|
|
20
|
+
/** Function that performs the translation of a given text. */
|
|
19
21
|
translate: Translator;
|
|
20
22
|
};
|
|
21
23
|
declare const createI18nPlugin: (props: I18nPluginProps) => Plugin;
|
|
22
24
|
|
|
23
25
|
type AiTranslationProps = {
|
|
24
|
-
/** The maximum number of tokens
|
|
26
|
+
/** The maximum number of tokens allowed in the AI's response. */
|
|
25
27
|
maxTokens: number;
|
|
26
|
-
/**
|
|
28
|
+
/** The language model to use for translations (e.g., gpt-4, gpt-3.5-turbo). */
|
|
27
29
|
model: LanguageModel;
|
|
28
|
-
/**
|
|
30
|
+
/** Number of text entries to translate in a single batch.
|
|
31
|
+
* @default 1000
|
|
32
|
+
*/
|
|
29
33
|
batchSize?: number;
|
|
30
|
-
/**
|
|
34
|
+
/** Custom translation guidelines for the AI. These are injected into the prompt. */
|
|
31
35
|
rules?: string[];
|
|
32
36
|
};
|
|
33
37
|
declare const ai: (props: AiTranslationProps) => Translator;
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export { ai, createI18nPlugin as i18n, mock };
|
|
39
|
+
export { type I18nPluginProps, type Translator, ai, createI18nPlugin as i18n };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,36 +2,38 @@ import { Plugin } from 'vite';
|
|
|
2
2
|
import { LanguageModel } from 'ai';
|
|
3
3
|
|
|
4
4
|
type Translator = (defaultLocale: string, list: {
|
|
5
|
-
|
|
5
|
+
source: string;
|
|
6
6
|
locale: string;
|
|
7
7
|
}[]) => TranslationResponse[] | Promise<TranslationResponse[]>;
|
|
8
8
|
type TranslationResponse = {
|
|
9
|
-
|
|
9
|
+
source: string;
|
|
10
10
|
locale: string;
|
|
11
11
|
translation: string;
|
|
12
12
|
};
|
|
13
13
|
type I18nPluginProps = {
|
|
14
|
-
/** The
|
|
14
|
+
/** The original language your source text is written in.
|
|
15
|
+
* @default "en"
|
|
16
|
+
*/
|
|
15
17
|
default?: string;
|
|
16
|
-
/**
|
|
18
|
+
/** The list of target locales to translate your text into. */
|
|
17
19
|
locales: string[];
|
|
18
|
-
/**
|
|
20
|
+
/** Function that performs the translation of a given text. */
|
|
19
21
|
translate: Translator;
|
|
20
22
|
};
|
|
21
23
|
declare const createI18nPlugin: (props: I18nPluginProps) => Plugin;
|
|
22
24
|
|
|
23
25
|
type AiTranslationProps = {
|
|
24
|
-
/** The maximum number of tokens
|
|
26
|
+
/** The maximum number of tokens allowed in the AI's response. */
|
|
25
27
|
maxTokens: number;
|
|
26
|
-
/**
|
|
28
|
+
/** The language model to use for translations (e.g., gpt-4, gpt-3.5-turbo). */
|
|
27
29
|
model: LanguageModel;
|
|
28
|
-
/**
|
|
30
|
+
/** Number of text entries to translate in a single batch.
|
|
31
|
+
* @default 1000
|
|
32
|
+
*/
|
|
29
33
|
batchSize?: number;
|
|
30
|
-
/**
|
|
34
|
+
/** Custom translation guidelines for the AI. These are injected into the prompt. */
|
|
31
35
|
rules?: string[];
|
|
32
36
|
};
|
|
33
37
|
declare const ai: (props: AiTranslationProps) => Translator;
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export { ai, createI18nPlugin as i18n, mock };
|
|
39
|
+
export { type I18nPluginProps, type Translator, ai, createI18nPlugin as i18n };
|
package/dist/index.js
CHANGED
|
@@ -18,32 +18,32 @@ var Cache = class {
|
|
|
18
18
|
constructor(data = {}) {
|
|
19
19
|
this.data = data;
|
|
20
20
|
}
|
|
21
|
-
set(
|
|
22
|
-
if (!this.data[
|
|
23
|
-
this.data[
|
|
21
|
+
set(source, locale, translation) {
|
|
22
|
+
if (!this.data[source]) {
|
|
23
|
+
this.data[source] = {};
|
|
24
24
|
}
|
|
25
|
-
if (!this.data[
|
|
26
|
-
this.data[
|
|
25
|
+
if (!this.data[source][locale]) {
|
|
26
|
+
this.data[source][locale] = translation;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
get(
|
|
30
|
-
return this.data[
|
|
29
|
+
get(source, locale) {
|
|
30
|
+
return this.data[source]?.[locale];
|
|
31
31
|
}
|
|
32
|
-
has(
|
|
33
|
-
return typeof this.get(
|
|
32
|
+
has(source, locale) {
|
|
33
|
+
return typeof this.get(source, locale) === "string";
|
|
34
34
|
}
|
|
35
|
-
delete(
|
|
36
|
-
if (this.data[
|
|
37
|
-
delete this.data[
|
|
35
|
+
delete(source, locale) {
|
|
36
|
+
if (this.data[source]?.[locale]) {
|
|
37
|
+
delete this.data[source][locale];
|
|
38
38
|
}
|
|
39
|
-
if (this.data[
|
|
40
|
-
delete this.data[
|
|
39
|
+
if (this.data[source] && Object.keys(this.data[source]).length === 0) {
|
|
40
|
+
delete this.data[source];
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
*entries() {
|
|
44
|
-
for (const [
|
|
44
|
+
for (const [source, locales] of Object.entries(this.data)) {
|
|
45
45
|
for (const [locale, translation] of Object.entries(locales)) {
|
|
46
|
-
yield {
|
|
46
|
+
yield { source, locale, translation };
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -53,21 +53,21 @@ var Cache = class {
|
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
// src/diff.ts
|
|
56
|
-
var findNewTranslations = (cache,
|
|
56
|
+
var findNewTranslations = (cache, sources, locales) => {
|
|
57
57
|
const list = [];
|
|
58
|
-
for (const
|
|
58
|
+
for (const source of sources) {
|
|
59
59
|
for (const locale of locales) {
|
|
60
|
-
if (!cache.has(
|
|
61
|
-
list.push({
|
|
60
|
+
if (!cache.has(source, locale)) {
|
|
61
|
+
list.push({ source, locale });
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
return list;
|
|
66
66
|
};
|
|
67
|
-
var removeUnusedTranslations = (cache,
|
|
67
|
+
var removeUnusedTranslations = (cache, sources, locales) => {
|
|
68
68
|
for (const item of cache.entries()) {
|
|
69
|
-
if (!locales.includes(item.locale) || !
|
|
70
|
-
cache.delete(item.
|
|
69
|
+
if (!locales.includes(item.locale) || !sources.includes(item.source)) {
|
|
70
|
+
cache.delete(item.source, item.locale);
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
};
|
|
@@ -88,10 +88,7 @@ var findSvelteTranslatable = (code) => {
|
|
|
88
88
|
css: false
|
|
89
89
|
});
|
|
90
90
|
const enter = (node) => {
|
|
91
|
-
if (
|
|
92
|
-
//
|
|
93
|
-
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
|
|
94
|
-
) {
|
|
91
|
+
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) {
|
|
95
92
|
const start = node.quasi.loc.start;
|
|
96
93
|
const end = node.quasi.loc.end;
|
|
97
94
|
const content = code.substring(
|
|
@@ -167,16 +164,16 @@ var createI18nPlugin = (props) => {
|
|
|
167
164
|
async buildStart() {
|
|
168
165
|
const cwd = process.cwd();
|
|
169
166
|
this.info("Finding all translatable text...");
|
|
170
|
-
const
|
|
167
|
+
const sourceTexts = await findTranslatable(cwd);
|
|
171
168
|
cache = await loadCache(cwd);
|
|
172
|
-
removeUnusedTranslations(cache,
|
|
173
|
-
const
|
|
174
|
-
if (
|
|
175
|
-
this.info(`Translating ${
|
|
176
|
-
const translations = await props.translate(props.default ?? "en",
|
|
169
|
+
removeUnusedTranslations(cache, sourceTexts, props.locales);
|
|
170
|
+
const newSourceTexts = findNewTranslations(cache, sourceTexts, props.locales);
|
|
171
|
+
if (newSourceTexts.length > 0) {
|
|
172
|
+
this.info(`Translating ${newSourceTexts.length} new texts.`);
|
|
173
|
+
const translations = await props.translate(props.default ?? "en", newSourceTexts);
|
|
177
174
|
this.info(`Translated ${translations.length} texts.`);
|
|
178
175
|
for (const item of translations) {
|
|
179
|
-
cache.set(item.
|
|
176
|
+
cache.set(item.source, item.locale, item.translation);
|
|
180
177
|
}
|
|
181
178
|
}
|
|
182
179
|
await saveCache(cwd, cache);
|
|
@@ -186,10 +183,10 @@ var createI18nPlugin = (props) => {
|
|
|
186
183
|
let replaced = false;
|
|
187
184
|
if (code.includes("lang.t`")) {
|
|
188
185
|
for (const item of cache.entries()) {
|
|
189
|
-
code = code.replaceAll(`lang.t\`${item.
|
|
186
|
+
code = code.replaceAll(`lang.t\`${item.source}\``, () => {
|
|
190
187
|
replaced = true;
|
|
191
|
-
return `lang.t.get(\`${item.
|
|
192
|
-
return `"${locale}":\`${cache.get(item.
|
|
188
|
+
return `lang.t.get(\`${item.source}\`, {${props.locales.map((locale) => {
|
|
189
|
+
return `"${locale}":\`${cache.get(item.source, locale)}\``;
|
|
193
190
|
}).join(",")}})`;
|
|
194
191
|
});
|
|
195
192
|
}
|
|
@@ -218,7 +215,7 @@ var ai = (props) => {
|
|
|
218
215
|
maxTokens: props.maxTokens,
|
|
219
216
|
schema: z.object({
|
|
220
217
|
translations: z.object({
|
|
221
|
-
|
|
218
|
+
source: z.string(),
|
|
222
219
|
locale: z.string(),
|
|
223
220
|
translation: z.string()
|
|
224
221
|
}).array()
|
|
@@ -238,19 +235,7 @@ var ai = (props) => {
|
|
|
238
235
|
return translations.flat(3);
|
|
239
236
|
};
|
|
240
237
|
};
|
|
241
|
-
|
|
242
|
-
// src/translate/mock.ts
|
|
243
|
-
var mock = (translation = "REPLACED") => {
|
|
244
|
-
return (_, originals) => {
|
|
245
|
-
const response = [];
|
|
246
|
-
for (const original of originals) {
|
|
247
|
-
response.push({ ...original, translation });
|
|
248
|
-
}
|
|
249
|
-
return response;
|
|
250
|
-
};
|
|
251
|
-
};
|
|
252
238
|
export {
|
|
253
239
|
ai,
|
|
254
|
-
createI18nPlugin as i18n
|
|
255
|
-
mock
|
|
240
|
+
createI18nPlugin as i18n
|
|
256
241
|
};
|
package/dist/svelte.svelte.cjs
CHANGED
|
@@ -34,12 +34,29 @@ var t = $derived.by(() => {
|
|
|
34
34
|
return api;
|
|
35
35
|
});
|
|
36
36
|
var lang = {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
/** Get the current locale.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* console.log(lang.locale)
|
|
41
|
+
*/
|
|
40
42
|
get locale() {
|
|
41
43
|
return locale;
|
|
42
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
|
+
*/
|
|
43
60
|
get t() {
|
|
44
61
|
return t;
|
|
45
62
|
}
|
package/dist/svelte.svelte.d.cts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
declare const lang: {
|
|
2
|
+
/** Get the current locale.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* console.log(lang.locale)
|
|
6
|
+
*/
|
|
2
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
|
+
*/
|
|
3
15
|
readonly t: (template: TemplateStringsArray, ...args: Array<string | number | {
|
|
4
16
|
toString(): string;
|
|
5
17
|
}>) => string;
|
package/dist/svelte.svelte.d.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
declare const lang: {
|
|
2
|
+
/** Get the current locale.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* console.log(lang.locale)
|
|
6
|
+
*/
|
|
2
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
|
+
*/
|
|
3
15
|
readonly t: (template: TemplateStringsArray, ...args: Array<string | number | {
|
|
4
16
|
toString(): string;
|
|
5
17
|
}>) => string;
|
package/dist/svelte.svelte.js
CHANGED
|
@@ -10,12 +10,29 @@ var t = $derived.by(() => {
|
|
|
10
10
|
return api;
|
|
11
11
|
});
|
|
12
12
|
var lang = {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
/** Get the current locale.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* console.log(lang.locale)
|
|
17
|
+
*/
|
|
16
18
|
get locale() {
|
|
17
19
|
return locale;
|
|
18
20
|
},
|
|
21
|
+
/** To change the locale that is being rendered simply change this property.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* lang.locale = 'jp'
|
|
25
|
+
*/
|
|
26
|
+
set locale(v) {
|
|
27
|
+
locale = v;
|
|
28
|
+
},
|
|
29
|
+
/** Translate helper for translating template strings.
|
|
30
|
+
* The i18n Vite plugin will find all instances where you want text
|
|
31
|
+
* to be translated and automatically translate your text during build time.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* lang.t`Hello world!`
|
|
35
|
+
*/
|
|
19
36
|
get t() {
|
|
20
37
|
return t;
|
|
21
38
|
}
|