autoglot 0.3.4 → 0.4.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 +13 -2
- package/bin/cli.mjs +7 -20
- package/bin/init.mjs +48 -31
- package/bin/templates/common.mjs +4 -0
- package/bin/templates/expo.mjs +4 -5
- package/bin/templates/react-native.mjs +4 -8
- package/bin/templates/vite-react.mjs +4 -5
- package/bin/templates/vue-i18n.mjs +4 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -36,13 +36,24 @@ npx autoglot init
|
|
|
36
36
|
|
|
37
37
|
Review the generated files, install the suggested i18n library, and add `ANTHROPIC_API_KEY` to your GitHub repository secrets. Edit `messages/en.json` and push your branch. For a local preview of the translation plan, run `npx autoglot --dry-run`.
|
|
38
38
|
|
|
39
|
+
### Non-interactive setup
|
|
40
|
+
|
|
41
|
+
Autoglot 0.4.0 adds unattended setup. From your app directory, pass a comma-separated list of target locales.
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npx --yes autoglot@0.4.0 init --locales=de,pt-BR
|
|
45
|
+
npx --yes autoglot@0.4.0 --dry-run
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Setup creates the missing files and skips paths that already exist. The dry run lists the keys to translate for `de` and `pt-BR` without an API key. Add `--install` to opt into installing the runtime libraries. If that install fails, unattended setup exits with an error after printing the retry command.
|
|
49
|
+
|
|
39
50
|
### Supported frameworks
|
|
40
51
|
|
|
41
52
|
| Framework | Runtime library | What `init` scaffolds |
|
|
42
53
|
|---|---|---|
|
|
43
|
-
| **Next.js (App Router)** | `next-intl` | `src/i18n/routing.ts`, `src/i18n/request.ts`, `src/middleware.ts`, plus a starter `src/app/[locale]/layout.tsx` + `page.tsx` if no `[locale]` segment exists |
|
|
54
|
+
| **Next.js (App Router)** | `next-intl` | `src/i18n/routing.ts`, `src/i18n/request.ts`, `src/middleware.ts`, plus a starter `src/app/[locale]/layout.tsx` + `page.tsx` if no `[locale]` segment exists. [Repository example](https://github.com/ffcabbar/autoglot/tree/main/examples/nextjs) |
|
|
44
55
|
| **Next.js (Pages Router)** | `next-i18next` | Messages + workflow only; prints precise wiring steps (next.config.js i18n block, `appWithTranslation` HOC, `serverSideTranslations` in pages) |
|
|
45
|
-
| **Expo** | `i18next` + `react-i18next` + `expo-localization` | `src/i18n/index.ts` with device-locale auto-detect on app start |
|
|
56
|
+
| **Expo** | `i18next` + `react-i18next` + `expo-localization` | `src/i18n/index.ts` with device-locale auto-detect on app start. [Android repository example](https://github.com/ffcabbar/autoglot/tree/main/examples/expo-android) |
|
|
46
57
|
| **React Native (bare)** | `i18next` + `react-i18next` | `src/i18n/index.ts` with all locales registered |
|
|
47
58
|
| **Vite + React** | `i18next` + `react-i18next` | `src/i18n.ts` |
|
|
48
59
|
| **Vue / Nuxt** | `vue-i18n` | `src/i18n.ts` (Composition API mode) |
|
package/bin/cli.mjs
CHANGED
|
@@ -15,7 +15,6 @@
|
|
|
15
15
|
|
|
16
16
|
import { readFileSync } from "node:fs";
|
|
17
17
|
|
|
18
|
-
const SUBCOMMANDS = new Set(["init", "add", "doctor", "extract", "help"]);
|
|
19
18
|
const argv = process.argv.slice(2);
|
|
20
19
|
const first = argv[0];
|
|
21
20
|
|
|
@@ -31,6 +30,7 @@ Usage
|
|
|
31
30
|
npx autoglot --help | -h Print help
|
|
32
31
|
|
|
33
32
|
init flags
|
|
33
|
+
--locales=de,fr Scaffold these target locales without prompts.
|
|
34
34
|
--install Install runtime i18n library after scaffolding.
|
|
35
35
|
--no-install Skip the interactive install prompt.
|
|
36
36
|
--pm=npm|yarn|pnpm|bun Override the auto-detected package manager.
|
|
@@ -76,35 +76,22 @@ if (first === "--help" || first === "-h" || first === "help") {
|
|
|
76
76
|
if (first === "init") {
|
|
77
77
|
const m = await import("./init.mjs");
|
|
78
78
|
await m.run(argv.slice(1));
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (first === "add") {
|
|
79
|
+
} else if (first === "add") {
|
|
83
80
|
const m = await import("./add.mjs");
|
|
84
81
|
await m.run(argv.slice(1));
|
|
85
82
|
process.exit(0);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (first === "doctor") {
|
|
83
|
+
} else if (first === "doctor") {
|
|
89
84
|
const m = await import("./doctor.mjs");
|
|
90
85
|
await m.run(argv.slice(1));
|
|
91
86
|
process.exit(0);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (first === "extract") {
|
|
87
|
+
} else if (first === "extract") {
|
|
95
88
|
const m = await import("./extract.mjs");
|
|
96
89
|
await m.run(argv.slice(1));
|
|
97
90
|
process.exit(process.exitCode ?? 0);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Unknown subcommand sanity check: if first arg looks like a subcommand
|
|
101
|
-
// (no leading dash, not a known one), warn the user instead of silently
|
|
102
|
-
// passing it through to the translator.
|
|
103
|
-
if (first && !first.startsWith("-") && !SUBCOMMANDS.has(first)) {
|
|
91
|
+
} else if (first && !first.startsWith("-")) {
|
|
104
92
|
process.stderr.write(`autoglot: unknown subcommand '${first}'.\n`);
|
|
105
93
|
process.stderr.write(`Run \`autoglot --help\` for the list of subcommands.\n`);
|
|
106
94
|
process.exit(2);
|
|
95
|
+
} else {
|
|
96
|
+
await import("./translate.mjs");
|
|
107
97
|
}
|
|
108
|
-
|
|
109
|
-
// Fall through to the translation engine.
|
|
110
|
-
await import("./translate.mjs");
|
package/bin/init.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
// `autoglot init` —
|
|
1
|
+
// `autoglot init` — scaffold a project with prompted or explicit target locales.
|
|
2
2
|
//
|
|
3
|
-
// Detects the framework
|
|
3
|
+
// Detects the framework and writes:
|
|
4
4
|
// - messages/en.json (with example keys)
|
|
5
5
|
// - messages/<locale>.json per chosen locale (empty)
|
|
6
6
|
// - translate.config.json
|
|
7
7
|
// - .github/workflows/translate.yml
|
|
8
8
|
// - framework-specific files when supported (e.g. next-intl scaffold)
|
|
9
9
|
//
|
|
10
|
-
//
|
|
10
|
+
// Skips existing files.
|
|
11
11
|
|
|
12
12
|
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync } from "node:fs";
|
|
13
13
|
import { join, dirname, resolve } from "node:path";
|
|
@@ -31,16 +31,18 @@ const TEMPLATES = {
|
|
|
31
31
|
"vite-react": viteReact,
|
|
32
32
|
"vue": vueI18n,
|
|
33
33
|
};
|
|
34
|
+
const LOCALE_CODE = /^[a-z]{2,3}(-[A-Z]{2})?$/;
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* Parse `init`-specific flags. Unknown flags are ignored (forward-compat).
|
|
37
38
|
*
|
|
39
|
+
* --locales=de,fr scaffold for explicit target locales without prompts
|
|
38
40
|
* --install opt-in: run the package manager to install runtime deps
|
|
39
41
|
* --pm=npm|yarn|pnpm|bun override the auto-detected package manager
|
|
40
42
|
* --no-install force off (useful in CI if a default ever flips)
|
|
41
43
|
*/
|
|
42
44
|
export function parseInitArgs(argv) {
|
|
43
|
-
const opts = { install: false, installExplicit: false, pm: null };
|
|
45
|
+
const opts = { install: false, installExplicit: false, pm: null, locales: null };
|
|
44
46
|
for (const arg of argv) {
|
|
45
47
|
if (arg === "--install") {
|
|
46
48
|
opts.install = true;
|
|
@@ -51,6 +53,9 @@ export function parseInitArgs(argv) {
|
|
|
51
53
|
} else if (arg.startsWith("--pm=")) {
|
|
52
54
|
const v = arg.slice("--pm=".length);
|
|
53
55
|
if (PACKAGE_MANAGERS.includes(v)) opts.pm = v;
|
|
56
|
+
} else if (arg === "--locales" || arg.startsWith("--locales=")) {
|
|
57
|
+
const values = arg.slice("--locales=".length).split(",");
|
|
58
|
+
opts.locales = [...new Set(values.map((value) => value.trim()))];
|
|
54
59
|
}
|
|
55
60
|
}
|
|
56
61
|
return opts;
|
|
@@ -58,6 +63,14 @@ export function parseInitArgs(argv) {
|
|
|
58
63
|
|
|
59
64
|
export async function run(argv = []) {
|
|
60
65
|
const opts = parseInitArgs(argv);
|
|
66
|
+
const defaultLocale = "en";
|
|
67
|
+
const nonInteractive = opts.locales !== null;
|
|
68
|
+
if (nonInteractive && opts.locales.some((locale) => !LOCALE_CODE.test(locale) || locale === defaultLocale)) {
|
|
69
|
+
process.stderr.write("autoglot init: expected target locale codes other than en, e.g. --locales=de,pt-BR.\n");
|
|
70
|
+
process.exitCode = 1;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
61
74
|
const cwd = process.cwd();
|
|
62
75
|
step("autoglot init");
|
|
63
76
|
info(`cwd: ${c.dim}${cwd}${c.reset}`);
|
|
@@ -87,33 +100,31 @@ export async function run(argv = []) {
|
|
|
87
100
|
|
|
88
101
|
// Pick locales
|
|
89
102
|
step("Locales");
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
localeChoices
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
+
let targetLocales = opts.locales;
|
|
104
|
+
if (!nonInteractive) {
|
|
105
|
+
const localeChoices = LOCALE_CATALOG.map((l) => ({
|
|
106
|
+
label: l.label,
|
|
107
|
+
value: l.code,
|
|
108
|
+
default: ["es", "de", "tr"].includes(l.code),
|
|
109
|
+
}));
|
|
110
|
+
targetLocales = await multiselect("Which target locales to translate into?", localeChoices);
|
|
111
|
+
if (targetLocales.length === 0) {
|
|
112
|
+
err("Need at least one target locale. Aborting.");
|
|
113
|
+
process.exitCode = 1;
|
|
114
|
+
closePrompts();
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
103
117
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
} else {
|
|
112
|
-
warn(`"${customLocale}" doesn't look like an ISO locale code. Skipping.`);
|
|
118
|
+
const customLocale = await ask("Add another locale not in the list? (ISO code, e.g. \"hi\")", "");
|
|
119
|
+
if (customLocale) {
|
|
120
|
+
if (LOCALE_CODE.test(customLocale)) {
|
|
121
|
+
targetLocales.push(customLocale);
|
|
122
|
+
} else {
|
|
123
|
+
warn(`"${customLocale}" doesn't look like an ISO locale code. Skipping.`);
|
|
124
|
+
}
|
|
113
125
|
}
|
|
114
126
|
}
|
|
115
127
|
|
|
116
|
-
const defaultLocale = "en";
|
|
117
128
|
ok(`Source: ${defaultLocale} → Targets: ${targetLocales.join(", ")}`);
|
|
118
129
|
|
|
119
130
|
// Note: we deliberately do NOT run `npm install` here. Auto-install of a
|
|
@@ -185,8 +196,7 @@ export async function run(argv = []) {
|
|
|
185
196
|
process.stdout.write(` ${status} ${f.path}\n`);
|
|
186
197
|
}
|
|
187
198
|
|
|
188
|
-
|
|
189
|
-
if (!proceed) {
|
|
199
|
+
if (!nonInteractive && !(await confirm("\nProceed?", true))) {
|
|
190
200
|
info("Aborted.");
|
|
191
201
|
closePrompts();
|
|
192
202
|
return;
|
|
@@ -203,7 +213,13 @@ export async function run(argv = []) {
|
|
|
203
213
|
continue;
|
|
204
214
|
}
|
|
205
215
|
mkdirSync(dirname(full), { recursive: true });
|
|
206
|
-
|
|
216
|
+
try {
|
|
217
|
+
writeFileSync(full, f.content, { flag: "wx" });
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error.code !== "EEXIST") throw error;
|
|
220
|
+
skipped++;
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
207
223
|
ok(f.path);
|
|
208
224
|
written++;
|
|
209
225
|
}
|
|
@@ -219,7 +235,7 @@ export async function run(argv = []) {
|
|
|
219
235
|
if (runtimeInstallHint) {
|
|
220
236
|
const pkgs = runtimeInstallHint.split(" ").filter(Boolean);
|
|
221
237
|
let shouldInstall = opts.install;
|
|
222
|
-
if (!opts.installExplicit) {
|
|
238
|
+
if (!nonInteractive && !opts.installExplicit) {
|
|
223
239
|
shouldInstall = await confirm(
|
|
224
240
|
`Install runtime packages now (${pkgs.join(", ")})?`,
|
|
225
241
|
false,
|
|
@@ -237,6 +253,7 @@ export async function run(argv = []) {
|
|
|
237
253
|
} else {
|
|
238
254
|
warn(`${pm} install exited with code ${res.status ?? "?"}. You can re-run manually.`);
|
|
239
255
|
installFailed = true;
|
|
256
|
+
if (nonInteractive) process.exitCode = 1;
|
|
240
257
|
}
|
|
241
258
|
}
|
|
242
259
|
}
|
package/bin/templates/common.mjs
CHANGED
package/bin/templates/expo.mjs
CHANGED
|
@@ -5,13 +5,15 @@
|
|
|
5
5
|
// We scaffold an i18n init that auto-picks the device locale and falls
|
|
6
6
|
// back to the configured default.
|
|
7
7
|
|
|
8
|
+
import { localeImportName } from "./common.mjs";
|
|
9
|
+
|
|
8
10
|
/** @param {{ defaultLocale: string, locales: string[] }} ctx */
|
|
9
11
|
export function indexTs(ctx) {
|
|
10
12
|
const imports = ctx.locales
|
|
11
|
-
.map((l) => `import ${
|
|
13
|
+
.map((l) => `import ${localeImportName(l)} from "../../messages/${l}.json";`)
|
|
12
14
|
.join("\n");
|
|
13
15
|
const resources = ctx.locales
|
|
14
|
-
.map((l) => ` ${quoteIfNeeded(l)}: { translation: ${
|
|
16
|
+
.map((l) => ` ${quoteIfNeeded(l)}: { translation: ${localeImportName(l)} },`)
|
|
15
17
|
.join("\n");
|
|
16
18
|
const localesArr = ctx.locales.map((l) => JSON.stringify(l)).join(", ");
|
|
17
19
|
|
|
@@ -58,9 +60,6 @@ export function followupNotes() {
|
|
|
58
60
|
];
|
|
59
61
|
}
|
|
60
62
|
|
|
61
|
-
function jsId(loc) {
|
|
62
|
-
return loc.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
63
|
-
}
|
|
64
63
|
function quoteIfNeeded(loc) {
|
|
65
64
|
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(loc) ? loc : JSON.stringify(loc);
|
|
66
65
|
}
|
|
@@ -7,13 +7,15 @@
|
|
|
7
7
|
// 3. Optionally wire device-locale detection with expo-localization
|
|
8
8
|
// or react-native-localize — we don't pick one for them.
|
|
9
9
|
|
|
10
|
+
import { localeImportName } from "./common.mjs";
|
|
11
|
+
|
|
10
12
|
/** @param {{ defaultLocale: string, locales: string[] }} ctx */
|
|
11
13
|
export function indexTs(ctx) {
|
|
12
14
|
const imports = ctx.locales
|
|
13
|
-
.map((l) => `import ${
|
|
15
|
+
.map((l) => `import ${localeImportName(l)} from "../../messages/${l}.json";`)
|
|
14
16
|
.join("\n");
|
|
15
17
|
const resources = ctx.locales
|
|
16
|
-
.map((l) => ` ${quoteIfNeeded(l)}: { translation: ${
|
|
18
|
+
.map((l) => ` ${quoteIfNeeded(l)}: { translation: ${localeImportName(l)} },`)
|
|
17
19
|
.join("\n");
|
|
18
20
|
|
|
19
21
|
return `import i18n from "i18next";
|
|
@@ -51,12 +53,6 @@ export function followupNotes() {
|
|
|
51
53
|
];
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
// Locale codes are usually safe ids (en, es, de, zh-CN), but zh-CN contains a
|
|
55
|
-
// dash which isn't a valid JS identifier. Use a sanitized name for the import
|
|
56
|
-
// binding and quote the property key on the way out.
|
|
57
|
-
function jsId(loc) {
|
|
58
|
-
return loc.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
59
|
-
}
|
|
60
56
|
function quoteIfNeeded(loc) {
|
|
61
57
|
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(loc) ? loc : JSON.stringify(loc);
|
|
62
58
|
}
|
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
// Generates src/i18n.ts with i18next configured. The user imports it
|
|
4
4
|
// once in src/main.tsx (before <App/> renders).
|
|
5
5
|
|
|
6
|
+
import { localeImportName } from "./common.mjs";
|
|
7
|
+
|
|
6
8
|
/** @param {{ defaultLocale: string, locales: string[] }} ctx */
|
|
7
9
|
export function i18nTs(ctx) {
|
|
8
10
|
const imports = ctx.locales
|
|
9
|
-
.map((l) => `import ${
|
|
11
|
+
.map((l) => `import ${localeImportName(l)} from "../messages/${l}.json";`)
|
|
10
12
|
.join("\n");
|
|
11
13
|
const resources = ctx.locales
|
|
12
|
-
.map((l) => ` ${quoteIfNeeded(l)}: { translation: ${
|
|
14
|
+
.map((l) => ` ${quoteIfNeeded(l)}: { translation: ${localeImportName(l)} },`)
|
|
13
15
|
.join("\n");
|
|
14
16
|
|
|
15
17
|
return `import i18n from "i18next";
|
|
@@ -44,9 +46,6 @@ export function followupNotes() {
|
|
|
44
46
|
];
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
function jsId(loc) {
|
|
48
|
-
return loc.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
49
|
-
}
|
|
50
49
|
function quoteIfNeeded(loc) {
|
|
51
50
|
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(loc) ? loc : JSON.stringify(loc);
|
|
52
51
|
}
|
|
@@ -3,13 +3,15 @@
|
|
|
3
3
|
// Generates src/i18n.ts using the vue-i18n composition API. The user
|
|
4
4
|
// installs the plugin in main.ts: app.use(i18n).
|
|
5
5
|
|
|
6
|
+
import { localeImportName } from "./common.mjs";
|
|
7
|
+
|
|
6
8
|
/** @param {{ defaultLocale: string, locales: string[] }} ctx */
|
|
7
9
|
export function i18nTs(ctx) {
|
|
8
10
|
const imports = ctx.locales
|
|
9
|
-
.map((l) => `import ${
|
|
11
|
+
.map((l) => `import ${localeImportName(l)} from "../messages/${l}.json";`)
|
|
10
12
|
.join("\n");
|
|
11
13
|
const messages = ctx.locales
|
|
12
|
-
.map((l) => ` ${quoteIfNeeded(l)}: ${
|
|
14
|
+
.map((l) => ` ${quoteIfNeeded(l)}: ${localeImportName(l)},`)
|
|
13
15
|
.join("\n");
|
|
14
16
|
|
|
15
17
|
return `import { createI18n } from "vue-i18n";
|
|
@@ -43,9 +45,6 @@ export function followupNotes() {
|
|
|
43
45
|
];
|
|
44
46
|
}
|
|
45
47
|
|
|
46
|
-
function jsId(loc) {
|
|
47
|
-
return loc.replace(/[^a-zA-Z0-9_$]/g, "_");
|
|
48
|
-
}
|
|
49
48
|
function quoteIfNeeded(loc) {
|
|
50
49
|
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(loc) ? loc : JSON.stringify(loc);
|
|
51
50
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "autoglot",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "AI-powered i18n translation bot. Diff your en.json on every push, translate only what changed, commit the JSON files back. MIT alternative to Languine, Lokalise, and Crowdin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|