@verbatra/sdk 0.1.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/LICENSE +21 -0
- package/README.md +116 -0
- package/dist/index.cjs +1805 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +414 -0
- package/dist/index.d.ts +414 -0
- package/dist/index.js +1773 -0
- package/dist/index.js.map +1 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mario Kreitz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
<h1 align="center">@verbatra/sdk</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">
|
|
4
|
+
Programmatic API to automate i18n translation and keep your locale files in sync across languages with AI and machine-translation providers.
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://github.com/mariokreitz/verbatra/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT" /></a>
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
## Description
|
|
12
|
+
|
|
13
|
+
`@verbatra/sdk` is the engine behind verbatra: load and validate a config, run the one-shot translate flow over every target locale, or watch the source and re-translate on each change. The [`@verbatra/cli`](https://github.com/mariokreitz/verbatra/tree/main/packages/cli) command is a thin wrapper over this package.
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
Node.js `>=22.14.0`.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm add -D @verbatra/sdk
|
|
23
|
+
# npm
|
|
24
|
+
npm install -D @verbatra/sdk
|
|
25
|
+
# yarn
|
|
26
|
+
yarn add -D @verbatra/sdk
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { loadConfig, translate } from "@verbatra/sdk";
|
|
33
|
+
|
|
34
|
+
// Discovers and validates verbatra.config.ts (or .verbatrarc.json, or a package.json "verbatra" key).
|
|
35
|
+
const config = await loadConfig();
|
|
36
|
+
|
|
37
|
+
// The provider reads its API key from the environment (e.g. ANTHROPIC_API_KEY). No key is passed.
|
|
38
|
+
const summary = await translate({ config });
|
|
39
|
+
|
|
40
|
+
console.log(`${summary.succeeded.length} locale(s) translated, ${summary.failed.length} failed`);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Defining config
|
|
44
|
+
|
|
45
|
+
`defineConfig` is an identity helper that gives you full type inference while authoring `verbatra.config.ts`:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { defineConfig } from "@verbatra/sdk";
|
|
49
|
+
|
|
50
|
+
export default defineConfig({
|
|
51
|
+
sourceLocale: "en",
|
|
52
|
+
targetLocales: ["de", "fr"],
|
|
53
|
+
format: "i18next-json",
|
|
54
|
+
files: {
|
|
55
|
+
pattern: "locales/{locale}.json",
|
|
56
|
+
},
|
|
57
|
+
provider: {
|
|
58
|
+
id: "anthropic",
|
|
59
|
+
options: {
|
|
60
|
+
model: "<your-model>", // replace with your provider's model id
|
|
61
|
+
maxTokens: 4096,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
`files.pattern` must contain the `{locale}` token, and `targetLocales` must not include `sourceLocale`; both are enforced when the config is validated. The supported `format` values are `i18next-json`, `vue-i18n-json`, `next-intl-json`, and `ngx-translate-json`. OpenAI and Gemini take `{ model, maxOutputTokens }`; DeepL takes `{}` (with an optional `glossaryId`). API keys are never part of the config. Each provider reads its own environment variable (`ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, `GEMINI_API_KEY`, `DEEPL_API_KEY`).
|
|
68
|
+
|
|
69
|
+
## API reference
|
|
70
|
+
|
|
71
|
+
### `defineConfig(config)`
|
|
72
|
+
|
|
73
|
+
Returns the config unchanged. It exists purely for type inference and editor autocomplete when authoring a code-defined config.
|
|
74
|
+
|
|
75
|
+
### `loadConfig(options?): Promise<VerbatraConfig>`
|
|
76
|
+
|
|
77
|
+
Discovers and validates the configuration. With no arguments it searches upward from the current working directory; `options` accepts `cwd`, an explicit `configPath`, or an in-memory `configOverride`. Resolves to the validated `VerbatraConfig`, and throws an `SdkError` if no config is found or it fails validation.
|
|
78
|
+
|
|
79
|
+
### `translate(input): Promise<RunSummary>`
|
|
80
|
+
|
|
81
|
+
Runs the one-shot read, diff, translate, write flow over every target locale. `input` is `{ config, cwd?, dryRun? }`. With `dryRun: true` it reads, diffs, and reports without calling the provider or writing anything. Resolves to a `RunSummary` (`dryRun`, `locales`, `succeeded`, and `failed`).
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const preview = await translate({ config, dryRun: true });
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### `watch(input): Promise<WatchController>`
|
|
88
|
+
|
|
89
|
+
Watches the source file and re-runs the translate flow on each debounced change. `input` is `{ config, cwd?, debounceMs?, onRun }`, where `onRun` receives a `WatchRunResult` per run. Resolves to a `WatchController` whose `stop()` closes the watcher and awaits the in-flight run.
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { loadConfig, watch } from "@verbatra/sdk";
|
|
93
|
+
|
|
94
|
+
const config = await loadConfig();
|
|
95
|
+
const controller = await watch({
|
|
96
|
+
config,
|
|
97
|
+
onRun: (result) => console.log(result.status),
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Stop cleanly on Ctrl-C.
|
|
101
|
+
process.on("SIGINT", () => void controller.stop());
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Errors and results
|
|
105
|
+
|
|
106
|
+
The SDK throws a single structured error type, `SdkError`, for whole-run failures such as a missing or invalid config or an unreadable source file. It carries a stable `code` and never contains an API key. Per-locale failures do not throw: they are recorded on the `RunSummary` so one failing locale never aborts the others.
|
|
107
|
+
|
|
108
|
+
## Documentation
|
|
109
|
+
|
|
110
|
+
- [Documentation site](https://verbatra.kreitz-webdev.de)
|
|
111
|
+
- [Project README](https://github.com/mariokreitz/verbatra)
|
|
112
|
+
- [`@verbatra/cli`](https://github.com/mariokreitz/verbatra/tree/main/packages/cli) for the command-line tool
|
|
113
|
+
|
|
114
|
+
## License
|
|
115
|
+
|
|
116
|
+
[MIT](https://github.com/mariokreitz/verbatra/blob/main/LICENSE) (c) Mario Kreitz
|