Created
June 8, 2022 03:59
-
-
Save sorrycc/444a368b3382d097e6c5ab08ad47b5c9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import invariant from 'tiny-invariant'; | |
import 'zx/globals'; | |
import { event, info } from '../logger'; | |
const cwd = process.cwd(); | |
const API_URL = 'http://localhost:8080/translate'; | |
const cacheFile = path.join(__dirname, 'translate.cache.json'); | |
const cacheData = fs.existsSync(cacheFile) | |
? JSON.parse(fs.readFileSync(cacheFile, 'utf-8')) | |
: {}; | |
const cache = new Map(Object.entries(cacheData)); | |
const saveCache = (key: string, value: string) => { | |
cache.set(key, value); | |
fs.writeFileSync( | |
cacheFile, | |
JSON.stringify(Object.fromEntries(cache), null, 2), | |
'utf-8', | |
); | |
}; | |
const translate = async (opts: { | |
text: string; | |
sourceLang?: string; | |
targetLang?: string; | |
}) => { | |
const { text, sourceLang = 'auto', targetLang = 'ZH' } = opts; | |
if (cache.has(text)) { | |
info(`Cache hit.`); | |
return cache.get(text); | |
} | |
const res = await fetch(API_URL, { | |
method: 'POST', | |
body: JSON.stringify({ | |
text, | |
source_lang: sourceLang, | |
target_lang: targetLang, | |
}), | |
}); | |
invariant(res.status === 200, `${res.status} ${res.statusText}`); | |
const { data } = await res.json(); | |
saveCache(text, data); | |
return data; | |
}; | |
(async () => { | |
info(argv); | |
const file = argv._[0]; | |
const absFile = path.isAbsolute(file) ? file : path.join(cwd, file); | |
// read file | |
// TODO: 长文件拆分 | |
invariant(fs.existsSync(absFile), `File not found: ${absFile}`); | |
const text = fs.readFileSync(absFile, 'utf-8').trim(); | |
info(`text length: ${text.length}`); | |
invariant(text.length <= 4000, `Too long...`); | |
// translate | |
const translatedText = await translate({ | |
text, | |
sourceLang: argv.sourceLang, | |
targetLang: argv.targetLang, | |
}); | |
// merge | |
const textArr = text.split('\n\n'); | |
const translatedTextArr = translatedText.split('\n\n'); | |
const mergedArr = textArr.map((text, i) => { | |
// don't translate code block | |
if (text.startsWith('```')) { | |
return text; | |
} else { | |
return `${text}\n\n${translatedTextArr[i]}`; | |
} | |
}); | |
// write new file | |
const absNewFile = absFile.replace(/\.md/, '.translated.md'); | |
fs.writeFileSync(absNewFile, mergedArr.join('\n\n'), 'utf-8'); | |
event(`Translated to ${absNewFile}`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment