vite-plugin-ai-auto-i18n 0.5.5 → 0.5.6

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
@@ -1,25 +1,39 @@
1
1
  import { Plugin } from "vite";
2
+ /**
3
+ * What a message was extracted from: `text` for JSX text, `attribute` for a translatable attribute, `mixed` for JSX text
4
+ * containing expressions (which become `{name}` placeholders), and `string` for a string literal marked for translation.
5
+ */
2
6
  type MessageKind = "text" | "attribute" | "mixed" | "string";
7
+ /** A translatable string found in a source file, before it has an ID. */
3
8
  interface ExtractedMessage {
9
+ /** The source text, with any HTML entities already decoded. */
4
10
  source: string;
11
+ /** Whitespace that surrounded the raw text. It is re-emitted around the translation so layout is unchanged. */
5
12
  prefix?: string;
6
13
  suffix?: string;
7
14
  kind: MessageKind;
8
15
  /** For "mixed" messages: placeholder names in the order of the element's expression children. */
9
16
  placeholders?: string[];
17
+ /** Source file the message was found in. */
10
18
  file: string;
19
+ /** Character offsets of the message in the file. */
11
20
  start: number;
12
21
  end: number;
22
+ /** Tag or call the message belongs to, for example `button`. */
13
23
  tag: string;
24
+ /** Enclosing component name. Used with `fingerprint` to match a message to its registry entry. */
14
25
  component: string;
15
26
  attribute?: string;
27
+ /** Position within the component, such as the enclosing element. Shown to translators as `Component > context`. */
16
28
  context: string;
29
+ /** Hash of the source and its location fields (kind, tag, component, attribute, context), used to match registry entries. */
17
30
  fingerprint: string;
18
31
  }
19
32
  /** A message with the stable ID that the registry assigned to it. */
20
33
  type ResolvedMessage = ExtractedMessage & {
21
34
  id: string;
22
35
  };
36
+ /** One message remembered in the registry. Entries are kept across builds so stable IDs survive edits. */
23
37
  type RegistryEntry = Pick<ExtractedMessage, "source" | "kind" | "tag" | "component" | "attribute" | "context" | "fingerprint"> & {
24
38
  id: string;
25
39
  location: string;
@@ -27,11 +41,14 @@ type RegistryEntry = Pick<ExtractedMessage, "source" | "kind" | "tag" | "compone
27
41
  firstSeen: string;
28
42
  lastSeen: string;
29
43
  };
44
+ /** The persisted `registry.json`: all known messages keyed by ID. `version` allows a future format change. */
30
45
  interface Registry {
31
46
  version: 1;
32
47
  entries: Record<string, RegistryEntry>;
33
48
  }
49
+ /** Maps message IDs to text. */
34
50
  type Catalog = Record<string, string>;
51
+ /** A message that needs a translation, as sent to a provider. */
35
52
  interface TranslationRequest {
36
53
  id: string;
37
54
  source: string;
@@ -39,6 +56,10 @@ interface TranslationRequest {
39
56
  kind: MessageKind;
40
57
  placeholders?: string[];
41
58
  }
59
+ /**
60
+ * Translates a batch of messages into one target locale. Return a catalogue with a key for each message that was
61
+ * translated. Missing keys are left untranslated. Throw a `PartialTranslationError` to keep the keys already done.
62
+ */
42
63
  interface TranslationProvider {
43
64
  translate(input: {
44
65
  sourceLocale: string;
@@ -62,6 +83,7 @@ export declare class PartialTranslationError extends Error {
62
83
  readonly failed: number;
63
84
  constructor(message: string, partial: Catalog, failed: number);
64
85
  }
86
+ /** A provider paired with the name used to refer to it in configuration and log messages. */
65
87
  interface NamedProvider {
66
88
  name: string;
67
89
  provider: TranslationProvider;
@@ -70,6 +92,9 @@ interface NamedProvider {
70
92
  * Tries providers in order. Messages a provider did not translate (a failed request, or a partial result) are sent
71
93
  * to the next provider. Throws only when nothing could be translated, or a PartialTranslationError when some messages
72
94
  * are still missing after every provider has been tried.
95
+ *
96
+ * @param providers Providers in the order they should be tried.
97
+ * @param onFallback Called with a human-readable message each time a provider hands work to the next one.
73
98
  */
74
99
  export declare class FallbackProvider implements TranslationProvider {
75
100
  private readonly providers;
@@ -83,8 +108,14 @@ export declare class FallbackProvider implements TranslationProvider {
83
108
  }
84
109
  /**
85
110
  * Normalizes the `provider` option to one provider. Returns undefined when no provider is configured.
111
+ *
112
+ * A single provider (or a bare provider object) is returned as is. A named map is validated, ordered so that `primary`
113
+ * comes first, and wrapped in a {@link FallbackProvider} when it holds more than one entry.
114
+ *
115
+ * @throws If an entry is not a provider, or `primary` names a provider that is not configured.
86
116
  */
87
117
  export declare function resolveProvider(config: ProviderConfig | undefined, onFallback?: (message: string) => void): TranslationProvider | undefined;
118
+ /** Tuning options shared by every HTTP provider that sends messages in batches. */
88
119
  interface BatchedProviderOptions {
89
120
  /** Messages sent per request. Smaller batches keep responses within output limits. Default 40. */
90
121
  batchSize?: number;
@@ -97,6 +128,7 @@ interface BatchedProviderOptions {
97
128
  /** Batches sent in parallel. Default 4. */
98
129
  concurrency?: number;
99
130
  }
131
+ /** One translation call: the locale pair and the messages to translate between them. */
100
132
  interface TranslateInput {
101
133
  sourceLocale: string;
102
134
  targetLocale: string;
@@ -114,38 +146,61 @@ declare abstract class BatchedProvider<O extends BatchedProviderOptions> impleme
114
146
  * a PartialTranslationError carries the translations that succeeded.
115
147
  */
116
148
  translate(input: TranslateInput): Promise<Catalog>;
149
+ /**
150
+ * Runs one batch, retrying retryable failures with exponential backoff (`retryDelayMs * 2^attempt`, jittered)
151
+ * up to `maxRetries` extra attempts. Non-retryable errors are rethrown immediately.
152
+ */
117
153
  private translateBatch;
118
154
  /** Sends one batch to the provider and returns the translations it produced for the requested IDs. */
119
155
  protected abstract request(input: TranslateInput): Promise<Catalog>;
120
156
  /** Parses the model's text reply, which must be a JSON object mapping IDs to strings. */
121
157
  protected toCatalog(content: string | undefined, messages: TranslationRequest[]): Catalog;
122
158
  }
159
+ /** Options for an OpenAI or OpenAI-compatible chat completions endpoint. */
123
160
  interface OpenAICompatibleOptions extends BatchedProviderOptions {
124
161
  apiKey: string;
125
162
  model: string;
163
+ /** Overrides the API root, for example to target a proxy or a self-hosted compatible server. */
126
164
  baseURL?: string;
127
165
  }
166
+ /**
167
+ * Translates batches through the `/chat/completions` endpoint. Requests use JSON mode so the reply is a parseable
168
+ * object of translations.
169
+ */
128
170
  export declare class OpenAICompatibleProvider extends BatchedProvider<OpenAICompatibleOptions> {
129
171
  protected request(input: TranslateInput): Promise<Catalog>;
130
172
  }
173
+ /** Options for the Anthropic Messages API. */
131
174
  interface AnthropicOptions extends BatchedProviderOptions {
132
175
  apiKey: string;
133
176
  model: string;
177
+ /** Overrides the API root, for example to target a proxy. */
134
178
  baseURL?: string;
135
179
  /** Maximum tokens the model may generate per batch. Default 8192. */
136
180
  maxTokens?: number;
137
181
  }
182
+ /**
183
+ * Translates batches through the Anthropic Messages API. The reply's text blocks are joined before parsing.
184
+ */
138
185
  export declare class AnthropicProvider extends BatchedProvider<AnthropicOptions> {
186
+ /** Sends one batch and parses the model's text reply; a surrounding Markdown code fence is stripped first. */
139
187
  protected request(input: TranslateInput): Promise<Catalog>;
140
188
  }
189
+ /** Options for the Gemini `generateContent` API. */
141
190
  interface GeminiOptions extends BatchedProviderOptions {
142
191
  apiKey: string;
143
192
  model: string;
193
+ /** Overrides the API root, for example to target a proxy. */
144
194
  baseURL?: string;
145
195
  /** Maximum tokens the model may generate per batch. Default 8192. */
146
196
  maxOutputTokens?: number;
147
197
  }
198
+ /**
199
+ * Translates batches through the Gemini `generateContent` API. The system prompt goes in `systemInstruction`, and
200
+ * `responseMimeType` requests JSON output.
201
+ */
148
202
  export declare class GeminiProvider extends BatchedProvider<GeminiOptions> {
203
+ /** Sends one batch and parses the first candidate's text; a surrounding Markdown code fence is stripped first. */
149
204
  protected request(input: TranslateInput): Promise<Catalog>;
150
205
  }
151
206
  export interface AutoI18nOptions {
@@ -188,5 +243,15 @@ export interface AutoI18nOptions {
188
243
  /** Text of the block comment that marks a string literal for translation. Default `i18n`. */
189
244
  stringMarker?: string;
190
245
  }
246
+ /**
247
+ * Vite plugin that extracts translatable strings from source files, translates them and serves the catalogues.
248
+ * Options are validated and the provider is resolved immediately, so a bad configuration throws before the build starts.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * import autoI18n from "vite-plugin-ai-i18n";
253
+ * export default defineConfig({ plugins: [autoI18n({ locales: ["en", "de"] })] });
254
+ * ```
255
+ */
191
256
  export default function autoI18n(options: AutoI18nOptions): Plugin;
192
257
  export type { AnthropicOptions, Catalog, ExtractedMessage, GeminiOptions, MessageKind, OpenAICompatibleOptions, ProviderConfig, Registry, RegistryEntry, ResolvedMessage, TranslationProvider, TranslationRequest };
@@ -2,8 +2,14 @@
2
2
  * Picks the best supported locale for a list of preferred locales (for example `navigator.languages`).
3
3
  * This function is also embedded into the generated `virtual:autoi18n` module via `toString()`,
4
4
  * so it must stay self-contained and must not reference anything outside its body.
5
+ *
6
+ * @param preferred Locales in priority order, such as `navigator.languages`.
7
+ * @param supported Locales the app has catalogues for.
8
+ * @param fallback Returned when no preferred locale matches.
9
+ * @returns The supported locale that matches first, preferring an exact match over a same-language match.
5
10
  */
6
11
  export declare function resolveLocale(preferred: string[], supported: string[], fallback?: string): string;
12
+ /** Lazily imports one locale's catalogue. Resolves to a module whose default export, or the object itself, is the catalogue. */
7
13
  type LocaleLoader = () => Promise<{
8
14
  default: Record<string, string>;
9
15
  } | Record<string, string>>;
@@ -11,6 +17,10 @@ type LocaleLoader = () => Promise<{
11
17
  * Returns a cached catalogue loader. Unknown locales load the fallback, and a failed load is not cached.
12
18
  * `prime` stores a catalogue that was already fetched, such as one pushed by the dev server.
13
19
  * Serialised into the runtime module via toString(), so it must not reference anything outside its body.
20
+ *
21
+ * @param loaders Loader for each supported locale, keyed by locale code.
22
+ * @param fallback Locale loaded when the requested one has no loader.
23
+ * @returns A function that resolves a locale to its catalogue, with a `prime(locale, catalog)` method.
14
24
  */
15
25
  export declare function createLocaleLoader(loaders: Record<string, LocaleLoader>, fallback?: string): {
16
26
  (locale: string): Promise<Record<string, string>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-ai-auto-i18n",
3
- "version": "0.5.5",
3
+ "version": "0.5.6",
4
4
  "type": "module",
5
5
  "description": "Implicit, build-time AI localization for JSX applications",
6
6
  "license": "ISC",