使用LibreTranslate本地翻译
# 本地运行en,zh的模型
docker run -itd \
-p 5000:5000 \
-e LT_LOAD_ONLY=en,zh \
-v /home/jcleng/work/libretranslate/.local/:/home/libretranslate/.local/:rw \
--name=trans registry.cn-hangzhou.aliyuncs.com/jcleng/libretranslate-libretranslate
使用代码,把
lang.txt文件的每行进行翻译kv形式保存到文件
#!/usr/bin/env php
<?php
function translate($chinese_string)
{
$url = 'http://127.0.0.1:5000/translate';
// 构造请求参数
$data = array(
'q' => $chinese_string . '.',
'source' => 'zh',
'target' => 'en'
);
// 初始化 cURL 会话
$ch = curl_init($url);
// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($data))
));
// 执行请求并获取响应
$response = curl_exec($ch);
// 检查是否有错误发生
if (curl_errno($ch)) {
$err = curl_error($ch);
// echo 'cURL 错误: ' . curl_error($ch);
} else {
// 获取 HTTP 状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return json_decode($response, true);
}
// 关闭 cURL 会话
curl_close($ch);
if (!empty($err)) {
throw new \Exception($err);
}
}
function toSnakeCase($input)
{
// 插入下划线在大写字母前(但不是开头),然后转为小写
return str_replace([
"?",
',',
".",
":",
":",
"。",
",",
"\"",
"'",
"!",
"!",
"【",
"】",
"-"
], '', str_replace([" "], '_', strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $input))));
}
function wfile($file_name, $one_data)
{
$filename = __DIR__ . "/{$file_name}.csv";
// $handle = fopen($filename, 'a');
// fputcsv($handle, $one_data);
file_put_contents($filename, implode(',', array_values($one_data)) . "\n", FILE_APPEND);
}
function rfile($file_path)
{
$filename = $file_path;
$handle = fopen($filename, 'r');
$lines = [];
if ($handle) {
while (($line = fgets($handle)) !== false) {
$lines[] = trim($line); // 去除前后空白和换行符
}
fclose($handle);
}
return ($lines);
}
function first_word_capital($sentence)
{
$sentence = strtolower(trim($sentence)); // 全部转小写并清理空格
$words = preg_split('/\s+/', $sentence); // 拆分成单词数组
if (!empty($words)) {
$words[0] = ucfirst($words[0]); // 第一个单词首字母大写
$sentence = implode(' ', $words);
}
return $sentence;
}
// ! 生成多语言
$lines = rfile(__DIR__ . '/lang.txt');
@unlink(__DIR__ . "/lang_ch.csv");
@unlink(__DIR__ . "/lang_en.csv");
$prefix = 'user_profile';
if (!empty($argv[1])) {
$prefix = $argv[1];
}
// 遍历文件进行操作
$json_arr = [];
foreach ($lines as $key => $chinese_string) {
if (strlen($chinese_string) == 1) {
echo $chinese_string . ' 未写入' . PHP_EOL;
continue;
}
$en_string = first_word_capital(str_replace(["."], '', translate($chinese_string)['translatedText'] ?? ''));
$lang_key = $prefix . toSnakeCase($en_string);
if (isset($json_arr[$lang_key])) {
continue;
}
$json_arr[$lang_key] = $chinese_string;
//
$one_data = [
'lang_key' => $lang_key,
'value' => $chinese_string,
];
wfile('lang_ch', $one_data);
$one_data = [
'lang_key' => $lang_key,
'value' => $en_string,
];
wfile('lang_en', $one_data);
}
uasort($json_arr, function ($a, $b) {
return strlen($b) - strlen($a);
});
// ! 替换html文件
$file_content = file_get_contents(__DIR__ . "/1.html");
foreach ($json_arr as $key => $value) {
$file_content = str_replace($value, "{{ \$t('$key') }}", $file_content);
}
file_put_contents(__DIR__ . "/2.html", $file_content);