vite-plugin-ai-auto-i18n 0.2.0 → 0.2.1

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.
@@ -0,0 +1,2 @@
1
+
2
+ export { }
@@ -0,0 +1,123 @@
1
+ import { Plugin } from 'vite';
2
+
3
+ type MessageKind = "text" | "attribute" | "mixed";
4
+ interface ExtractedMessage {
5
+ source: string;
6
+ prefix?: string;
7
+ suffix?: string;
8
+ kind: MessageKind;
9
+ /** For "mixed" messages: placeholder names in the order of the element's expression children. */
10
+ placeholders?: string[];
11
+ file: string;
12
+ start: number;
13
+ end: number;
14
+ tag: string;
15
+ component: string;
16
+ attribute?: string;
17
+ context: string;
18
+ fingerprint: string;
19
+ }
20
+ /** A message with the stable ID that the registry assigned to it. */
21
+ type ResolvedMessage = ExtractedMessage & {
22
+ id: string;
23
+ };
24
+ type RegistryEntry = Pick<ExtractedMessage, "source" | "kind" | "tag" | "component" | "attribute" | "context" | "fingerprint"> & {
25
+ id: string;
26
+ location: string;
27
+ status: "active" | "orphaned";
28
+ firstSeen: string;
29
+ lastSeen: string;
30
+ };
31
+ interface Registry {
32
+ version: 1;
33
+ entries: Record<string, RegistryEntry>;
34
+ }
35
+ type Catalog = Record<string, string>;
36
+ interface TranslationRequest {
37
+ id: string;
38
+ source: string;
39
+ context: string;
40
+ kind: MessageKind;
41
+ placeholders?: string[];
42
+ }
43
+ interface TranslationProvider {
44
+ translate(input: {
45
+ sourceLocale: string;
46
+ targetLocale: string;
47
+ messages: TranslationRequest[];
48
+ }): Promise<Catalog>;
49
+ }
50
+
51
+ /**
52
+ * Some batches of a translation request failed while others succeeded. `partial` holds the translations
53
+ * that did come back, so the caller can keep them and report the rest.
54
+ */
55
+ declare class PartialTranslationError extends Error {
56
+ readonly partial: Catalog;
57
+ readonly failed: number;
58
+ constructor(message: string, partial: Catalog, failed: number);
59
+ }
60
+
61
+ interface OpenAICompatibleOptions {
62
+ apiKey: string;
63
+ model: string;
64
+ baseURL?: string;
65
+ /** Messages sent per request. Smaller batches keep responses within output limits. Default 40. */
66
+ batchSize?: number;
67
+ /** Abort a single request after this many milliseconds. Default 60000. */
68
+ timeoutMs?: number;
69
+ /** Extra attempts for network errors, timeouts, 408/409/429/5xx and malformed responses. Default 2. */
70
+ maxRetries?: number;
71
+ /** Base delay before a retry; doubles on each attempt. Default 500. */
72
+ retryDelayMs?: number;
73
+ /** Batches sent in parallel. Default 4. */
74
+ concurrency?: number;
75
+ }
76
+ declare class OpenAICompatibleProvider implements TranslationProvider {
77
+ private options;
78
+ constructor(options: OpenAICompatibleOptions);
79
+ /**
80
+ * Translates in parallel batches. If every batch fails, the first error is thrown. If only some fail,
81
+ * a PartialTranslationError carries the translations that succeeded.
82
+ */
83
+ translate(input: {
84
+ sourceLocale: string;
85
+ targetLocale: string;
86
+ messages: TranslationRequest[];
87
+ }): Promise<Catalog>;
88
+ private translateBatch;
89
+ private request;
90
+ }
91
+
92
+ interface AutoI18nOptions {
93
+ locales: string[];
94
+ sourceLocale?: string;
95
+ /**
96
+ * "build" (default) compiles one locale per build. "runtime" ships every catalogue and
97
+ * switches locale in the browser via `setLocale()` from the `virtual:autoi18n` module.
98
+ */
99
+ delivery?: "build" | "runtime";
100
+ /** Compile this build for one locale. Defaults to sourceLocale; set AUTOI18N_LOCALE for CI matrix builds. */
101
+ locale?: string;
102
+ include?: string[];
103
+ exclude?: string[];
104
+ directory?: string;
105
+ provider?: TranslationProvider;
106
+ failOnMissingProvider?: boolean;
107
+ onAmbiguous?: "warn" | "error";
108
+ /**
109
+ * Minutes an orphaned message (one no longer in the source) is kept in the registry before it is deleted.
110
+ * Default 43200 (30 days). `0` deletes orphans on the next build; `Infinity` keeps them forever.
111
+ */
112
+ orphanRetentionMinutes?: number;
113
+ /**
114
+ * Locales translated at the same time. Default 2. Batches within a locale are limited separately by
115
+ * `OpenAICompatibleOptions.concurrency`. Lower this if your provider rate-limits per account.
116
+ */
117
+ localeConcurrency?: number;
118
+ /** Log each message sent to the translation provider and each translation received, with timestamps. Default false. */
119
+ debug?: boolean;
120
+ }
121
+ declare function autoI18n(options: AutoI18nOptions): Plugin;
122
+
123
+ export { type AutoI18nOptions, type Catalog, type ExtractedMessage, type MessageKind, type OpenAICompatibleOptions, OpenAICompatibleProvider, PartialTranslationError, type Registry, type RegistryEntry, type ResolvedMessage, type TranslationProvider, type TranslationRequest, autoI18n as default };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Picks the best supported locale for a list of preferred locales (for example `navigator.languages`).
3
+ * This function is also embedded into the generated `virtual:autoi18n` module via `toString()`,
4
+ * so it must stay self-contained and must not reference anything outside its body.
5
+ */
6
+ declare function resolveLocale(preferred: string[], supported: string[], fallback?: string): string;
7
+
8
+ type LocaleLoader = () => Promise<{
9
+ default: Record<string, string>;
10
+ } | Record<string, string>>;
11
+ /**
12
+ * Returns a cached catalogue loader. Unknown locales load the fallback, and a failed load is not cached.
13
+ * `prime` stores a catalogue that was already fetched, such as one pushed by the dev server.
14
+ * Serialised into the runtime module via toString(), so it must not reference anything outside its body.
15
+ */
16
+ declare function createLocaleLoader(loaders: Record<string, LocaleLoader>, fallback?: string): {
17
+ (locale: string): Promise<Record<string, string>>;
18
+ prime(locale: string, catalog: Record<string, string>): void;
19
+ };
20
+
21
+ export { type LocaleLoader, createLocaleLoader, resolveLocale };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-ai-auto-i18n",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "description": "Implicit, build-time AI localization for JSX applications",
6
6
  "engines": {