Skip to content

Commit

Permalink
Merge pull request #11 from midday-ai/fix/js-adapter
Browse files Browse the repository at this point in the history
Fix js adapter
  • Loading branch information
pontusab authored Dec 25, 2024
2 parents b06b375 + fbb419e commit 8b6a718
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 37 deletions.
Binary file modified bun.lockb
Binary file not shown.
Binary file modified examples/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions examples/next-international/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export default {
"scope.more.stars#other": "{count} stars on GitHub",
"missing.translation.in.fr": "This should work",
"cows#one": "A cow",
"cows#other": "{count} cows",
} as const;
4 changes: 3 additions & 1 deletion examples/next-international/locales/fr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
hello: "Bonjour",
welcome: "Bonjour {name} !",
"about.you": "Bonjour {name} ! Vous avez {age} ans",
"about.you": "Bonjour {name} ! Tu as {age} ans",
"scope.test": "Un domaine",
"scope.more.test": "Un domaine",
"scope.more.param": "Un domaine avec {param}",
Expand All @@ -9,4 +10,5 @@ export default {
"scope.more.stars#other": "{count} étoiles sur GitHub",
"missing.translation.in.fr": "Cela devrait fonctionner",
"cows#one": "Une vache",
"cows#other": "{count} vaches",
} as const;
6 changes: 3 additions & 3 deletions examples/next-intl/messages/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"HomePage": {
"title": "Startseite",
"param": "Hallo {name}",
"plural": "Sie haben {count, plural, =0 {noch keine Follower} =1 {einen Follower} other {# Follower}}.",
"plural": "Du hast {count, plural, =0 {noch keine Follower} =1 {einen Follower} other {# Follower}}.",
"select": "{gender, select, female {Sie} male {Er} other {Sie}} ist online.",
"escaped": "Geschweifte Klammern mit einfachen Anführungszeichen escapen (z. B. '{name'})",
"rich": "Bitte beachten Sie <guidelines>die Richtlinien</guidelines>.",
"escaped": "Geschweifte Klammern mit einfachen Anführungszeichen escapen (z.B. '{name}')",
"rich": "Bitte beachten Sie die <guidelines>Richtlinien</guidelines>.",
"number": "Verfügbar ab {price, number, currency}",
"date": "Bestellt am {orderDate, date, medium}"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@languine/example",
"version": "1.0.0",
"dependencies": {
"@languine/cli": "link:@languine/cli"
"languine": "link:languine"
},
"devDependencies": {
"@biomejs/js-api": "^0.7.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "languine",
"version": "0.5.2",
"version": "0.5.4",
"type": "module",
"bin": "dist/index.js",
"main": "dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export async function diff() {
process.exit(0);
}

let added = 0,
removed = 0;
let added = 0;
let removed = 0;

for (const line of diff.split("\n")) {
if (line.startsWith("+") && !line.startsWith("+++")) added++;
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/commands/translate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { execSync } from "node:child_process";
import fs from "node:fs/promises";
import path from "node:path";
import { createOpenAI } from "@ai-sdk/openai";
import { intro, outro, spinner } from "@clack/prompts";
import chalk from "chalk";
import { getApiKey, getConfig } from "../utils.js";
import { simpleGit } from "simple-git";
import { getTranslator } from "../translators/index.js";
import type { PromptOptions, UpdateResult } from "../types.js";
import { simpleGit } from "simple-git";
import { getApiKey, getConfig } from "../utils.js";

export async function translate(targetLocale?: string, force: boolean = false) {
export async function translate(targetLocale?: string, force = false) {
intro("Starting translation process...");

const config = await getConfig();
Expand All @@ -33,6 +32,7 @@ export async function translate(targetLocale?: string, force: boolean = false) {
const openai = createOpenAI({
apiKey: await getApiKey("OpenAI", "OPENAI_API_KEY"),
});

const model = openai(config.openai.model);

const s = spinner();
Expand Down
21 changes: 11 additions & 10 deletions packages/cli/src/translators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import { markdown } from "./md.js";
export async function getTranslator(
format: string,
): Promise<Translator | undefined> {
if (format === "ts" || format === "js") {
return javascript;
}

if (format === "json") {
return json;
}

if (format === "md" || format === "mdx") {
return markdown;
switch (format) {
case "ts":
case "js":
return javascript;
case "json":
return json;
case "md":
case "mdx":
return markdown;
default:
return undefined;
}
}
25 changes: 15 additions & 10 deletions packages/cli/src/translators/js.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { generateObject } from "ai";
import { baseRequirements, createBasePrompt } from "../prompt.js";
import type { PromptOptions, Translator } from "../types.js";
import dedent from "dedent";
import { diffLines } from "diff";
import { z } from "zod";
import dedent from "dedent";
import { baseRequirements, createBasePrompt } from "../prompt.js";
import type { PromptOptions, Translator } from "../types.js";

function createRegex(quote: string, multiline = false) {
return `${quote}(?:\\\\.|[^${quote}\\\\${multiline ? "" : "\\n"}])*${quote}`;
}

const quotesRegex = new RegExp(
`${createRegex(`"`)}|${createRegex(`'`)}|${createRegex(`\``, true)}`,
`${createRegex(`"`)}|${createRegex(`'`)}|${createRegex("`", true)}`,
"g",
);

Expand Down Expand Up @@ -74,7 +74,7 @@ export const javascript: Translator = {
const toTranslate: StringMatch[] = [];

let lineStartIdx = 0;
diff.forEach((change) => {
for (const change of diff) {
if (change.added) {
const affected = strings.filter(
(v) =>
Expand All @@ -88,18 +88,20 @@ export const javascript: Translator = {
if (!change.removed) {
lineStartIdx += change.value.length;
}
});
}

let translated: string[] = [];

if (toTranslate.length > 0) {
const { object } = await generateObject({
model: options.model,
prompt: getPrompt(toTranslate, options),
schema: z.array(z.string()),
schema: z.object({
translations: z.array(z.string()),
}),
});

translated = object;
translated = object.translations;
}

const output = replaceStrings(
Expand Down Expand Up @@ -127,11 +129,13 @@ export const javascript: Translator = {
const { object } = await generateObject({
model: options.model,
prompt: getPrompt(strings, options),
schema: z.array(z.string()),
schema: z.object({
translations: z.array(z.string()),
}),
});

return {
content: replaceStrings(options.content, strings, object),
content: replaceStrings(options.content, strings, object.translations),
};
},
};
Expand All @@ -141,6 +145,7 @@ function getPrompt(strings: StringMatch[], options: PromptOptions) {
${baseRequirements}
- Preserve all object/property keys, syntax characters, and punctuation marks exactly
- Only translate text content within quotation marks
- Don't remove the quotes around the keys and values
A list of javascript codeblocks, return the translated javascript string in a JSON array of string:`;
const codeblocks = strings
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/translators/json.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { generateObject } from "ai";
import dedent from "dedent";
import { baseRequirements, createBasePrompt } from "../prompt.js";
import type { PromptOptions, Translator } from "../types.js";
import dedent from "dedent";

export const json: Translator = {
async onUpdate(options) {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/translators/md.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { PromptOptions, Translator } from "../types.js";
import { baseRequirements, createBasePrompt } from "../prompt.js";
import { generateObject, generateText } from "ai";
import { z } from "zod";
import { diffLines } from "diff";
import dedent from "dedent";
import { diffLines } from "diff";
import { z } from "zod";
import { baseRequirements, createBasePrompt } from "../prompt.js";
import type { PromptOptions, Translator } from "../types.js";

function extractDiff(pervious: string, content: string) {
const contentLines = content.split("\n");
Expand Down

0 comments on commit 8b6a718

Please sign in to comment.