@verbaly/compiler 0.32.0 → 0.33.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 +56 -5
- package/dist/claude-CrAkiSVP.js +53 -0
- package/dist/claude-CrAkiSVP.js.map +1 -0
- package/dist/cli.js.map +1 -1
- package/dist/config-D8gkaunO.js +68 -0
- package/dist/config-D8gkaunO.js.map +1 -0
- package/dist/index.d.ts +92 -275
- package/dist/index.js +306 -1694
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -20,7 +20,7 @@ Extraction covers `.js`/`.ts`/`.jsx`/`.tsx` **and `.svelte`, `.vue` and `.astro`
|
|
|
20
20
|
## 🧰 CLI
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
npx verbaly init # scaffold config + locale catalogs (detects your
|
|
23
|
+
npx verbaly init # scaffold config + locale catalogs (detects your framework)
|
|
24
24
|
npx verbaly doctor # diagnose the setup (config, catalogs, wiring, types, translations)
|
|
25
25
|
npx verbaly wrap # onboarding codemod: report plain JSX text, --write wraps it in t``
|
|
26
26
|
npx verbaly extract # sync catalogs + types
|
|
@@ -69,12 +69,17 @@ Hand-edited catalogs get the same treatment as imported files: the gate does not
|
|
|
69
69
|
|
|
70
70
|
**Machine output is a draft until a human says yes.** Everything `translate` writes is recorded in `locales/.verbaly-drafts.json` (commit it, never edit it by hand). `verbaly review` lists the drafts, `--approve` accepts them, and importing a translator's file clears the flag because a human already reviewed it. `verbaly check --drafts` turns "nothing unreviewed ships" into a CI rule; plain `check` leaves it alone, since a draft has a value and is therefore not missing.
|
|
71
71
|
|
|
72
|
-
Plug your own provider in `verbaly.config.ts
|
|
72
|
+
Plug your own provider in `verbaly.config.ts`. In TypeScript, `TranslateProvider` types it for you:
|
|
73
73
|
|
|
74
74
|
```ts
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
75
|
+
import type { TranslateProvider } from '@verbaly/compiler';
|
|
76
|
+
|
|
77
|
+
const provider: TranslateProvider = async ({ sourceLocale, targetLocale, messages, origins }) => {
|
|
78
|
+
// origins maps a key to the source files it appears in, so you can translate with context
|
|
79
|
+
return { ...translated };
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export default { sourceLocale: 'en', locales: ['en', 'es'], translate: { provider } };
|
|
78
83
|
```
|
|
79
84
|
|
|
80
85
|
## 🌍 Human translators & TMS
|
|
@@ -124,6 +129,52 @@ Per-element `data-verbaly-links='{"repo":"https://…"}'` merges over the config
|
|
|
124
129
|
|
|
125
130
|
> ⚠️ Early development (`0.x`): API not stable yet.
|
|
126
131
|
|
|
132
|
+
## 🧩 Programmatic API
|
|
133
|
+
|
|
134
|
+
Almost nobody needs this: the CLI and the framework plugins cover the whole cycle. It exists for the one case they do not, building your own integration for a bundler or a tool we do not ship.
|
|
135
|
+
|
|
136
|
+
The package exports two layers, and **nothing else is public**. Anything you can see in the source but not in this list is internal and can change in any release.
|
|
137
|
+
|
|
138
|
+
**Your project's own types**, for a typed config file or a custom provider:
|
|
139
|
+
|
|
140
|
+
`VerbalyConfig` · `ResolvedConfig` · `RenderConfig` · `TranslateConfig` · `TranslateProvider` · `TranslateRequest` · `TranslateResult` · `TranslateOptions`
|
|
141
|
+
|
|
142
|
+
**Building an integration.** This is exactly what `@verbaly/vite`, `@verbaly/unplugin`, `@verbaly/next`, `@verbaly/astro`, `@verbaly/nuxt` and `@verbaly/mcp` consume, so a third one has everything they have:
|
|
143
|
+
|
|
144
|
+
| Area | Exports |
|
|
145
|
+
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
146
|
+
| Config & catalogs | `loadConfig` `resolveConfig` `loadCatalogs` `writeCatalog` · types `Catalog` `Catalogs` |
|
|
147
|
+
| Extraction | `extractProject` `collectOrigins` `syncCatalogs` `pruneCatalogs` `MessageRegistry` `stableKey` · type `SyncResult` |
|
|
148
|
+
| Codegen | `generateDts` `writeDts` `generateRuntimeModule` `generateLocaleModule` · type `RuntimeModuleOptions` |
|
|
149
|
+
| Bundler plumbing | `transformSource` `transformCode` `runBuildGate` `createSourceFilter` `isTransformTarget` `resolveVirtualId` `loadVirtualModule` `RESOLVED_VIRTUAL_ID` `LOCALE_MODULE_PREFIX` `SOURCE_FILE_RE` · types `PluginOptions` `TransformResult` |
|
|
150
|
+
| The gate | `check` `formatCheckResult` `formatCheckWarnings` · types `CheckResult` `MissingEntry` `UnknownEntry` `BrokenEntry` `IssueSeverity` |
|
|
151
|
+
| Coverage | `status` `formatStatusResult` · types `StatusResult` `LocaleStatus` |
|
|
152
|
+
| Draft review | `loadDrafts` `saveDrafts` `markDrafts` `effectiveDrafts` · type `Drafts` |
|
|
153
|
+
| Translation | `translateCatalogs` `resolveProvider` |
|
|
154
|
+
| Static rendering | `renderSite` · types `RenderSiteOptions` `RenderSiteResult` |
|
|
155
|
+
|
|
156
|
+
A minimal plugin is `loadConfig` + `loadCatalogs` once, then `transformSource` per file and `runBuildGate` at the end:
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
import {
|
|
160
|
+
MessageRegistry,
|
|
161
|
+
loadCatalogs,
|
|
162
|
+
loadConfig,
|
|
163
|
+
runBuildGate,
|
|
164
|
+
transformSource,
|
|
165
|
+
} from '@verbaly/compiler';
|
|
166
|
+
|
|
167
|
+
const cfg = await loadConfig(process.cwd());
|
|
168
|
+
const catalogs = loadCatalogs(cfg);
|
|
169
|
+
const registry = new MessageRegistry();
|
|
170
|
+
|
|
171
|
+
// per file: rewrites t`…` to a keyed call and hands you the messages it found
|
|
172
|
+
const { messages, result } = transformSource(code, id, registry);
|
|
173
|
+
|
|
174
|
+
// at the end of the build: throws with the reason and the remedy
|
|
175
|
+
runBuildGate(cfg, registry);
|
|
176
|
+
```
|
|
177
|
+
|
|
127
178
|
## License
|
|
128
179
|
|
|
129
180
|
[MIT](https://github.com/AronSoto/verbaly/blob/develop/LICENSE) © Aron Soto
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { t as isModuleNotFound } from "./config-D8gkaunO.js";
|
|
2
|
+
//#region src/providers/claude.ts
|
|
3
|
+
const DEFAULT_MODEL = "claude-sonnet-5";
|
|
4
|
+
const SYSTEM = `You translate UI strings for the Verbaly i18n library.
|
|
5
|
+
Rules:
|
|
6
|
+
- Translate only the human-readable text, naturally for the target locale.
|
|
7
|
+
- Preserve verbatim: placeholders like {name}, format specs like {price:currency/EUR}, variant blocks like {count | one: ... | other: # ...} (translate only the text inside each variant, keep keys and # as-is), ICU syntax, named tags like <em>...</em> and escapes {{ }} || ##.
|
|
8
|
+
- Keys are opaque identifiers: return exactly the same keys, never translate or rename them.`;
|
|
9
|
+
function claudeProvider(options = {}) {
|
|
10
|
+
return async (request) => {
|
|
11
|
+
const text = (await new (await (loadSdk()))(options.apiKey ? { apiKey: options.apiKey } : {}).messages.create({
|
|
12
|
+
model: options.model ?? DEFAULT_MODEL,
|
|
13
|
+
max_tokens: options.maxTokens ?? 16e3,
|
|
14
|
+
thinking: { type: "disabled" },
|
|
15
|
+
system: SYSTEM,
|
|
16
|
+
messages: [{
|
|
17
|
+
role: "user",
|
|
18
|
+
content: buildPrompt(request)
|
|
19
|
+
}],
|
|
20
|
+
output_config: { format: batchFormat(request) }
|
|
21
|
+
})).content.find((block) => block.type === "text")?.text ?? "{}";
|
|
22
|
+
return JSON.parse(text);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function buildPrompt(request) {
|
|
26
|
+
const origins = request.origins;
|
|
27
|
+
const context = origins && Object.keys(origins).length ? `\n\nWhere each string appears (context for tone and length, do not translate these paths):\n` + Object.entries(origins).map(([key, files]) => ` ${key}: ${files.join(", ")}`).join("\n") : "";
|
|
28
|
+
return `Translate each value from "${request.sourceLocale}" to "${request.targetLocale}". Return a JSON object with the same keys and translated values.\n\n` + JSON.stringify(request.messages, null, 2) + context;
|
|
29
|
+
}
|
|
30
|
+
function batchFormat(request) {
|
|
31
|
+
const keys = Object.keys(request.messages);
|
|
32
|
+
return {
|
|
33
|
+
type: "json_schema",
|
|
34
|
+
schema: {
|
|
35
|
+
type: "object",
|
|
36
|
+
properties: Object.fromEntries(keys.map((key) => [key, { type: "string" }])),
|
|
37
|
+
required: keys,
|
|
38
|
+
additionalProperties: false
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function loadSdk(load = () => import("@anthropic-ai/sdk")) {
|
|
43
|
+
try {
|
|
44
|
+
return (await load()).default;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (isModuleNotFound(error, "@anthropic-ai/sdk")) throw new Error("[verbaly] the claude translate provider needs @anthropic-ai/sdk: install it as a dev dependency (e.g. `pnpm add -D @anthropic-ai/sdk` / `npm i -D @anthropic-ai/sdk`) and set ANTHROPIC_API_KEY", { cause: error });
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
export { claudeProvider };
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=claude-CrAkiSVP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"claude-CrAkiSVP.js","names":[],"sources":["../src/providers/claude.ts"],"sourcesContent":["import { isModuleNotFound } from '../config';\nimport type { TranslateProvider, TranslateRequest } from '../translate';\n\nexport interface ClaudeProviderOptions {\n model?: string;\n apiKey?: string;\n maxTokens?: number;\n}\n\n// balanced default for translation\nconst DEFAULT_MODEL = 'claude-sonnet-5';\n\nconst SYSTEM = `You translate UI strings for the Verbaly i18n library.\nRules:\n- Translate only the human-readable text, naturally for the target locale.\n- Preserve verbatim: placeholders like {name}, format specs like {price:currency/EUR}, variant blocks like {count | one: ... | other: # ...} (translate only the text inside each variant, keep keys and # as-is), ICU syntax, named tags like <em>...</em> and escapes {{ }} || ##.\n- Keys are opaque identifiers: return exactly the same keys, never translate or rename them.`;\n\nexport function claudeProvider(options: ClaudeProviderOptions = {}): TranslateProvider {\n return async (request: TranslateRequest) => {\n const Anthropic = await loadSdk();\n const client = new Anthropic(options.apiKey ? { apiKey: options.apiKey } : {});\n const response = await client.messages.create({\n model: options.model ?? DEFAULT_MODEL,\n max_tokens: options.maxTokens ?? 16000,\n thinking: { type: 'disabled' },\n system: SYSTEM,\n messages: [{ role: 'user', content: buildPrompt(request) }],\n output_config: { format: batchFormat(request) },\n });\n const text = response.content.find((block) => block.type === 'text')?.text ?? '{}';\n return JSON.parse(text) as Record<string, string>;\n };\n}\n\nexport function buildPrompt(request: TranslateRequest): string {\n const origins = request.origins;\n const context =\n origins && Object.keys(origins).length\n ? `\\n\\nWhere each string appears (context for tone and length, do not translate these paths):\\n` +\n Object.entries(origins)\n .map(([key, files]) => ` ${key}: ${files.join(', ')}`)\n .join('\\n')\n : '';\n return (\n `Translate each value from \"${request.sourceLocale}\" to \"${request.targetLocale}\". ` +\n `Return a JSON object with the same keys and translated values.\\n\\n` +\n JSON.stringify(request.messages, null, 2) +\n context\n );\n}\n\nexport function batchFormat(request: TranslateRequest) {\n const keys = Object.keys(request.messages);\n return {\n type: 'json_schema' as const,\n schema: {\n type: 'object',\n properties: Object.fromEntries(keys.map((key) => [key, { type: 'string' }])),\n required: keys,\n additionalProperties: false,\n },\n };\n}\n\ntype SdkModule = { default: typeof import('@anthropic-ai/sdk').default };\n\n// injectable for tests\nexport async function loadSdk(\n load: () => Promise<SdkModule> = () => import('@anthropic-ai/sdk'),\n): Promise<SdkModule['default']> {\n try {\n const mod = await load();\n return mod.default;\n } catch (error) {\n if (isModuleNotFound(error, '@anthropic-ai/sdk')) {\n throw new Error(\n '[verbaly] the claude translate provider needs @anthropic-ai/sdk: install it as a dev dependency (e.g. `pnpm add -D @anthropic-ai/sdk` / `npm i -D @anthropic-ai/sdk`) and set ANTHROPIC_API_KEY',\n { cause: error },\n );\n }\n throw error;\n }\n}\n"],"mappings":";;AAUA,MAAM,gBAAgB;AAEtB,MAAM,SAAS;;;;;AAMf,SAAgB,eAAe,UAAiC,CAAC,GAAs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