Skip to content

Commit

Permalink
Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Jan 19, 2025
1 parent 7da20f1 commit 9c2db8c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 34 deletions.
4 changes: 3 additions & 1 deletion apps/web/.env-example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
OPENAI_API_KEY=
ENCRYPTION_SECRET=
TRIGGER_SECRET_KEY=
TRIGGER_SECRET_KEY=
AI_GATEWAY_ENDPOINT=
AI_MODEL=
8 changes: 6 additions & 2 deletions apps/web/src/jobs/utils/translate.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { openai } from "@ai-sdk/openai";
import { createOpenAI } from "@ai-sdk/openai";
import { generateObject } from "ai";
import { z } from "zod";
import { createFinalPrompt } from "./prompt";
import type { PromptOptions } from "./types";

const openai = createOpenAI({
baseURL: process.env.AI_GATEWAY_ENDPOINT,
});

function getPrompt(
content: Array<{ key: string; sourceText: string }>,
options: PromptOptions,
Expand All @@ -26,7 +30,7 @@ export async function translate(
const prompt = getPrompt(content, options);

const { object } = await generateObject({
model: openai("gpt-4o-mini"),
model: openai(process.env.AI_MODEL!),
prompt,
mode: "json",
schema: z.object({
Expand Down
56 changes: 47 additions & 9 deletions apps/web/src/trpc/routers/jobs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { db } from "@/db";
import { organizations } from "@/db/schema";
import type { translateTask } from "@/jobs/translate/translate";
import { tasks } from "@trigger.dev/sdk/v3";
import { eq } from "drizzle-orm";
import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "../init";
import { rateLimitMiddleware } from "../middlewares/ratelimits";
Expand All @@ -23,15 +26,50 @@ export const jobsRouter = createTRPCRouter({
)
.use(rateLimitMiddleware)
.mutation(async ({ input }) => {
const run = await tasks.trigger<typeof translateTask>("translate", {
apiKey: input.apiKey,
projectId: input.projectId,
sourceFormat: input.sourceFormat,
sourceLanguage: input.sourceLanguage,
targetLanguages: input.targetLanguages,
content: input.content,
});
const org = await db
.select()
.from(organizations)
.where(eq(organizations.id, input.projectId))
.get();

return run;
const isFreeUser = org?.plan === "free";

const options = isFreeUser
? {
queue: {
name: "free-users",
concurrencyLimit: 1,
},
// Free users get a concurrency key of "free-users" to share between each other
concurrencyKey: "free-users",
}
: {
queue: {
name: "paid-users",
concurrencyLimit: 5,
},
// Paid users get a concurrency key of their organization id with a concurrency limit of 5
concurrencyKey: org?.id,
};

const run = await tasks.trigger<typeof translateTask>(
"translate",
{
apiKey: input.apiKey,
projectId: input.projectId,
sourceFormat: input.sourceFormat,
sourceLanguage: input.sourceLanguage,
targetLanguages: input.targetLanguages,
content: input.content,
},
options,
);

return {
run,
meta: {
plan: isFreeUser ? "free" : "pro",
},
};
}),
});
5 changes: 3 additions & 2 deletions examples/i18next/languine.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { defineConfig } from "languine";

export default defineConfig({
projectId: "",
locale: {
source: "en",
targets: ["es", "fr"],
targets: ["es"],
},
files: {
json: {
include: ["locales/[locale].json"],
include: ["src/locales/[locale].json"],
},
},
});
10 changes: 4 additions & 6 deletions examples/po/locales/es.po
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
# This is a PO file containing English strings that serve as the source for translations.
# Each entry consists of a msgid (string identifier) and msgstr (translated string).
msgid "welcome_message"
msgstr "Bienvenido a nuestra aplicación!"
msgstr "¡Bienvenido a nuestra aplicación!"

msgid "error_message"
msgstr "Se produjo un error: {error_code}"
msgstr "Ocurrió un error: {error_code}"

msgid "example_message"
msgstr "Este es un mensaje de ejemplo con una {variable}"
msgstr "Este es un mensaje de ejemplo con un {variable}"

msgid "hello"
msgstr "Hola"
msgstr "Hola"
2 changes: 1 addition & 1 deletion examples/react-i18next/locales/sv.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"notifications": {
"messages": "Du har {{count}} nya meddelande(n).",
"empty": "Inga nya meddelanden."
"empty": "Inga nya aviseringar."
},
"date": {
"today": "Idag är det {{date}}."
Expand Down
15 changes: 10 additions & 5 deletions packages/cli/src/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { installDependencies } from "@/utils/install.ts";
import { intro, isCancel, outro, select, text } from "@clack/prompts";
import { intro, isCancel, note, outro, select, text } from "@clack/prompts";
import chalk from "chalk";
import { z } from "zod";
import type { parserTypeSchema } from "../parsers/index.js";
Expand Down Expand Up @@ -205,12 +205,17 @@ export default defineConfig({

outro(chalk.green("Configuration file created successfully!"));
console.log();
console.log("Next steps:");
console.log(
`1. Review your languine.config.${configFormat === "typescript" ? "ts" : "json"} file`,

note(
`Run 'languine translate' to start translating your files`,
"Next steps.",
);
console.log("2. Run 'languine translate' to start translating your files");

console.log();

outro(
`Problems? ${chalk.underline(chalk.cyan("https://git.new/problem"))}`,
);
} catch (error) {
outro(chalk.red("Failed to create configuration file"));
console.error(error);
Expand Down
18 changes: 10 additions & 8 deletions packages/cli/src/commands/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ export async function translateCommand(args: string[] = []) {
}

if (!isSilent) {
if (!checkOnly) {
note(
"Upgrade to Pro for priority queue access at https://languine.ai/pricing",
"Pro tip",
);
}

s.start(checkOnly ? "Checking translations..." : "Translating...");
}

Expand Down Expand Up @@ -179,7 +172,7 @@ export async function translateCommand(args: string[] = []) {

let result: TranslationResult;

const run = await client.jobs.startJob.mutate({
const { run, meta } = await client.jobs.startJob.mutate({
apiKey: apiKey,
projectId: config.projectId,
sourceFormat: type,
Expand All @@ -188,6 +181,15 @@ export async function translateCommand(args: string[] = []) {
content: translationInput,
});

if (!isSilent && meta.plan === "free") {
if (!checkOnly) {
note(
"Upgrade to Pro for priority queue access at https://languine.ai/pricing",
"Pro tip",
);
}
}

await auth.withAuth(
{ accessToken: run.publicAccessToken },
async () => {
Expand Down

0 comments on commit 9c2db8c

Please sign in to comment.