vite-plugin-ai-auto-i18n 0.6.0 → 0.8.0
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 +8 -4
- package/dist/index.d.ts +49 -56
- package/dist/index.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -398,13 +398,13 @@ In dev, saving a source file re-indexes the project, translates only new or chan
|
|
|
398
398
|
|
|
399
399
|
### Locale groups
|
|
400
400
|
|
|
401
|
-
The package exports ready-made locale lists for the `locales` option. Each is a plain array of BCP 47 codes,
|
|
401
|
+
The package exports a `locales` object with ready-made locale lists for the `locales` option. Each group is a plain array of BCP 47 codes, listed below by its key on `locales`. Spread a group to keep the shared object unchanged:
|
|
402
402
|
|
|
403
403
|
```ts
|
|
404
|
-
import autoI18n, {
|
|
404
|
+
import autoI18n, { locales } from "vite-plugin-ai-auto-i18n";
|
|
405
405
|
|
|
406
|
-
autoI18n({ locales: [...EUROPE] });
|
|
407
|
-
autoI18n({ locales: ["en", ...ASIA] });
|
|
406
|
+
autoI18n({ locales: [...locales.EUROPE] });
|
|
407
|
+
autoI18n({ locales: ["en", ...locales.ASIA] });
|
|
408
408
|
```
|
|
409
409
|
|
|
410
410
|
| Group | Languages |
|
|
@@ -562,6 +562,10 @@ Return only the IDs you translated. Omitted IDs stay in the source language and
|
|
|
562
562
|
|
|
563
563
|
The `prune` CLI options (`--directory`, `--retention-minutes`, `--debug`) are described in the orphan paragraph of [Try the bundled example](#-try-the-bundled-example).
|
|
564
564
|
|
|
565
|
+
### Releases
|
|
566
|
+
|
|
567
|
+
Every push to `main` runs [.github/workflows/release.yml](.github/workflows/release.yml): verify, then bump the version from [conventional commits](https://www.conventionalcommits.org/) since the last `v*` tag (`fix`/`perf` → patch, `feat` → minor, `!` or `BREAKING CHANGE:` → major), publish to npm, and create a GitHub Release whose notes list the changes since the previous tag. Pushes without a releasable commit do nothing. The release commit is `chore(release): vX.Y.Z [skip ci]`.
|
|
568
|
+
|
|
565
569
|
## 🧪 Test coverage
|
|
566
570
|
|
|
567
571
|
Run the suite with coverage (V8 provider, `src/**`):
|
package/dist/index.d.ts
CHANGED
|
@@ -68,13 +68,29 @@ interface TranslationProvider {
|
|
|
68
68
|
messages: TranslationRequest[];
|
|
69
69
|
}): Promise<Catalog>;
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Named providers. The services listed in the README have typed keys so editors can suggest them; any other name is
|
|
73
|
+
* accepted through the index signature.
|
|
74
|
+
*/
|
|
75
|
+
interface NamedProviders {
|
|
76
|
+
primary?: string;
|
|
77
|
+
anthropic?: TranslationProvider;
|
|
78
|
+
gemini?: TranslationProvider;
|
|
79
|
+
openai?: TranslationProvider;
|
|
80
|
+
mistral?: TranslationProvider;
|
|
81
|
+
groq?: TranslationProvider;
|
|
82
|
+
deepseek?: TranslationProvider;
|
|
83
|
+
openrouter?: TranslationProvider;
|
|
84
|
+
xai?: TranslationProvider;
|
|
85
|
+
cerebras?: TranslationProvider;
|
|
86
|
+
ollama?: TranslationProvider;
|
|
87
|
+
[name: string]: TranslationProvider | string | undefined;
|
|
88
|
+
}
|
|
71
89
|
/**
|
|
72
90
|
* A single provider, or named providers. In the named form, `primary` picks the provider tried first; the rest are
|
|
73
91
|
* fallbacks in declaration order. Entries set to `undefined` are skipped, so unconfigured providers can stay in the map.
|
|
74
92
|
*/
|
|
75
|
-
type ProviderConfig = TranslationProvider |
|
|
76
|
-
primary?: string;
|
|
77
|
-
} & Record<string, TranslationProvider | string | undefined>);
|
|
93
|
+
type ProviderConfig = TranslationProvider | NamedProviders;
|
|
78
94
|
/**
|
|
79
95
|
* Some batches of a translation request failed while others succeeded. `partial` holds the translations
|
|
80
96
|
* that did come back, so the caller can keep them and report the rest.
|
|
@@ -214,58 +230,35 @@ interface GeminiOptions extends BatchedProviderOptions {
|
|
|
214
230
|
export declare class GeminiProvider extends BatchedProvider<GeminiOptions> {
|
|
215
231
|
protected request(input: TranslateInput): Promise<Catalog>;
|
|
216
232
|
}
|
|
217
|
-
/**
|
|
218
|
-
export declare const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
export declare const NORTH_AMERICA: string[];
|
|
247
|
-
/** Central America and the Caribbean: Spanish, English, French, Haitian Creole, Dutch. */
|
|
248
|
-
export declare const CENTRAL_AMERICA_CARIBBEAN: string[];
|
|
249
|
-
/** South America: Spanish, Portuguese, Quechua, Guarani, Aymara. */
|
|
250
|
-
export declare const SOUTH_AMERICA: string[];
|
|
251
|
-
/** Americas: North, Central, Caribbean and South America. */
|
|
252
|
-
export declare const AMERICAS: string[];
|
|
253
|
-
/** Oceania: English, Maori, Samoan, Tongan. */
|
|
254
|
-
export declare const OCEANIA: string[];
|
|
255
|
-
/** ASEAN: Indonesian, Malay, Burmese, Khmer, Lao, Filipino, Thai, Vietnamese, plus English as working language. */
|
|
256
|
-
export declare const ASEAN: string[];
|
|
257
|
-
/** Mercosur: Spanish, Portuguese, Guarani. */
|
|
258
|
-
export declare const MERCOSUR: string[];
|
|
259
|
-
/** Gulf Cooperation Council: Arabic and English, the working language of most member states. */
|
|
260
|
-
export declare const GCC: string[];
|
|
261
|
-
/** Commonwealth of Independent States: Russian, Belarusian, Armenian, Azerbaijani, Kazakh, Kyrgyz, Tajik, Uzbek. */
|
|
262
|
-
export declare const CIS: string[];
|
|
263
|
-
/** G7 official languages: English, French, German, Italian, Japanese. */
|
|
264
|
-
export declare const G7: string[];
|
|
265
|
-
/** Francophonie: French and major French-speaking African and Caribbean languages. */
|
|
266
|
-
export declare const FRANCOPHONE: string[];
|
|
267
|
-
/** Most widely spoken languages worldwide by total speakers. */
|
|
268
|
-
export declare const WORLDWIDE_POPULAR: string[];
|
|
233
|
+
/** Ready-made locale lists for the `locales` option, grouped by region or organization: `locales: [...locales.EUROPE]`. */
|
|
234
|
+
export declare const locales: {
|
|
235
|
+
EU: string[];
|
|
236
|
+
EUROPE: string[];
|
|
237
|
+
NORDIC: string[];
|
|
238
|
+
BALTIC: string[];
|
|
239
|
+
BALKANS: string[];
|
|
240
|
+
BENELUX: string[];
|
|
241
|
+
EAST_ASIA: string[];
|
|
242
|
+
SOUTH_ASIA: string[];
|
|
243
|
+
SOUTHEAST_ASIA: string[];
|
|
244
|
+
CENTRAL_ASIA: string[];
|
|
245
|
+
CAUCASUS: string[];
|
|
246
|
+
ASIA: string[];
|
|
247
|
+
MIDDLE_EAST: string[];
|
|
248
|
+
AFRICA: string[];
|
|
249
|
+
NORTH_AMERICA: string[];
|
|
250
|
+
CENTRAL_AMERICA_CARIBBEAN: string[];
|
|
251
|
+
SOUTH_AMERICA: string[];
|
|
252
|
+
AMERICAS: string[];
|
|
253
|
+
OCEANIA: string[];
|
|
254
|
+
ASEAN: string[];
|
|
255
|
+
MERCOSUR: string[];
|
|
256
|
+
GCC: string[];
|
|
257
|
+
CIS: string[];
|
|
258
|
+
G7: string[];
|
|
259
|
+
FRANCOPHONE: string[];
|
|
260
|
+
WORLDWIDE_POPULAR: string[];
|
|
261
|
+
};
|
|
269
262
|
export interface AutoI18nOptions {
|
|
270
263
|
locales: string[];
|
|
271
264
|
sourceLocale?: string;
|
|
@@ -317,4 +310,4 @@ export interface AutoI18nOptions {
|
|
|
317
310
|
* ```
|
|
318
311
|
*/
|
|
319
312
|
export default function autoI18n(options: AutoI18nOptions): Plugin;
|
|
320
|
-
export type { AnthropicOptions, Catalog, ExtractedMessage, GeminiOptions, MessageKind, OpenAICompatibleOptions, ProviderConfig, Registry, RegistryEntry, ResolvedMessage, TranslationProvider, TranslationRequest };
|
|
313
|
+
export type { AnthropicOptions, Catalog, ExtractedMessage, GeminiOptions, MessageKind, NamedProviders, OpenAICompatibleOptions, ProviderConfig, Registry, RegistryEntry, ResolvedMessage, TranslationProvider, TranslationRequest };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import{t as e}from"./debug-DVrC55pP.js";import{A as t,C as n,D as r,E as i,F as a,I as o,L as s,M as c,N as l,O as u,P as d,R as f,S as p,T as m,b as h,c as g,g as _,i as v,j as y,k as b,l as x,m as S,n as C,o as w,p as T,r as E,t as ee,u as te,v as D,w as O,x as ne,y as k}from"./registry-W7usElv-.js";import{n as A,t as j}from"./resolve-ELDMKdoA.js";import{ArkErrors as M}from"arktype";import{glob as re,readFile as ie,rm as N,stat as ae}from"node:fs/promises";import P from"node:path";import{fileURLToPath as oe}from"node:url";import{createHash as se}from"node:crypto";import{IntlMessageFormat as ce}from"intl-messageformat";import{parseSync as le}from"rolldown/utils";import ue from"magic-string";async function de(e,t,n){let r=Array(e.length),i=0;return await Promise.all(Array.from({length:Math.min(Math.max(1,t),e.length)},async()=>{for(;i<e.length;){let t=i++;try{r[t]={status:`fulfilled`,value:await n(e[t])}}catch(e){r[t]={status:`rejected`,reason:e}}}})),r}var fe=class{available;waiters=[];constructor(e){this.available=Math.max(1,Math.floor(e))}async run(e){await this.acquire();try{return await e()}finally{this.release()}}acquire(){return this.available>0?(this.available--,Promise.resolve()):new Promise(e=>this.waiters.push(e))}release(){let e=this.waiters.shift();e?e():this.available++}};const pe=/* @__PURE__ */ new Set([1,2,3,4,5,6]);function me(e){return e.replace(/['{}]/g,e=>e===`'`?`''`:`'${e}'`)}function he(e){return e.replace(/[^\p{L}\p{N}_]/gu,`_`)}function ge(e){let t;try{t=new ce(e,`en`).getAst()}catch{return}let n=/* @__PURE__ */ new Set,r=e=>{for(let t of e)if(pe.has(t.type)&&t.value&&n.add(t.value),t.children&&r(t.children),t.options)for(let e of Object.values(t.options))r(e.value)};return r(t),[...n]}function _e(e,t){let n=new Set(e),r=new Set(t);return n.size===r.size&&[...n].every(e=>r.has(e))}function ve(e,t){if(e.trim()===``)return!1;let n=ge(e);return n!==void 0&&_e(n,t)}var F=class extends Error{partial;failed;constructor(e,t,n){super(e),this.partial=t,this.failed=n,this.name=`PartialTranslationError`}};async function ye(n){let{provider:r,sourceLocale:i,locale:a,requests:o,previous:s,debug:c}=n,l=new Set(o.map(e=>e.id)),{messages:u,sources:d}=t(s,l).file,f=o.filter(e=>u[e.id]===void 0||d[e.id]!==e.source),p={},m=0,h;if(f.length&&r){if(c)for(let t of f)e(`sent ${i} -> ${a} ${t.id}: ${JSON.stringify(t.source)} (context: ${t.context})`);try{p=await r.translate({sourceLocale:i,targetLocale:a,messages:f})}catch(e){if(!(e instanceof F))throw e;p=e.partial,m=e.failed,h=e.message}if(c)for(let t of f){let n=p[t.id];e(`received ${i} -> ${a} ${t.id}: ${typeof n==`string`?JSON.stringify(n):`(no translation)`}`)}}let g={...u},_={...d},v=0;for(let e of f){delete g[e.id],delete _[e.id];let t=p[e.id];typeof t==`string`&&t.trim()&&(e.kind===`mixed`&&!ve(t,e.placeholders??[])?v++:(g[e.id]=t,_[e.id]=e.source))}let y=o.filter(e=>g[e.id]===void 0).length;return{file:{messages:g,sources:_},untranslated:y,rejected:v,requested:f.length,failed:m,failure:h}}async function be(e){let t=structuredClone(e.registry),{ambiguous:n,messages:r}=E(t,e.files.flatMap(e=>e.messages)),i=C(t,e.orphanRetentionMinutes),a={};for(let e of r)a[e.id]=e.source;let o=r.map(e=>({id:e.id,source:e.source,context:`${e.component} > ${e.context}`,kind:e.kind,placeholders:e.placeholders})),s=await de(e.locales,e.localeConcurrency,t=>ye({provider:e.provider,sourceLocale:e.sourceLocale,locale:t,requests:o,previous:e.previous.get(t)??u(),debug:e.debug})),c=/* @__PURE__ */ new Map,l=/* @__PURE__ */ new Map;return s.forEach((t,n)=>{let r=e.locales[n];t.status===`fulfilled`?c.set(r,t.value):l.set(r,t.reason)}),{registry:t,ambiguous:n,pruned:i,messages:r,sourceFile:{messages:a,sources:{}},locales:c,failures:l}}function I(e){return e.replace(/\s+/g,` `).trim()}function xe(e){let t=[I(e.source),e.kind,e.tag.toLowerCase(),e.attribute??``,e.context].join(``);return se(`sha256`).update(t).digest(`hex`).slice(0,24)}function L(e,t){let n=le(t,e,{lang:`tsx`,sourceType:`unambiguous`});if(n.errors.length)throw SyntaxError(`${t}: ${n.errors[0].message}`);return{program:n.program,commentsByEnd:new Map(n.comments.map(e=>[e.end,e]))}}function Se(e,t){let n=[],r=e=>{if(t(e,n)!==!1){n.push(e);for(let t of Object.keys(e)){if(t===`parent`)continue;let n=e[t];Array.isArray(n)?n.forEach(e=>R(e)&&r(e)):R(n)&&r(n)}n.pop()}};r(e)}function R(e){return typeof e==`object`&&!!e&&typeof e.type==`string`}function z(e){return R(e)&&e.type===`Literal`&&typeof e.value==`string`}function Ce(e,t,n,r){let i=n;for(;;){let n=i;for(;n>0&&/\s/.test(e[n-1]);)n--;let a=t.get(n);if(!a)return!1;if(a.type===`Block`&&a.value.trim()===r)return!0;i=a.start}}function B(e){return e.includes(`&`)?e.replace(ne,(e,t,n)=>{if(n!==void 0)return k.get(n)??e;let r=t.startsWith(`x`)?parseInt(t.slice(1),16):parseInt(t,10);return r<=1114111?String.fromCodePoint(r):e}):e}function V(e){return z(e)?B(e.value):void 0}function H(e){return B(e.value)}function we(e){return{prefix:e.match(/^\s*/)?.[0]??``,suffix:e.match(/\s*$/)?.[0]??``}}const U=e=>/[A-Za-z]/.test(e);function Te(e){return e.attributes.some(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===`translate`&&V(e.value)===`no`)}function Ee(e){return e.type===`JSXIdentifier`&&!g.test(e.name)&&!_.has(e.name)}function W(e){if(e.type===`Identifier`)return e.name;if(e.type===`MemberExpression`&&!e.computed&&e.property.type===`Identifier`){let t=W(e.object);return t?`${t}.${e.property.name}`:void 0}}const De=(e,t)=>e.type===`FunctionExpression`?!(t?.type===`MethodDefinition`||t?.type===`Property`&&(t.method||t.kind!==`init`)):e.type===`FunctionDeclaration`||e.type===`ArrowFunctionExpression`;function Oe(e,t){for(let n=e.length-1;n>=0;n--)if(t(e[n]))return n;return-1}function ke(e){let t=Oe(e,e=>e.type===`JSXElement`);return t<0?void 0:{index:t,element:e[t]}}function G(e){return e.some(e=>{let t=e.type===`JSXOpeningElement`?e:e.type===`JSXElement`?e.openingElement:void 0;return t!==void 0&&Te(t)})}function K(e,t,n){let r=e.attributes.filter(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&[`aria-label`,`title`,`name`,`type`,`role`].includes(e.name.name)).map(e=>`${e.name.name}=${V(e.value)??`{expr}`}`).sort().join(`;`),i=e.attributes.filter(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&/^on[A-Z]/.test(e.name.name)).map(e=>e.name.name).sort().join(`,`);return[t,n??``,r,i].filter(Boolean).join(` | `)}function Ae(e,t,n={},r=L(e,t)){let{program:a,commentsByEnd:o}=r,s=[],c=(e,t=e.source)=>{s.push({...e,fingerprint:xe({...e,source:t})})},l=e=>Object.prototype.hasOwnProperty.call(n.componentProps??{},e)?n.componentProps[e]:[],u=e=>{if(e.type!==`JSXIdentifier`)return!1;let t=e.name;return Ee(e)||g.test(t)&&l(t).includes(`children`)};function d(e){let t=e.length-1;for(;t>=0&&!De(e[t],e[t-1]);)t--;if(t<0)return`<module>`;let n=e[t],r=e[t-1];return r?.type===`VariableDeclarator`&&r.id.type===`Identifier`?r.id.name:n.type===`FunctionDeclaration`&&n.id?n.id.name:`<anonymous>`}function f(e,n){if(G([...n,e]))return;let r=e.children,i=r.filter(e=>e.type===`JSXExpressionContainer`);if(!i.length||r.some(e=>e.type!==`JSXText`&&e.type!==`JSXExpressionContainer`)||!i.every(e=>W(e.expression)!==void 0)||!r.some(e=>e.type===`JSXText`&&U(H(e))))return;let a=e.openingElement.name;if(!u(a))return;let o=[];for(let e of i){let t=W(e.expression),n=t;for(let e=2;o.includes(n);e++)n=`${t}_${e}`;o.push(n)}let s=[];for(let e of o){let t=he(e),n=t;for(let e=2;s.includes(n);e++)n=`${t}_${e}`;s.push(n)}let l=``,f=``,p=0;for(let e of r)if(e.type===`JSXText`){let t=H(e);l+=t,f+=me(t)}else l+=`{${o[p]}}`,f+=`{${s[p]}}`,p++;let m=I(f);if(!m)return;let h=a.name;c({source:m,...we(l),kind:`mixed`,placeholders:s,file:t,start:e.start,end:e.end,tag:h,component:d(n),context:K(e.openingElement,h)},I(l))}function p(e,n){if(G([...n,e]))return;let r=H(e),i=I(r);if(!i)return;let a=ke(n);if(!a)return;let{index:o,element:s}=a;if(s.children.some(e=>e.type===`JSXExpressionContainer`))return;let l=s.openingElement.name;if(!u(l))return;let f=l.name;c({source:i,...we(r),kind:`text`,file:t,start:e.start,end:e.end,tag:f,component:d(n.slice(0,o)),context:K(s.openingElement,f)})}function m(e,n){if(e.name.type!==`JSXIdentifier`)return;let r=e.name.name,a=n[n.length-1];if(a?.type!==`JSXOpeningElement`)return;let o=a.name.type===`JSXIdentifier`?a.name.name:``;if(!(Ee(a.name)?i.has(r):l(o).includes(r))||G([...n,e])||!z(e.value))return;let s=I(B(e.value.value));if(!s||!U(s))return;let u=ke(n);if(!u)return;let{index:f,element:p}=u;c({source:s,kind:`attribute`,file:t,start:e.value.start,end:e.value.end,tag:o,component:d(n.slice(0,f)),attribute:r,context:K(p.openingElement,o,r)})}function h(r,i){if(!z(r)||i[i.length-1]?.type===`JSXAttribute`||i.some(e=>e.start===r.start)||!Ce(e,o,r.start,n.stringMarker??`i18n`))return;let a=I(r.value);a&&U(a)&&c({source:a,kind:`string`,file:t,start:r.start,end:r.end,tag:``,component:d(i),context:``})}return Se(a,(e,t)=>{switch(e.type){case`JSXElement`:return f(e,t);case`JSXText`:return p(e,t);case`JSXAttribute`:return m(e,t);case`Literal`:return h(e,t)}}),s.sort((e,t)=>e.start-t.start)}function je(e,t,r,i,a,o=!1,s=`en`,c=L(e,t)){let{program:l}=c,u={mixed:/* @__PURE__ */ new Map,text:/* @__PURE__ */ new Map,attribute:/* @__PURE__ */ new Map,string:/* @__PURE__ */ new Map};for(let e of r)u[e.kind]?.set(e.start,e);let d=r.map(e=>e.start).sort((e,t)=>e-t),f=(e,t)=>{let n=0,r=d.length;for(;n<r;){let t=n+r>>1;d[t]<e?n=t+1:r=t}return n<d.length&&d[n]<t},h=[];Se(l,e=>{if(!f(e.start,e.end))return!1;if(e.type===`JSXElement`){let t=u.mixed.get(e.start);t&&h.push({kind:`mixed`,node:e,message:t})}else if(e.type===`JSXText`){let t=u.text.get(e.start);t&&h.push({kind:`text`,node:e,message:t})}else if(e.type===`JSXAttribute`&&e.value?.type===`Literal`){let t=u.attribute.get(e.value.start);t&&h.push({kind:`attribute`,node:e.value,message:t})}else if(e.type===`Literal`&&typeof e.value==`string`){let t=u.string.get(e.start);t&&h.push({kind:`string`,node:e,message:t})}});let g=new ue(e),_=!1,v=!1,y=!1,b=e=>i===s?e.source:a[e.id]??e.source,x=e=>o?(_=!0,`${O}(${JSON.stringify(e.id)}, ${JSON.stringify(e.source)})`):JSON.stringify(b(e)),C=e=>{let t=e.prefix??``,n=e.suffix??``;return o?[t&&JSON.stringify(t),x(e),n&&JSON.stringify(n)].filter(Boolean).join(` + `):JSON.stringify(`${t}${b(e)}${n}`)};for(let e of h.reverse()){let{message:t}=e;if(e.kind===`mixed`){let r=e.node;if(!r.closingElement)continue;let a=t.placeholders??[],s=r.children.flatMap(e=>e.type===`JSXExpressionContainer`?[e.expression]:[]),c=e=>g.slice(s[e].start,s[e].end),l=r.openingElement.end,u=r.closingElement.start,d=a.map((e,t)=>`${JSON.stringify(e)}: ${c(t)}`).join(`, `);if(v=!0,o){y=!0,g.overwrite(l,u,`{${p}(${C(t)}, {${d}}, ${n}())}`);continue}let f=b(t),m=ve(f,a)?f:t.source,h=JSON.stringify(`${t.prefix??``}${m}${t.suffix??``}`);g.overwrite(l,u,`{${p}(${h}, {${d}}, ${JSON.stringify(i)})}`)}else e.kind===`text`?g.overwrite(e.node.start,e.node.end,`{${C(t)}}`):e.kind===`attribute`?g.overwrite(e.node.start,e.node.end,o?`{${x(t)}}`:x(t)):g.overwrite(e.node.start,e.node.end,x(t))}let w=[..._?[`t as ${O}`]:[],...y?[`getLocale as ${n}`]:[]];return w.length&&g.prepend(`import { ${w.join(`, `)} } from "${m}";\n`),v&&g.prepend(`import { format as ${p} } from "${S}";\n`),{code:g.toString(),map:g.generateMap({source:t,hires:!0,includeContent:!0})}}function Me(e,t,n,r,i){try{let a=i+`\0`+n,o=t.get(a);return o||(o=new e(n,i),t.set(a,o)),o.formatToParts(r).map(e=>e.value)}catch(e){return console.warn(`autoi18n: could not format `+JSON.stringify(n)+`.`,e),[n]}}function Ne(){return[`import { IntlMessageFormat } from ${JSON.stringify(T)};`,`const formatMessage = ${Me.toString()};`,`const cache = new Map();`,`export function format(text, values, locale) { return formatMessage(IntlMessageFormat, cache, text, values, locale); }`].join(`
|
|
1
|
+
import{t as e}from"./debug-DVrC55pP.js";import{A as t,C as n,D as r,E as i,F as a,I as o,L as s,M as c,N as l,O as u,P as d,R as f,S as p,T as m,b as h,c as g,g as _,i as v,j as y,k as b,l as x,m as S,n as C,o as w,p as T,r as E,t as ee,u as te,v as D,w as O,x as ne,y as k}from"./registry-W7usElv-.js";import{n as A,t as j}from"./resolve-ELDMKdoA.js";import{ArkErrors as M}from"arktype";import{glob as re,readFile as ie,rm as N,stat as ae}from"node:fs/promises";import P from"node:path";import{fileURLToPath as oe}from"node:url";import{createHash as se}from"node:crypto";import{IntlMessageFormat as ce}from"intl-messageformat";import{parseSync as le}from"rolldown/utils";import ue from"magic-string";async function de(e,t,n){let r=Array(e.length),i=0;return await Promise.all(Array.from({length:Math.min(Math.max(1,t),e.length)},async()=>{for(;i<e.length;){let t=i++;try{r[t]={status:`fulfilled`,value:await n(e[t])}}catch(e){r[t]={status:`rejected`,reason:e}}}})),r}var fe=class{available;waiters=[];constructor(e){this.available=Math.max(1,Math.floor(e))}async run(e){await this.acquire();try{return await e()}finally{this.release()}}acquire(){return this.available>0?(this.available--,Promise.resolve()):new Promise(e=>this.waiters.push(e))}release(){let e=this.waiters.shift();e?e():this.available++}};const pe=/* @__PURE__ */ new Set([1,2,3,4,5,6]);function me(e){return e.replace(/['{}]/g,e=>e===`'`?`''`:`'${e}'`)}function he(e){return e.replace(/[^\p{L}\p{N}_]/gu,`_`)}function ge(e){let t;try{t=new ce(e,`en`).getAst()}catch{return}let n=/* @__PURE__ */ new Set,r=e=>{for(let t of e)if(pe.has(t.type)&&t.value&&n.add(t.value),t.children&&r(t.children),t.options)for(let e of Object.values(t.options))r(e.value)};return r(t),[...n]}function _e(e,t){let n=new Set(e),r=new Set(t);return n.size===r.size&&[...n].every(e=>r.has(e))}function ve(e,t){if(e.trim()===``)return!1;let n=ge(e);return n!==void 0&&_e(n,t)}var F=class extends Error{partial;failed;constructor(e,t,n){super(e),this.partial=t,this.failed=n,this.name=`PartialTranslationError`}};async function ye(n){let{provider:r,sourceLocale:i,locale:a,requests:o,previous:s,debug:c}=n,l=new Set(o.map(e=>e.id)),{messages:u,sources:d}=t(s,l).file,f=o.filter(e=>u[e.id]===void 0||d[e.id]!==e.source),p={},m=0,h;if(f.length&&r){if(c)for(let t of f)e(`sent ${i} -> ${a} ${t.id}: ${JSON.stringify(t.source)} (context: ${t.context})`);try{p=await r.translate({sourceLocale:i,targetLocale:a,messages:f})}catch(e){if(!(e instanceof F))throw e;p=e.partial,m=e.failed,h=e.message}if(c)for(let t of f){let n=p[t.id];e(`received ${i} -> ${a} ${t.id}: ${typeof n==`string`?JSON.stringify(n):`(no translation)`}`)}}let g={...u},_={...d},v=0;for(let e of f){delete g[e.id],delete _[e.id];let t=p[e.id];typeof t==`string`&&t.trim()&&(e.kind===`mixed`&&!ve(t,e.placeholders??[])?v++:(g[e.id]=t,_[e.id]=e.source))}let y=o.filter(e=>g[e.id]===void 0).length;return{file:{messages:g,sources:_},untranslated:y,rejected:v,requested:f.length,failed:m,failure:h}}async function be(e){let t=structuredClone(e.registry),{ambiguous:n,messages:r}=E(t,e.files.flatMap(e=>e.messages)),i=C(t,e.orphanRetentionMinutes),a={};for(let e of r)a[e.id]=e.source;let o=r.map(e=>({id:e.id,source:e.source,context:`${e.component} > ${e.context}`,kind:e.kind,placeholders:e.placeholders})),s=await de(e.locales,e.localeConcurrency,t=>ye({provider:e.provider,sourceLocale:e.sourceLocale,locale:t,requests:o,previous:e.previous.get(t)??u(),debug:e.debug})),c=/* @__PURE__ */ new Map,l=/* @__PURE__ */ new Map;return s.forEach((t,n)=>{let r=e.locales[n];t.status===`fulfilled`?c.set(r,t.value):l.set(r,t.reason)}),{registry:t,ambiguous:n,pruned:i,messages:r,sourceFile:{messages:a,sources:{}},locales:c,failures:l}}function I(e){return e.replace(/\s+/g,` `).trim()}function xe(e){let t=[I(e.source),e.kind,e.tag.toLowerCase(),e.attribute??``,e.context].join(``);return se(`sha256`).update(t).digest(`hex`).slice(0,24)}function L(e,t){let n=le(t,e,{lang:`tsx`,sourceType:`unambiguous`});if(n.errors.length)throw SyntaxError(`${t}: ${n.errors[0].message}`);return{program:n.program,commentsByEnd:new Map(n.comments.map(e=>[e.end,e]))}}function Se(e,t){let n=[],r=e=>{if(t(e,n)!==!1){n.push(e);for(let t of Object.keys(e)){if(t===`parent`)continue;let n=e[t];Array.isArray(n)?n.forEach(e=>R(e)&&r(e)):R(n)&&r(n)}n.pop()}};r(e)}function R(e){return typeof e==`object`&&!!e&&typeof e.type==`string`}function z(e){return R(e)&&e.type===`Literal`&&typeof e.value==`string`}function Ce(e,t,n,r){let i=n;for(;;){let n=i;for(;n>0&&/\s/.test(e[n-1]);)n--;let a=t.get(n);if(!a)return!1;if(a.type===`Block`&&a.value.trim()===r)return!0;i=a.start}}function B(e){return e.includes(`&`)?e.replace(ne,(e,t,n)=>{if(n!==void 0)return k.get(n)??e;let r=t.startsWith(`x`)?parseInt(t.slice(1),16):parseInt(t,10);return r<=1114111?String.fromCodePoint(r):e}):e}function we(e){return z(e)?B(e.value):void 0}function V(e){return B(e.value)}function Te(e){return{prefix:e.match(/^\s*/)?.[0]??``,suffix:e.match(/\s*$/)?.[0]??``}}const H=e=>/[A-Za-z]/.test(e);function Ee(e){return e.attributes.some(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&e.name.name===`translate`&&we(e.value)===`no`)}function De(e){return e.type===`JSXIdentifier`&&!g.test(e.name)&&!_.has(e.name)}function U(e){if(e.type===`Identifier`)return e.name;if(e.type===`MemberExpression`&&!e.computed&&e.property.type===`Identifier`){let t=U(e.object);return t?`${t}.${e.property.name}`:void 0}}const Oe=(e,t)=>e.type===`FunctionExpression`?!(t?.type===`MethodDefinition`||t?.type===`Property`&&(t.method||t.kind!==`init`)):e.type===`FunctionDeclaration`||e.type===`ArrowFunctionExpression`;function ke(e,t){for(let n=e.length-1;n>=0;n--)if(t(e[n]))return n;return-1}function Ae(e){let t=ke(e,e=>e.type===`JSXElement`);return t<0?void 0:{index:t,element:e[t]}}function W(e){return e.some(e=>{let t=e.type===`JSXOpeningElement`?e:e.type===`JSXElement`?e.openingElement:void 0;return t!==void 0&&Ee(t)})}function G(e,t,n){let r=e.attributes.filter(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&[`aria-label`,`title`,`name`,`type`,`role`].includes(e.name.name)).map(e=>`${e.name.name}=${we(e.value)??`{expr}`}`).sort().join(`;`),i=e.attributes.filter(e=>e.type===`JSXAttribute`&&e.name.type===`JSXIdentifier`&&/^on[A-Z]/.test(e.name.name)).map(e=>e.name.name).sort().join(`,`);return[t,n??``,r,i].filter(Boolean).join(` | `)}function je(e,t,n={},r=L(e,t)){let{program:a,commentsByEnd:o}=r,s=[],c=(e,t=e.source)=>{s.push({...e,fingerprint:xe({...e,source:t})})},l=e=>Object.prototype.hasOwnProperty.call(n.componentProps??{},e)?n.componentProps[e]:[],u=e=>{if(e.type!==`JSXIdentifier`)return!1;let t=e.name;return De(e)||g.test(t)&&l(t).includes(`children`)};function d(e){let t=e.length-1;for(;t>=0&&!Oe(e[t],e[t-1]);)t--;if(t<0)return`<module>`;let n=e[t],r=e[t-1];return r?.type===`VariableDeclarator`&&r.id.type===`Identifier`?r.id.name:n.type===`FunctionDeclaration`&&n.id?n.id.name:`<anonymous>`}function f(e,n){if(W([...n,e]))return;let r=e.children,i=r.filter(e=>e.type===`JSXExpressionContainer`);if(!i.length||r.some(e=>e.type!==`JSXText`&&e.type!==`JSXExpressionContainer`)||!i.every(e=>U(e.expression)!==void 0)||!r.some(e=>e.type===`JSXText`&&H(V(e))))return;let a=e.openingElement.name;if(!u(a))return;let o=[];for(let e of i){let t=U(e.expression),n=t;for(let e=2;o.includes(n);e++)n=`${t}_${e}`;o.push(n)}let s=[];for(let e of o){let t=he(e),n=t;for(let e=2;s.includes(n);e++)n=`${t}_${e}`;s.push(n)}let l=``,f=``,p=0;for(let e of r)if(e.type===`JSXText`){let t=V(e);l+=t,f+=me(t)}else l+=`{${o[p]}}`,f+=`{${s[p]}}`,p++;let m=I(f);if(!m)return;let h=a.name;c({source:m,...Te(l),kind:`mixed`,placeholders:s,file:t,start:e.start,end:e.end,tag:h,component:d(n),context:G(e.openingElement,h)},I(l))}function p(e,n){if(W([...n,e]))return;let r=V(e),i=I(r);if(!i)return;let a=Ae(n);if(!a)return;let{index:o,element:s}=a;if(s.children.some(e=>e.type===`JSXExpressionContainer`))return;let l=s.openingElement.name;if(!u(l))return;let f=l.name;c({source:i,...Te(r),kind:`text`,file:t,start:e.start,end:e.end,tag:f,component:d(n.slice(0,o)),context:G(s.openingElement,f)})}function m(e,n){if(e.name.type!==`JSXIdentifier`)return;let r=e.name.name,a=n[n.length-1];if(a?.type!==`JSXOpeningElement`)return;let o=a.name.type===`JSXIdentifier`?a.name.name:``;if(!(De(a.name)?i.has(r):l(o).includes(r))||W([...n,e])||!z(e.value))return;let s=I(B(e.value.value));if(!s||!H(s))return;let u=Ae(n);if(!u)return;let{index:f,element:p}=u;c({source:s,kind:`attribute`,file:t,start:e.value.start,end:e.value.end,tag:o,component:d(n.slice(0,f)),attribute:r,context:G(p.openingElement,o,r)})}function h(r,i){if(!z(r)||i[i.length-1]?.type===`JSXAttribute`||i.some(e=>e.start===r.start)||!Ce(e,o,r.start,n.stringMarker??`i18n`))return;let a=I(r.value);a&&H(a)&&c({source:a,kind:`string`,file:t,start:r.start,end:r.end,tag:``,component:d(i),context:``})}return Se(a,(e,t)=>{switch(e.type){case`JSXElement`:return f(e,t);case`JSXText`:return p(e,t);case`JSXAttribute`:return m(e,t);case`Literal`:return h(e,t)}}),s.sort((e,t)=>e.start-t.start)}function Me(e,t,r,i,a,o=!1,s=`en`,c=L(e,t)){let{program:l}=c,u={mixed:/* @__PURE__ */ new Map,text:/* @__PURE__ */ new Map,attribute:/* @__PURE__ */ new Map,string:/* @__PURE__ */ new Map};for(let e of r)u[e.kind]?.set(e.start,e);let d=r.map(e=>e.start).sort((e,t)=>e-t),f=(e,t)=>{let n=0,r=d.length;for(;n<r;){let t=n+r>>1;d[t]<e?n=t+1:r=t}return n<d.length&&d[n]<t},h=[];Se(l,e=>{if(!f(e.start,e.end))return!1;if(e.type===`JSXElement`){let t=u.mixed.get(e.start);t&&h.push({kind:`mixed`,node:e,message:t})}else if(e.type===`JSXText`){let t=u.text.get(e.start);t&&h.push({kind:`text`,node:e,message:t})}else if(e.type===`JSXAttribute`&&e.value?.type===`Literal`){let t=u.attribute.get(e.value.start);t&&h.push({kind:`attribute`,node:e.value,message:t})}else if(e.type===`Literal`&&typeof e.value==`string`){let t=u.string.get(e.start);t&&h.push({kind:`string`,node:e,message:t})}});let g=new ue(e),_=!1,v=!1,y=!1,b=e=>i===s?e.source:a[e.id]??e.source,x=e=>o?(_=!0,`${O}(${JSON.stringify(e.id)}, ${JSON.stringify(e.source)})`):JSON.stringify(b(e)),C=e=>{let t=e.prefix??``,n=e.suffix??``;return o?[t&&JSON.stringify(t),x(e),n&&JSON.stringify(n)].filter(Boolean).join(` + `):JSON.stringify(`${t}${b(e)}${n}`)};for(let e of h.reverse()){let{message:t}=e;if(e.kind===`mixed`){let r=e.node;if(!r.closingElement)continue;let a=t.placeholders??[],s=r.children.flatMap(e=>e.type===`JSXExpressionContainer`?[e.expression]:[]),c=e=>g.slice(s[e].start,s[e].end),l=r.openingElement.end,u=r.closingElement.start,d=a.map((e,t)=>`${JSON.stringify(e)}: ${c(t)}`).join(`, `);if(v=!0,o){y=!0,g.overwrite(l,u,`{${p}(${C(t)}, {${d}}, ${n}())}`);continue}let f=b(t),m=ve(f,a)?f:t.source,h=JSON.stringify(`${t.prefix??``}${m}${t.suffix??``}`);g.overwrite(l,u,`{${p}(${h}, {${d}}, ${JSON.stringify(i)})}`)}else e.kind===`text`?g.overwrite(e.node.start,e.node.end,`{${C(t)}}`):e.kind===`attribute`?g.overwrite(e.node.start,e.node.end,o?`{${x(t)}}`:x(t)):g.overwrite(e.node.start,e.node.end,x(t))}let w=[..._?[`t as ${O}`]:[],...y?[`getLocale as ${n}`]:[]];return w.length&&g.prepend(`import { ${w.join(`, `)} } from "${m}";\n`),v&&g.prepend(`import { format as ${p} } from "${S}";\n`),{code:g.toString(),map:g.generateMap({source:t,hires:!0,includeContent:!0})}}function Ne(e,t,n,r,i){try{let a=i+`\0`+n,o=t.get(a);return o||(o=new e(n,i),t.set(a,o)),o.formatToParts(r).map(e=>e.value)}catch(e){return console.warn(`autoi18n: could not format `+JSON.stringify(n)+`.`,e),[n]}}function Pe(){return[`import { IntlMessageFormat } from ${JSON.stringify(T)};`,`const formatMessage = ${Ne.toString()};`,`const cache = new Map();`,`export function format(text, values, locale) { return formatMessage(IntlMessageFormat, cache, text, values, locale); }`].join(`
|
|
2
2
|
`)+`
|
|
3
|
-
`}function
|
|
4
|
-
`)}\n};`,`const sourceLocale = ${
|
|
3
|
+
`}function K(e){return JSON.stringify(e).replace(/[<>&\u2028\u2029]/g,e=>`\\u${e.charCodeAt(0).toString(16).padStart(4,`0`)}`)}function Fe(e,t){return[`const loaders = {\n${Object.entries(e).map(([e,t])=>` ${K(e)}: () => import(${K(t)}).then(m => (m.default ?? m).messages)`).join(`,
|
|
4
|
+
`)}\n};`,`const sourceLocale = ${K(t)};`,`const resolveLocale = ${j.toString()};`,`const createLocaleLoader = ${A.toString()};`,`const loadLocale = createLocaleLoader(loaders, sourceLocale);`,`const listeners = new Set();`,`let currentLocale = sourceLocale;`,`let messages = {};`,`let version = 0;`,`let latestRequest = 0;`,`export const locales = Object.keys(loaders);`,`export function getLocale() { return currentLocale; }`,`export function getVersion() { return version; }`,`export function resolveSupported(preferred) { return resolveLocale(preferred, locales, sourceLocale); }`,`export async function setLocale(locale) {`,` const request = ++latestRequest;`,` let selected = loaders[locale] ? locale : sourceLocale;`,` let catalog;`,` try {`,` catalog = await loadLocale(selected);`,` } catch (error) {`,` console.warn('autoi18n: could not load "' + selected + '", falling back to "' + sourceLocale + '".', error);`,` selected = sourceLocale;`,` try {`,` catalog = await loadLocale(selected);`,` } catch (fallbackError) {`,` // Keep the current locale and messages so the UI stays consistent. Texts without a catalogue show their source.`,` console.error('autoi18n: could not load the source catalogue "' + sourceLocale + '".', fallbackError);`,` return;`,` }`,` }`,` // A slower earlier request must not overwrite the locale the user picked last.`,` if (request !== latestRequest) return;`,` messages = catalog;`,` currentLocale = selected;`,` notify();`,`}`,`function notify() {`,` version++;`,` listeners.forEach(fn => fn(currentLocale));`,`}`,`export function subscribe(fn) { listeners.add(fn); return () => { listeners.delete(fn); }; }`,`export function t(id, source) { return messages[id] ?? source; }`,`// Dev server pushes the catalogues that changed after a source change; no page reload is needed.`,`if (import.meta.hot) {`,` import.meta.hot.on("autoi18n:update", data => {`,` for (const [locale, catalog] of Object.entries(data.catalogs)) loadLocale.prime(locale, catalog);`,` if (!data.catalogs[currentLocale]) return;`,` messages = data.catalogs[currentLocale];`,` notify();`,` });`,`}`].join(`
|
|
5
5
|
`)+`
|
|
6
|
-
`}async function q(e){try{let t=await ae(e);return`${t.mtimeMs}:${t.size}`}catch{return}}var Fe=class{entries=/* @__PURE__ */ new Map;async read(e,t){let n=await q(e),r=this.entries.get(e);if(n!==void 0&&r?.stamp===n)return r.value;let i=await t();return n!==void 0&&this.entries.set(e,{stamp:n,value:i,written:!1}),i}async holds(e,t,n){let r=this.entries.get(e);return!r?.written||!n(r.value,t)?!1:await q(e)===r.stamp}async write(e,t,n){await n();let r=await q(e);r!==void 0&&this.entries.set(e,{stamp:r,value:t,written:!0})}};function J(e,t){if(!e)return!1;let n=Object.keys(t);return n.length===Object.keys(e).length&&n.every(n=>e[n]===t[n])}function Ie(e,t){return J(e.messages,t.messages)&&J(e.sources,t.sources)}var Le=class{extractions=/* @__PURE__ */ new Map;parsedSources=/* @__PURE__ */ new Map;fileMessages=/* @__PURE__ */ new Map;catalogs=/* @__PURE__ */ new Map;catalogFiles={};warnedFiles=/* @__PURE__ */ new Set;legacySidecarsRemoved=!1;locales=new Fe;registries=new Fe;hashOf(e){return this.extractions.get(e)?.hash}readLocale(e,t){return this.locales.read(e,()=>y(e,t))}async writeLocale(e,t){if(!await this.locales.holds(e,t,Ie))return this.locales.write(e,t,()=>c(e,t))}readRegistry(e,t){return this.registries.read(e,()=>ee(e,t))}writeRegistry(e,t){return this.registries.write(e,t,()=>v(e,t))}},Re=class{providers;onFallback;constructor(e,t=()=>{}){this.providers=e,this.onFallback=t}async translate(e){let t={},n=e.messages,r,i;for(let[a,{name:o,provider:s}]of this.providers.entries()){try{Object.assign(t,await s.translate({...e,messages:n}))}catch(e){r??=e,i=e,e instanceof F&&Object.assign(t,e.partial);let s=this.providers[a+1];s&&n.some(e=>t[e.id]===void 0)&&this.onFallback(`${o} failed (${ze(e)}); sending the rest to ${s.name}.`)}if(n=n.filter(e=>t[e.id]===void 0),!n.length)return t}throw Object.keys(t).length===0?r:new F(`${n.length} messages failed to translate: ${ze(i)}`,t,n.length)}};const ze=e=>e instanceof Error?e.message:String(e),Be=e=>typeof e==`object`&&!!e&&typeof e.translate==`function`;function Ve(e,t){if(e===void 0)return;if(Be(e))return e;let{primary:n,...r}=e,i=[];for(let[e,t]of Object.entries(r))if(t!==void 0){if(!Be(t))throw Error(`autoi18n: provider "${e}" must be an object with a translate(input) method.`);i.push({name:e,provider:t})}if(n!==void 0){let e=i.findIndex(e=>e.name===n);if(e<0)throw Error(`autoi18n: primary provider "${n}" is not configured.`);i.unshift(...i.splice(e,1))}if(i.length!==0)return i.length===1?i[0].provider:new Re(i,t)}var Y=class extends Error{retryable;retryAfterMs;constructor(e,t,n){super(e),this.retryable=t,this.retryAfterMs=n}};function He(e,t=Date.now()){if(!e)return;let n=Number(e);if(Number.isFinite(n))return Math.min(Math.max(n,0)*1e3,D);let r=Date.parse(e);if(!Number.isNaN(r))return Math.min(Math.max(r-t,0),D)}const Ue=e=>new Promise(t=>setTimeout(t,e));function X(e){return`You are a professional software localization engine. Translate UI messages from ${e.sourceLocale} to ${e.targetLocale}. Preserve placeholders exactly. Messages of kind "mixed" are ICU MessageFormat: keep each {name} argument unchanged, keep apostrophes and braces valid ICU (a literal apostrophe is written as two apostrophes, and literal braces are quoted), and use plural or select only when the target language needs them. Respect context and UI conventions. Return ONLY a JSON object mapping message IDs to translated strings. Do not translate product names or code.`}function Z(e){return JSON.stringify(e.map(e=>({id:e.id,text:e.source,context:e.context,kind:e.kind})))}async function We(e){let t=e.status===408||e.status===409||e.status===429||e.status>=500;return await e.body?.cancel().catch(()=>void 0),new Y(`Translation API failed (${e.status})`,t,He(e.headers.get(`retry-after`)))}var Q=class{options;slots;constructor(e){this.options=e,this.slots=new fe(e.concurrency??4)}async translate(e){let t=Math.max(1,this.options.batchSize??40),n=[];for(let r=0;r<e.messages.length;r+=t)n.push(e.messages.slice(r,r+t));let r=await Promise.allSettled(n.map(t=>this.slots.run(()=>this.translateBatch({...e,messages:t})))),i={},a=[],o=0,s=0;if(r.forEach((e,t)=>{e.status===`fulfilled`?(s++,Object.assign(i,e.value)):(a.push(e.reason),o+=n[t].length)}),!a.length)return i;throw s?new F(`${o} messages failed to translate: ${a[0].message}`,i,o):a[0]}async translateBatch(e){let t=this.options.maxRetries??2,n=this.options.retryDelayMs??500;for(let r=0;;r++)try{return await this.request(e)}catch(e){if(e instanceof Y&&!e.retryable||r>=t)throw e;let i=e instanceof Y?e.retryAfterMs:void 0;await Ue(i??n*2**r*(.5+Math.random()/2))}}async post(e,t,n){let r=await fetch(e,{method:`POST`,signal:AbortSignal.timeout(this.options.timeoutMs??6e4),headers:{...t,"content-type":`application/json`},body:JSON.stringify(n)});if(!r.ok)throw await We(r);return r.json()}async complete(e,t,n,r,i,a){let o=r(await this.post(e,t,n));return this.toCatalog(o instanceof M?void 0:i(o),a.messages)}toCatalog(e,t){let n=e?.trim().replace(/^```(?:json)?\s*|\s*```$/g,``);if(!n)throw new Y(`Translation provider returned no content`,!0);let r;try{r=JSON.parse(n)}catch{throw new Y(`Translation provider returned invalid JSON`,!0)}let i=s(r);if(i instanceof M)throw new Y(`Translation provider returned a non-object`,!0);let a=new Set(t.map(e=>e.id)),o={};for(let[e,t]of Object.entries(i))a.has(e)&&typeof t==`string`&&t.trim()&&(o[e]=t);return o}},Ge=class extends Q{async request(e){let{apiKey:t,model:n,baseURL:r=h}=this.options;return this.complete(`${r}/chat/completions`,t?{Authorization:`Bearer ${t}`}:{},{model:n,temperature:0,response_format:{type:`json_object`},messages:[{role:`system`,content:X(e)},{role:`user`,content:Z(e.messages)}]},d,e=>e.choices[0].message.content??void 0,e)}},Ke=class extends Q{async request(e){return this.complete(`${this.options.baseURL??`https://api.anthropic.com/v1`}/messages`,{"x-api-key":this.options.apiKey,"anthropic-version":w},{model:this.options.model,max_tokens:this.options.maxTokens??8192,system:X(e),messages:[{role:`user`,content:Z(e.messages)}]},l,e=>e.content.filter(e=>e.type===`text`).map(e=>e.text??``).join(``),e)}},qe=class extends Q{async request(e){return this.complete(`${this.options.baseURL??`https://generativelanguage.googleapis.com/v1beta`}/models/${encodeURIComponent(this.options.model)}:generateContent`,{"x-goog-api-key":this.options.apiKey},{systemInstruction:{parts:[{text:X(e)}]},contents:[{role:`user`,parts:[{text:Z(e.messages)}]}],generationConfig:{responseMimeType:`application/json`,maxOutputTokens:this.options.maxOutputTokens??8192}},a,e=>(e.candidates[0]?.content?.parts??[]).map(e=>e.text??``).join(``),e)}};const Je=[`bg`,`cs`,`da`,`de`,`el`,`es`,`et`,`fi`,`fr`,`ga`,`hr`,`hu`,`it`,`lt`,`lv`,`mt`,`nl`,`pl`,`pt`,`ro`,`sk`,`sl`,`sv`],Ye=[`da`,`fi`,`is`,`no`,`sv`],Xe=[`et`,`lv`,`lt`],Ze=[`bg`,`bs`,`el`,`hr`,`mk`,`ro`,`sq`,`sr`,`sl`],Qe=[`nl`,`fr`,`lb`],$e=[.../* @__PURE__ */ new Set([...Je,`no`,`is`,`sq`,`bs`,`mk`,`sr`,`uk`,`ru`,`be`,`ca`])],$=[`zh`,`ja`,`ko`,`mn`],et=[`hi`,`bn`,`ur`,`pa`,`ta`,`te`,`mr`,`gu`,`kn`,`ml`,`ne`,`si`],tt=[`id`,`ms`,`my`,`km`,`lo`,`fil`,`th`,`vi`],nt=[`kk`,`ky`,`tg`,`tk`,`uz`],rt=[`hy`,`az`,`ka`],it=[.../* @__PURE__ */ new Set([...$,...et,...tt,...nt,...rt])],at=[`ar`,`fa`,`he`,`tr`,`ku`],ot=[`ar`,`fr`,`pt`,`sw`,`am`,`ha`,`yo`,`ig`,`zu`,`xh`,`af`,`so`,`rw`,`wo`],st=[`en`,`fr`,`es`],ct=[`es`,`en`,`fr`,`ht`,`nl`],lt=[`es`,`pt`,`qu`,`gn`,`ay`],ut=[.../* @__PURE__ */ new Set([...st,...ct,...lt])],dt=[`en`,`mi`,`sm`,`to`],ft=[`id`,`ms`,`my`,`km`,`lo`,`fil`,`th`,`vi`,`en`],pt=[`es`,`pt`,`gn`],mt=[`ar`,`en`],ht=[`ru`,`be`,`hy`,`az`,`kk`,`ky`,`tg`,`uz`],gt=[`en`,`fr`,`de`,`it`,`ja`],_t=[`fr`,`ht`,`wo`,`ln`,`rw`],vt=[`en`,`zh`,`hi`,`es`,`ar`,`bn`,`pt`,`ru`,`ja`,`de`,`fr`,`ko`,`it`,`tr`,`vi`,`id`,`ur`];function yt(e){let t=o(e);if(!(t instanceof M))return;let n=t.map(e=>`${e.path.join(`.`)||`options`}: ${e.message}`).join(`; `);throw Error(`autoi18n: invalid options (${n}).`)}const bt=e=>({info:t=>e.info(t),warn:t=>e.warn(t),error:t=>e.error(t)});async function xt(e,t){await Promise.all(t.map(t=>N(P.join(e,`${t}.sources.json`),{force:!0})))}function St(t){yt(t);let n=t.sourceLocale??`en`,i=t.directory??`.autoi18n`,a=t.delivery??`build`,o=t.debug??!1,s={componentProps:t.componentProps,stringMarker:t.stringMarker},c=t.orphanRetentionMinutes??43200,l,u=Ve(t.provider,e=>l?.warn(e)),d,p=``,h=new Le,g,_=Promise.resolve(),v,y=()=>t.locale??process.env.AUTOI18N_LOCALE,C=()=>y()??n,w=()=>P.resolve(d.root,i,`locales`),T=t.include??te,E=t.exclude??x;function ee(){let e=t.locales.filter(e=>e!==n),r=y();return a===`build`&&r&&d.command!==`serve`?e.filter(e=>e===r):e}async function D(){let e=[];for await(let t of re(T,{cwd:d.root,exclude:E}))e.push(P.resolve(d.root,t));return e}async function O(e,t,n){let r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,a=[];for(let o of e){let e=h.extractions.get(o),c;if(e&&t&&o!==n)c=e;else{let t=await ie(o,`utf8`),n=f(t);if(e?.hash===n)c=e;else{let e=L(t,o);c={hash:n,messages:Ae(t,o,s,e)},i.set(o,e)}}r.set(o,c),a.push({file:o,messages:c.messages})}return{nextExtractions:r,nextParsed:i,indexed:a}}function ne(e,n){for(let[r,i]of n.locales)i.requested&&!u&&t.failOnMissingProvider&&e.error(`Missing ${i.requested} ${r} translations and no provider is configured.`),i.rejected&&e.info(`autoi18n: rejected ${i.rejected} ${r} translations with changed placeholders.`),i.untranslated&&(u?e.warn:e.info)(`autoi18n: ${i.untranslated} ${r} messages have no translation and fall back to the source text.`),i.failed&&e.warn(`autoi18n: ${i.failed} ${r} translation batches failed (${i.failure}); they will be requested again on the next build.`)}async function k(r,i){l=r;let a=i!==void 0&&h.extractions.has(i),s=a?[...h.extractions.keys()]:await D(),{nextExtractions:d,nextParsed:f,indexed:m}=await O(s,a,i),g=w(),_=ee(),v=/* @__PURE__ */ new Map;for(let e of _)v.set(e,await h.readLocale(b(g,e),r.warn));let y=await be({files:m,registry:await h.readRegistry(p,r.warn),orphanRetentionMinutes:c,sourceLocale:n,locales:_,localeConcurrency:Math.max(1,Math.floor(t.localeConcurrency??2)),previous:v,provider:u,debug:o});if(o)for(let t of y.pruned)e(`removed ${t.id} ${JSON.stringify(t.source)} (last seen ${t.lastSeen})`);if(y.pruned.length&&r.info(`autoi18n: removed ${y.pruned.length} registry entries no longer in the source.`),await h.writeRegistry(p,y.registry),y.ambiguous.length){let e=`autoi18n: ${y.ambiguous.length} duplicated texts changed occurrence count, so their IDs may have moved (e.g. "${y.ambiguous[0]}").`;t.onAmbiguous===`error`?r.error(e):r.warn(e)}await h.writeLocale(b(g,n),y.sourceFile),h.legacySidecarsRemoved||=(await xt(g,[n,...t.locales]),!0);let x=/* @__PURE__ */ new Map([[n,y.sourceFile.messages]]);for(let[e,t]of y.locales)await h.writeLocale(b(g,e),t.file),x.set(e,t.file.messages);for(let e of y.failures.keys())x.set(e,v.get(e)?.messages??{});let S=[...x].filter(([e,t])=>!J(h.catalogs.get(e),t)).map(([e])=>e),C=0;h.fileMessages=/* @__PURE__ */ new Map;for(let{file:e,messages:t}of m)h.fileMessages.set(e,y.messages.slice(C,C+t.length)),C+=t.length;h.extractions=d,h.parsedSources=f,h.catalogs=x,h.catalogFiles=Object.fromEntries(t.locales.map(e=>[e,b(g,e)])),ne(r,y),y.messages.length&&r.info(`autoi18n: indexed ${y.messages.length} messages across ${s.length} files.`);let T=[...y.failures];for(let[e,t]of T.slice(1))r.warn(`autoi18n: ${e} translation failed: ${t.message}`);if(T.length)throw T[0][1];return S}function A(e,t){let n=_.then(()=>k(e,t));return _=n.then(()=>void 0,()=>void 0),n}function j(e){e.length&&g?.ws.send({type:`custom`,event:r,data:{catalogs:Object.fromEntries(e.map(e=>[e,h.catalogs.get(e)]))}})}async function M(e){try{j(await A(bt(d.logger),e))}catch(e){d.logger.error(`autoi18n: re-translation failed: ${e.message}`)}}function N(){clearTimeout(v),v=setTimeout(()=>void M(),150)}function ae(e){if(!/\.[jt]sx?$/.test(e))return!1;let t=P.relative(d.root,e).split(P.sep).join(`/`);return T.some(e=>P.matchesGlob(t,e))&&!E.some(e=>P.matchesGlob(t,e))}return{name:`vite-plugin-autoi18n`,enforce:`pre`,configResolved(e){d=e,p=P.resolve(d.root,i,`registry.json`);let r=C();if(r!==n&&!t.locales.includes(r))throw Error(`autoi18n: locale "${r}" is not one of the configured locales (${t.locales.join(`, `)}).`)},resolveId(e,t){if(e===`virtual:autoi18n`)return`\0${m}`;if(e===`virtual:autoi18n-format`)return`\0${S}`;if(t===`\0virtual:autoi18n-format`&&e===`intl-messageformat`)return this.resolve(e,oe(import.meta.url),{skipSelf:!0})},load(e){if(e===`\0virtual:autoi18n`)return Pe(h.catalogFiles,n);if(e===`\0virtual:autoi18n-format`)return Ne()},configureServer(e){g=e,e.watcher.on(`add`,e=>{ae(e)&&N()}),e.watcher.on(`unlink`,e=>{h.fileMessages.has(e)&&N()})},closeBundle(){clearTimeout(v)},async buildStart(){await k({info:e=>this.info(e),warn:e=>this.warn(e),error:e=>this.error(e)})},async transform(e,t){let r=t.split(`?`)[0];if(!h.fileMessages.has(r))return null;let i=f(e);i!==h.hashOf(r)&&await A(bt(d.logger),r);let o=h.fileMessages.get(r);if(!o||i!==h.hashOf(r))return h.warnedFiles.has(r)||(h.warnedFiles.add(r),this.warn(`autoi18n: ${r} changed while it was being indexed; its messages are left untranslated.`)),null;let s=d.command===`serve`?n:C(),c=h.parsedSources.get(r);h.parsedSources.delete(r);let l=je(e,r,o,s,h.catalogs.get(s)??{},a===`runtime`,n,c);return{code:l.code,map:l.map}},async handleHotUpdate({file:e}){if(e.startsWith(w()+P.sep))return[];h.fileMessages.has(e)&&await M(e)}}}export{ot as AFRICA,ut as AMERICAS,ft as ASEAN,it as ASIA,Ke as AnthropicProvider,Ze as BALKANS,Xe as BALTIC,Qe as BENELUX,rt as CAUCASUS,ct as CENTRAL_AMERICA_CARIBBEAN,nt as CENTRAL_ASIA,ht as CIS,$ as EAST_ASIA,Je as EU,$e as EUROPE,_t as FRANCOPHONE,Re as FallbackProvider,gt as G7,mt as GCC,qe as GeminiProvider,pt as MERCOSUR,at as MIDDLE_EAST,Ye as NORDIC,st as NORTH_AMERICA,dt as OCEANIA,Ge as OpenAICompatibleProvider,F as PartialTranslationError,tt as SOUTHEAST_ASIA,lt as SOUTH_AMERICA,et as SOUTH_ASIA,vt as WORLDWIDE_POPULAR,St as default,Ve as resolveProvider};
|
|
6
|
+
`}async function q(e){try{let t=await ae(e);return`${t.mtimeMs}:${t.size}`}catch{return}}var Ie=class{entries=/* @__PURE__ */ new Map;async read(e,t){let n=await q(e),r=this.entries.get(e);if(n!==void 0&&r?.stamp===n)return r.value;let i=await t();return n!==void 0&&this.entries.set(e,{stamp:n,value:i,written:!1}),i}async holds(e,t,n){let r=this.entries.get(e);return!r?.written||!n(r.value,t)?!1:await q(e)===r.stamp}async write(e,t,n){await n();let r=await q(e);r!==void 0&&this.entries.set(e,{stamp:r,value:t,written:!0})}};function J(e,t){if(!e)return!1;let n=Object.keys(t);return n.length===Object.keys(e).length&&n.every(n=>e[n]===t[n])}function Le(e,t){return J(e.messages,t.messages)&&J(e.sources,t.sources)}var Re=class{extractions=/* @__PURE__ */ new Map;parsedSources=/* @__PURE__ */ new Map;fileMessages=/* @__PURE__ */ new Map;catalogs=/* @__PURE__ */ new Map;catalogFiles={};warnedFiles=/* @__PURE__ */ new Set;legacySidecarsRemoved=!1;locales=new Ie;registries=new Ie;hashOf(e){return this.extractions.get(e)?.hash}readLocale(e,t){return this.locales.read(e,()=>y(e,t))}async writeLocale(e,t){if(!await this.locales.holds(e,t,Le))return this.locales.write(e,t,()=>c(e,t))}readRegistry(e,t){return this.registries.read(e,()=>ee(e,t))}writeRegistry(e,t){return this.registries.write(e,t,()=>v(e,t))}},ze=class{providers;onFallback;constructor(e,t=()=>{}){this.providers=e,this.onFallback=t}async translate(e){let t={},n=e.messages,r,i;for(let[a,{name:o,provider:s}]of this.providers.entries()){try{Object.assign(t,await s.translate({...e,messages:n}))}catch(e){r??=e,i=e,e instanceof F&&Object.assign(t,e.partial);let s=this.providers[a+1];s&&n.some(e=>t[e.id]===void 0)&&this.onFallback(`${o} failed (${Be(e)}); sending the rest to ${s.name}.`)}if(n=n.filter(e=>t[e.id]===void 0),!n.length)return t}throw Object.keys(t).length===0?r:new F(`${n.length} messages failed to translate: ${Be(i)}`,t,n.length)}};const Be=e=>e instanceof Error?e.message:String(e),Ve=e=>typeof e==`object`&&!!e&&typeof e.translate==`function`;function He(e,t){if(e===void 0)return;if(Ve(e))return e;let{primary:n,...r}=e,i=[];for(let[e,t]of Object.entries(r))if(t!==void 0){if(!Ve(t))throw Error(`autoi18n: provider "${e}" must be an object with a translate(input) method.`);i.push({name:e,provider:t})}if(n!==void 0){let e=i.findIndex(e=>e.name===n);if(e<0)throw Error(`autoi18n: primary provider "${n}" is not configured.`);i.unshift(...i.splice(e,1))}if(i.length!==0)return i.length===1?i[0].provider:new ze(i,t)}var Y=class extends Error{retryable;retryAfterMs;constructor(e,t,n){super(e),this.retryable=t,this.retryAfterMs=n}};function Ue(e,t=Date.now()){if(!e)return;let n=Number(e);if(Number.isFinite(n))return Math.min(Math.max(n,0)*1e3,D);let r=Date.parse(e);if(!Number.isNaN(r))return Math.min(Math.max(r-t,0),D)}const We=e=>new Promise(t=>setTimeout(t,e));function X(e){return`You are a professional software localization engine. Translate UI messages from ${e.sourceLocale} to ${e.targetLocale}. Preserve placeholders exactly. Messages of kind "mixed" are ICU MessageFormat: keep each {name} argument unchanged, keep apostrophes and braces valid ICU (a literal apostrophe is written as two apostrophes, and literal braces are quoted), and use plural or select only when the target language needs them. Respect context and UI conventions. Return ONLY a JSON object mapping message IDs to translated strings. Do not translate product names or code.`}function Z(e){return JSON.stringify(e.map(e=>({id:e.id,text:e.source,context:e.context,kind:e.kind})))}async function Ge(e){let t=e.status===408||e.status===409||e.status===429||e.status>=500;return await e.body?.cancel().catch(()=>void 0),new Y(`Translation API failed (${e.status})`,t,Ue(e.headers.get(`retry-after`)))}var Q=class{options;slots;constructor(e){this.options=e,this.slots=new fe(e.concurrency??4)}async translate(e){let t=Math.max(1,this.options.batchSize??40),n=[];for(let r=0;r<e.messages.length;r+=t)n.push(e.messages.slice(r,r+t));let r=await Promise.allSettled(n.map(t=>this.slots.run(()=>this.translateBatch({...e,messages:t})))),i={},a=[],o=0,s=0;if(r.forEach((e,t)=>{e.status===`fulfilled`?(s++,Object.assign(i,e.value)):(a.push(e.reason),o+=n[t].length)}),!a.length)return i;throw s?new F(`${o} messages failed to translate: ${a[0].message}`,i,o):a[0]}async translateBatch(e){let t=this.options.maxRetries??2,n=this.options.retryDelayMs??500;for(let r=0;;r++)try{return await this.request(e)}catch(e){if(e instanceof Y&&!e.retryable||r>=t)throw e;let i=e instanceof Y?e.retryAfterMs:void 0;await We(i??n*2**r*(.5+Math.random()/2))}}async post(e,t,n){let r=await fetch(e,{method:`POST`,signal:AbortSignal.timeout(this.options.timeoutMs??6e4),headers:{...t,"content-type":`application/json`},body:JSON.stringify(n)});if(!r.ok)throw await Ge(r);return r.json()}async complete(e,t,n,r,i,a){let o=r(await this.post(e,t,n));return this.toCatalog(o instanceof M?void 0:i(o),a.messages)}toCatalog(e,t){let n=e?.trim().replace(/^```(?:json)?\s*|\s*```$/g,``);if(!n)throw new Y(`Translation provider returned no content`,!0);let r;try{r=JSON.parse(n)}catch{throw new Y(`Translation provider returned invalid JSON`,!0)}let i=s(r);if(i instanceof M)throw new Y(`Translation provider returned a non-object`,!0);let a=new Set(t.map(e=>e.id)),o={};for(let[e,t]of Object.entries(i))a.has(e)&&typeof t==`string`&&t.trim()&&(o[e]=t);return o}},Ke=class extends Q{async request(e){let{apiKey:t,model:n,baseURL:r=h}=this.options;return this.complete(`${r}/chat/completions`,t?{Authorization:`Bearer ${t}`}:{},{model:n,temperature:0,response_format:{type:`json_object`},messages:[{role:`system`,content:X(e)},{role:`user`,content:Z(e.messages)}]},d,e=>e.choices[0].message.content??void 0,e)}},qe=class extends Q{async request(e){return this.complete(`${this.options.baseURL??`https://api.anthropic.com/v1`}/messages`,{"x-api-key":this.options.apiKey,"anthropic-version":w},{model:this.options.model,max_tokens:this.options.maxTokens??8192,system:X(e),messages:[{role:`user`,content:Z(e.messages)}]},l,e=>e.content.filter(e=>e.type===`text`).map(e=>e.text??``).join(``),e)}},Je=class extends Q{async request(e){return this.complete(`${this.options.baseURL??`https://generativelanguage.googleapis.com/v1beta`}/models/${encodeURIComponent(this.options.model)}:generateContent`,{"x-goog-api-key":this.options.apiKey},{systemInstruction:{parts:[{text:X(e)}]},contents:[{role:`user`,parts:[{text:Z(e.messages)}]}],generationConfig:{responseMimeType:`application/json`,maxOutputTokens:this.options.maxOutputTokens??8192}},a,e=>(e.candidates[0]?.content?.parts??[]).map(e=>e.text??``).join(``),e)}};const Ye=[`bg`,`cs`,`da`,`de`,`el`,`es`,`et`,`fi`,`fr`,`ga`,`hr`,`hu`,`it`,`lt`,`lv`,`mt`,`nl`,`pl`,`pt`,`ro`,`sk`,`sl`,`sv`],Xe=[`da`,`fi`,`is`,`no`,`sv`],Ze=[`et`,`lv`,`lt`],Qe=[`bg`,`bs`,`el`,`hr`,`mk`,`ro`,`sq`,`sr`,`sl`],$e=[`nl`,`fr`,`lb`],et=[.../* @__PURE__ */ new Set([...Ye,`no`,`is`,`sq`,`bs`,`mk`,`sr`,`uk`,`ru`,`be`,`ca`])],tt=[`zh`,`ja`,`ko`,`mn`],$=[`hi`,`bn`,`ur`,`pa`,`ta`,`te`,`mr`,`gu`,`kn`,`ml`,`ne`,`si`],nt=[`id`,`ms`,`my`,`km`,`lo`,`fil`,`th`,`vi`],rt=[`kk`,`ky`,`tg`,`tk`,`uz`],it=[`hy`,`az`,`ka`],at=[.../* @__PURE__ */ new Set([...tt,...$,...nt,...rt,...it])],ot=[`ar`,`fa`,`he`,`tr`,`ku`],st=[`ar`,`fr`,`pt`,`sw`,`am`,`ha`,`yo`,`ig`,`zu`,`xh`,`af`,`so`,`rw`,`wo`],ct=[`en`,`fr`,`es`],lt=[`es`,`en`,`fr`,`ht`,`nl`],ut=[`es`,`pt`,`qu`,`gn`,`ay`],dt={EU:Ye,EUROPE:et,NORDIC:Xe,BALTIC:Ze,BALKANS:Qe,BENELUX:$e,EAST_ASIA:tt,SOUTH_ASIA:$,SOUTHEAST_ASIA:nt,CENTRAL_ASIA:rt,CAUCASUS:it,ASIA:at,MIDDLE_EAST:ot,AFRICA:st,NORTH_AMERICA:ct,CENTRAL_AMERICA_CARIBBEAN:lt,SOUTH_AMERICA:ut,AMERICAS:[.../* @__PURE__ */ new Set([...ct,...lt,...ut])],OCEANIA:[`en`,`mi`,`sm`,`to`],ASEAN:[`id`,`ms`,`my`,`km`,`lo`,`fil`,`th`,`vi`,`en`],MERCOSUR:[`es`,`pt`,`gn`],GCC:[`ar`,`en`],CIS:[`ru`,`be`,`hy`,`az`,`kk`,`ky`,`tg`,`uz`],G7:[`en`,`fr`,`de`,`it`,`ja`],FRANCOPHONE:[`fr`,`ht`,`wo`,`ln`,`rw`],WORLDWIDE_POPULAR:[`en`,`zh`,`hi`,`es`,`ar`,`bn`,`pt`,`ru`,`ja`,`de`,`fr`,`ko`,`it`,`tr`,`vi`,`id`,`ur`]};function ft(e){let t=o(e);if(!(t instanceof M))return;let n=t.map(e=>`${e.path.join(`.`)||`options`}: ${e.message}`).join(`; `);throw Error(`autoi18n: invalid options (${n}).`)}const pt=e=>({info:t=>e.info(t),warn:t=>e.warn(t),error:t=>e.error(t)});async function mt(e,t){await Promise.all(t.map(t=>N(P.join(e,`${t}.sources.json`),{force:!0})))}function ht(t){ft(t);let n=t.sourceLocale??`en`,i=t.directory??`.autoi18n`,a=t.delivery??`build`,o=t.debug??!1,s={componentProps:t.componentProps,stringMarker:t.stringMarker},c=t.orphanRetentionMinutes??43200,l,u=He(t.provider,e=>l?.warn(e)),d,p=``,h=new Re,g,_=Promise.resolve(),v,y=()=>t.locale??process.env.AUTOI18N_LOCALE,C=()=>y()??n,w=()=>P.resolve(d.root,i,`locales`),T=t.include??te,E=t.exclude??x;function ee(){let e=t.locales.filter(e=>e!==n),r=y();return a===`build`&&r&&d.command!==`serve`?e.filter(e=>e===r):e}async function D(){let e=[];for await(let t of re(T,{cwd:d.root,exclude:E}))e.push(P.resolve(d.root,t));return e}async function O(e,t,n){let r=/* @__PURE__ */ new Map,i=/* @__PURE__ */ new Map,a=[];for(let o of e){let e=h.extractions.get(o),c;if(e&&t&&o!==n)c=e;else{let t=await ie(o,`utf8`),n=f(t);if(e?.hash===n)c=e;else{let e=L(t,o);c={hash:n,messages:je(t,o,s,e)},i.set(o,e)}}r.set(o,c),a.push({file:o,messages:c.messages})}return{nextExtractions:r,nextParsed:i,indexed:a}}function ne(e,n){for(let[r,i]of n.locales)i.requested&&!u&&t.failOnMissingProvider&&e.error(`Missing ${i.requested} ${r} translations and no provider is configured.`),i.rejected&&e.info(`autoi18n: rejected ${i.rejected} ${r} translations with changed placeholders.`),i.untranslated&&(u?e.warn:e.info)(`autoi18n: ${i.untranslated} ${r} messages have no translation and fall back to the source text.`),i.failed&&e.warn(`autoi18n: ${i.failed} ${r} translation batches failed (${i.failure}); they will be requested again on the next build.`)}async function k(r,i){l=r;let a=i!==void 0&&h.extractions.has(i),s=a?[...h.extractions.keys()]:await D(),{nextExtractions:d,nextParsed:f,indexed:m}=await O(s,a,i),g=w(),_=ee(),v=/* @__PURE__ */ new Map;for(let e of _)v.set(e,await h.readLocale(b(g,e),r.warn));let y=await be({files:m,registry:await h.readRegistry(p,r.warn),orphanRetentionMinutes:c,sourceLocale:n,locales:_,localeConcurrency:Math.max(1,Math.floor(t.localeConcurrency??2)),previous:v,provider:u,debug:o});if(o)for(let t of y.pruned)e(`removed ${t.id} ${JSON.stringify(t.source)} (last seen ${t.lastSeen})`);if(y.pruned.length&&r.info(`autoi18n: removed ${y.pruned.length} registry entries no longer in the source.`),await h.writeRegistry(p,y.registry),y.ambiguous.length){let e=`autoi18n: ${y.ambiguous.length} duplicated texts changed occurrence count, so their IDs may have moved (e.g. "${y.ambiguous[0]}").`;t.onAmbiguous===`error`?r.error(e):r.warn(e)}await h.writeLocale(b(g,n),y.sourceFile),h.legacySidecarsRemoved||=(await mt(g,[n,...t.locales]),!0);let x=/* @__PURE__ */ new Map([[n,y.sourceFile.messages]]);for(let[e,t]of y.locales)await h.writeLocale(b(g,e),t.file),x.set(e,t.file.messages);for(let e of y.failures.keys())x.set(e,v.get(e)?.messages??{});let S=[...x].filter(([e,t])=>!J(h.catalogs.get(e),t)).map(([e])=>e),C=0;h.fileMessages=/* @__PURE__ */ new Map;for(let{file:e,messages:t}of m)h.fileMessages.set(e,y.messages.slice(C,C+t.length)),C+=t.length;h.extractions=d,h.parsedSources=f,h.catalogs=x,h.catalogFiles=Object.fromEntries(t.locales.map(e=>[e,b(g,e)])),ne(r,y),y.messages.length&&r.info(`autoi18n: indexed ${y.messages.length} messages across ${s.length} files.`);let T=[...y.failures];for(let[e,t]of T.slice(1))r.warn(`autoi18n: ${e} translation failed: ${t.message}`);if(T.length)throw T[0][1];return S}function A(e,t){let n=_.then(()=>k(e,t));return _=n.then(()=>void 0,()=>void 0),n}function j(e){e.length&&g?.ws.send({type:`custom`,event:r,data:{catalogs:Object.fromEntries(e.map(e=>[e,h.catalogs.get(e)]))}})}async function M(e){try{j(await A(pt(d.logger),e))}catch(e){d.logger.error(`autoi18n: re-translation failed: ${e.message}`)}}function N(){clearTimeout(v),v=setTimeout(()=>void M(),150)}function ae(e){if(!/\.[jt]sx?$/.test(e))return!1;let t=P.relative(d.root,e).split(P.sep).join(`/`);return T.some(e=>P.matchesGlob(t,e))&&!E.some(e=>P.matchesGlob(t,e))}return{name:`vite-plugin-autoi18n`,enforce:`pre`,configResolved(e){d=e,p=P.resolve(d.root,i,`registry.json`);let r=C();if(r!==n&&!t.locales.includes(r))throw Error(`autoi18n: locale "${r}" is not one of the configured locales (${t.locales.join(`, `)}).`)},resolveId(e,t){if(e===`virtual:autoi18n`)return`\0${m}`;if(e===`virtual:autoi18n-format`)return`\0${S}`;if(t===`\0virtual:autoi18n-format`&&e===`intl-messageformat`)return this.resolve(e,oe(import.meta.url),{skipSelf:!0})},load(e){if(e===`\0virtual:autoi18n`)return Fe(h.catalogFiles,n);if(e===`\0virtual:autoi18n-format`)return Pe()},configureServer(e){g=e,e.watcher.on(`add`,e=>{ae(e)&&N()}),e.watcher.on(`unlink`,e=>{h.fileMessages.has(e)&&N()})},closeBundle(){clearTimeout(v)},async buildStart(){await k({info:e=>this.info(e),warn:e=>this.warn(e),error:e=>this.error(e)})},async transform(e,t){let r=t.split(`?`)[0];if(!h.fileMessages.has(r))return null;let i=f(e);i!==h.hashOf(r)&&await A(pt(d.logger),r);let o=h.fileMessages.get(r);if(!o||i!==h.hashOf(r))return h.warnedFiles.has(r)||(h.warnedFiles.add(r),this.warn(`autoi18n: ${r} changed while it was being indexed; its messages are left untranslated.`)),null;let s=d.command===`serve`?n:C(),c=h.parsedSources.get(r);h.parsedSources.delete(r);let l=Me(e,r,o,s,h.catalogs.get(s)??{},a===`runtime`,n,c);return{code:l.code,map:l.map}},async handleHotUpdate({file:e}){if(e.startsWith(w()+P.sep))return[];h.fileMessages.has(e)&&await M(e)}}}export{qe as AnthropicProvider,ze as FallbackProvider,Je as GeminiProvider,Ke as OpenAICompatibleProvider,F as PartialTranslationError,ht as default,dt as locales,He as resolveProvider};
|