Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ jobs:
- uses: oven-sh/setup-bun@v2
- run: bun install --frozen-lockfile
- run: bun run build
# Smoke-test the CLI binary under both runtimes — catches shebang /
# bundling regressions that make the published bin unrunnable.
- run: node dist/cli.js --help
- run: bun dist/cli.js --help
- run: bun test
- run: bunx tsc --noEmit
4 changes: 3 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"peerDependencies": {
"solid-js": ">=1.7.0",
"vite": ">=4.0.0",
"ai": ">=3.0.0"
"ai": ">=3.0.0 <5.0.0"
},
"peerDependenciesMeta": {
"vite": {
Expand Down
122 changes: 30 additions & 92 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env node
// NOTE: no shebang here — tsup adds it via `banner` in tsup.config.ts.
// Having both produces a dist/cli.js with two shebang lines, which is a
// syntax error under node and bun (the published binary cannot run).

import {
readFileSync,
Expand All @@ -7,10 +9,10 @@ import {
mkdirSync,
} from "node:fs";
import { resolve, join, dirname, relative, basename } from "node:path";
import { hashContent } from "./hash.js";
import { translateBatch, translateMarkdown } from "./translate.js";
import { extractStringsFromSource } from "./extract.js";
import type { CLIConfig, LockFile } from "./types.js";
import { syncLocaleFiles, formatSyncFailures } from "./lock.js";
import type { CLIConfig } from "./types.js";

const CONFIG_FILENAMES = [
"solid-translate.config.json",
Expand Down Expand Up @@ -360,104 +362,40 @@ async function translateLocaleFiles(
batchSize: number,
systemPrompt?: string,
) {
const sourceFilePath = join(localesDir, `${sourceLocale}.json`);
if (!existsSync(sourceFilePath)) {
const result = await syncLocaleFiles({
localesDir,
sourceLocale,
targetLocales,
batchSize,
translate: (batch, targetLocale, contexts) =>
translateBatch(
model,
batch,
targetLocale,
sourceLocale,
systemPrompt,
contexts,
),
log: (message) => console.log(message),
});

if (result.status === "no-source") {
console.log(
"No source locale file found. Run `solid-translate extract` first.",
);
return;
}

const sourceDict: Record<string, string> = JSON.parse(
readFileSync(sourceFilePath, "utf-8"),
);

// Read lock file
const lockFilePath = join(localesDir, ".solid-translate.lock");
let lock: LockFile = { version: 1, sourceLocale, keys: {} };
if (existsSync(lockFilePath)) {
try {
lock = JSON.parse(readFileSync(lockFilePath, "utf-8"));
} catch {
// start fresh
}
}

// Find changed keys
const changedKeys: Record<string, string> = {};
for (const [key, value] of Object.entries(sourceDict)) {
const hash = hashContent(value);
const existing = lock.keys[key];
if (!existing || existing.hash !== hash) {
changedKeys[key] = value;
lock.keys[key] = { hash, source: value };
}
}

// Remove deleted keys
for (const key of Object.keys(lock.keys)) {
if (!(key in sourceDict)) {
delete lock.keys[key];
}
}

if (Object.keys(changedKeys).length === 0) {
console.log("No changes detected in locale files.");
return;
}

const count = Object.keys(changedKeys).length;
console.log(
`Translating ${count} key${count > 1 ? "s" : ""} to ${targetLocales.length} locale${targetLocales.length > 1 ? "s" : ""}...`,
);

for (const targetLocale of targetLocales) {
const targetFilePath = join(localesDir, `${targetLocale}.json`);

let existing: Record<string, string> = {};
if (existsSync(targetFilePath)) {
try {
existing = JSON.parse(readFileSync(targetFilePath, "utf-8"));
} catch {
// regenerate
}
if (result.failures.length > 0) {
console.error("\nTranslation failed for some batches:");
for (const line of formatSyncFailures(result.failures)) {
console.error(` ${line}`);
}

const entries = Object.entries(changedKeys);
for (let i = 0; i < entries.length; i += batchSize) {
const batch = Object.fromEntries(entries.slice(i, i + batchSize));
try {
const translated = await translateBatch(
model,
batch,
targetLocale,
sourceLocale,
systemPrompt,
);
Object.assign(existing, translated);
} catch (err) {
console.error(
`Failed to translate batch for ${targetLocale}:`,
err,
);
}
}

// Remove deleted keys
for (const key of Object.keys(existing)) {
if (!(key in sourceDict)) {
delete existing[key];
}
}

const sorted = Object.fromEntries(
Object.entries(existing).sort(([a], [b]) => a.localeCompare(b)),
console.error(
"Failed keys were not recorded in the lock file — fix the error and rerun `solid-translate translate` to retry them.",
);
writeFileSync(targetFilePath, JSON.stringify(sorted, null, 2) + "\n");
console.log(` ${targetLocale}: ${Object.keys(sorted).length} keys`);
process.exit(1);
}

writeFileSync(lockFilePath, JSON.stringify(lock, null, 2) + "\n");
}

main().catch((err) => {
Expand Down
19 changes: 14 additions & 5 deletions src/locale-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@ export function detectLocale(availableLocales?: string[]): string {
return normalizeLocale(browserLocales[0] || "en");
}

// Exact match
// Map normalized available locales back to their canonical casing so a
// browser "pt-br" can match an available "pt-BR" (and return "pt-BR").
const canonical = new Map<string, string>();
for (const al of availableLocales) {
const normalized = normalizeLocale(al);
if (!canonical.has(normalized)) canonical.set(normalized, al);
}

// Exact match (case-insensitive)
for (const bl of browserLocales) {
const normalized = normalizeLocale(bl);
if (availableLocales.includes(normalized)) return normalized;
const match = canonical.get(normalizeLocale(bl));
if (match) return match;
}

// Language-only match (e.g. "en-US" → "en")
for (const bl of browserLocales) {
const lang = bl.split("-")[0]!.toLowerCase();
if (availableLocales.includes(lang)) return lang;
const lang = normalizeLocale(bl).split("-")[0]!;
const match = canonical.get(lang);
if (match) return match;
}

return availableLocales[0] || "en";
Expand Down
Loading
Loading