Skip to content

Commit

Permalink
targetLocaleへの変更に伴い、翻訳関連の関数の引数名を更新
Browse files Browse the repository at this point in the history
  • Loading branch information
ttizze committed Feb 19, 2025
1 parent 51b9f38 commit 6e3b986
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
12 changes: 6 additions & 6 deletions next/src/features/translate/lib/io-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { NumberedElement } from "../types";
export async function saveTranslationsForPage(
extractedTranslations: NumberedElement[],
pageSegments: { id: number; number: number }[],
locale: string,
targetLocale: string,
aiModel: string,
) {
const systemUserId = await getOrCreateAIUser(aiModel);
Expand All @@ -23,7 +23,7 @@ export async function saveTranslationsForPage(
return null;
}
return {
locale,
locale: targetLocale,
text: translation.text,
pageSegmentId,
userId: systemUserId,
Expand All @@ -41,7 +41,7 @@ export async function saveTranslationsForPage(
export async function saveTranslationsForComment(
extractedTranslations: NumberedElement[],
pageCommentSegments: { id: number; number: number }[],
locale: string,
targetLocale: string,
aiModel: string,
) {
const systemUserId = await getOrCreateAIUser(aiModel);
Expand All @@ -59,7 +59,7 @@ export async function saveTranslationsForComment(
return null;
}
return {
locale,
locale: targetLocale,
text: translation.text,
pageCommentSegmentId,
userId: systemUserId,
Expand All @@ -78,7 +78,7 @@ export async function getTranslatedText(
geminiApiKey: string,
aiModel: string,
numberedElements: NumberedElement[],
locale: string,
targetLocale: string,
title: string,
) {
const source_text = numberedElements
Expand All @@ -89,6 +89,6 @@ export async function getTranslatedText(
aiModel,
title,
source_text,
locale,
targetLocale,
);
}
17 changes: 9 additions & 8 deletions next/src/features/translate/lib/translate.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function translateChunk(
geminiApiKey: string,
aiModel: string,
numberedElements: NumberedElement[],
locale: string,
targetLocale: string,
pageId: number,
title: string,
translateTarget: TranslateTarget,
Expand All @@ -82,7 +82,7 @@ async function translateChunk(
geminiApiKey,
aiModel,
pendingElements,
locale,
targetLocale,
title,
);

Expand All @@ -97,7 +97,7 @@ async function translateChunk(
await saveTranslationsForPage(
partialTranslations,
pageSegments,
locale,
targetLocale,
aiModel,
);
} else {
Expand All @@ -110,7 +110,7 @@ async function translateChunk(
await saveTranslationsForComment(
partialTranslations,
pageCommentSegments,
locale,
targetLocale,
aiModel,
);
}
Expand Down Expand Up @@ -138,20 +138,21 @@ async function getTranslatedText(
geminiApiKey: string,
aiModel: string,
numberedElements: NumberedElement[],
locale: string,
targetLocale: string,
title: string,
) {
const source_text = numberedElements
.map((el) => JSON.stringify(el))
.join("\n");
const localeName =
supportedLocaleOptions.find((sl) => sl.code === locale)?.name || locale;
const targetLocaleName =
supportedLocaleOptions.find((sl) => sl.code === targetLocale)?.name ||
targetLocale;
const result = await getGeminiModelResponse(
geminiApiKey,
aiModel,
title,
source_text,
localeName,
targetLocaleName,
);
return result;
}
4 changes: 2 additions & 2 deletions next/src/features/translate/services/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getGeminiModelResponse(
model: string,
title: string,
source_text: string,
target_language: string,
target_locale: string,
) {
const decryptedApiKey = decrypt(geminiApiKey);
const genAI = new GoogleGenerativeAI(decryptedApiKey);
Expand Down Expand Up @@ -62,7 +62,7 @@ export async function getGeminiModelResponse(
for (let retryCount = 0; retryCount < MAX_RETRIES; retryCount++) {
try {
const result = await modelConfig.generateContent(
generateSystemMessage(title, source_text, target_language),
generateSystemMessage(title, source_text, target_locale),
);
return result.response.text();
} catch (error: unknown) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function generateSystemMessage(
title: string,
source_text: string,
target_language: string,
target_locale: string,
): string {
return `
You are a skilled translator. Your task is to accurately translate the given text into beautiful and natural sentences in the target language. Please follow these guidelines:
Expand All @@ -13,7 +13,7 @@ export function generateSystemMessage(
Document title: ${title}
Translate the following array of English texts into ${target_language}.
Translate the following array of English texts into ${target_locale}.
Important instructions:
- Do not explain your process or self-reference.
Expand Down Expand Up @@ -47,7 +47,7 @@ export function generateSystemMessage(
Input text:
${source_text}
Translate to ${target_language} and output in the following format:
Translate to ${target_locale} and output in the following format:
[
{
"number": 1,
Expand Down
2 changes: 1 addition & 1 deletion next/src/features/translate/translate-user-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Queue } from "@/lib/queue.server";
import type { Job } from "bullmq";
import { translate } from "./lib/translate.server";
import type { TranslateJobParams } from "./types";
const QUEUE_VERSION = 13;
const QUEUE_VERSION = 14;

export const getTranslateUserQueue = (userId: string) => {
return Queue<TranslateJobParams>(
Expand Down

0 comments on commit 6e3b986

Please sign in to comment.