-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve gemini translator (#1006)
- Loading branch information
Showing
9 changed files
with
331 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GOOGLE_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ | |
"test": "yarn test:patch", | ||
"test:patch": "git apply -v --check --directory origin ./tools/adev-patches/*.patch", | ||
"update-origin": "tsx tools/update-origin.ts", | ||
"translate": "tsx tools/translate.ts" | ||
"translate": "tsx --env-file=.env tools/translator/main.ts" | ||
}, | ||
"packageManager": "[email protected]", | ||
"devDependencies": { | ||
"@google/generative-ai": "0.14.1", | ||
"@google/generative-ai": "0.21.0", | ||
"@types/node": "20.14.10", | ||
"chokidar": "3.6.0", | ||
"consola": "3.2.3", | ||
|
@@ -32,6 +32,6 @@ | |
"textlint-rule-preset-ja-spacing": "2.4.3", | ||
"textlint-rule-preset-ja-technical-writing": "10.0.1", | ||
"textlint-rule-prh": "^6.0.0", | ||
"tsx": "^4.16.2" | ||
"tsx": "4.19.2" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import consola from 'consola'; | ||
import assert from 'node:assert'; | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { resolve } from 'node:path'; | ||
import { parseArgs } from 'node:util'; | ||
import { | ||
cpRf, | ||
exists, | ||
getEnFilePath, | ||
getLocalizedFilePath, | ||
} from '../lib/fsutils'; | ||
import { rootDir } from '../lib/workspace'; | ||
import { GeminiTranslator } from './translate'; | ||
|
||
async function main() { | ||
const apiKey = process.env.GOOGLE_API_KEY; | ||
assert(apiKey, 'GOOGLE_API_KEY 環境変数が設定されていません。'); | ||
|
||
const args = parseArgs({ | ||
options: { write: { type: 'boolean', default: false, short: 'w' } }, | ||
allowPositionals: true, | ||
}); | ||
const { write } = args.values; | ||
const [file] = args.positionals; | ||
|
||
const fileExists = await exists(file); | ||
if (!fileExists) { | ||
throw new Error(`ファイルが見つかりません: ${file}`); | ||
} | ||
|
||
const content = await readFile(file, 'utf-8'); | ||
const prh = await readFile(resolve(rootDir, 'prh.yml'), 'utf-8'); | ||
|
||
const translator = new GeminiTranslator(apiKey); | ||
const translated = await translator.translate(content, prh); | ||
|
||
console.log(translated); | ||
await writeTranslatedContent(file, translated, write); | ||
} | ||
|
||
async function writeTranslatedContent( | ||
file: string, | ||
content: string, | ||
forceWrite = false | ||
) { | ||
const outputFile = getLocalizedFilePath(file); | ||
const save = | ||
forceWrite || | ||
(await consola.prompt(`翻訳結果を保存しますか?\n保存先: ${outputFile}`, { | ||
type: 'confirm', | ||
initial: false, | ||
})); | ||
if (!save) { | ||
return; | ||
} | ||
|
||
const enFile = getEnFilePath(file); | ||
// .en.* が存在しない場合は原文コピーを忘れているため、.en.md に元ファイルをコピーする | ||
if (!(await exists(enFile))) { | ||
consola.warn( | ||
`原文ファイルが見つかりません。入力ファイルを ${enFile} にコピーします。` | ||
); | ||
await cpRf(file, enFile); | ||
} | ||
await writeFile(outputFile, content); | ||
consola.success(`保存しました`); | ||
} | ||
|
||
main().catch((error) => { | ||
consola.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type ContentBlock = string; | ||
|
||
export function splitMarkdown(content: string): ContentBlock[] { | ||
// split content by heading lines (lines starting with ##) | ||
return content.split(/\n(?=##\s)/); | ||
} | ||
|
||
export async function renderMarkdown(blocks: ContentBlock[]) { | ||
const content = blocks | ||
.map((block) => (block.endsWith('\n') ? block.replace(/\n$/, '') : block)) | ||
.join('\n\n'); | ||
// add trailing newline | ||
return content + '\n'; | ||
} |
Oops, something went wrong.