Skip to content

【开源自荐】本地离线文字翻译Api #3992

@jianchang512

Description

@jianchang512

开源地址: https://github.com/jianchang512/ott

image

功能概述:

支持多语言的本地离线文字翻译Api工具,基于 LibreTranslate 的封装,目的是提供一个容易在本地机器上直接部署的翻译Api服务,无需docker,并提供 Windows 预编译exe包,不必部署,双击可用,方便新手和小白使用。第一次启动需要下载模型,后续即可离线运行.

特点:

  1. windows提供exe编译包,双击即用无需部署
  2. Mac Linux 提供本机简单部署方式
  3. 第一次下载模型后即可离线使用

Api接口使用

假如你部署的地址和端口是 http://127.0.0.1:9911

API地址:http://127.0.0.1:9911/translate

支持的语言代码: zh=中文简 zt=中文繁 en=英语 fr=法语 de=德语 ja=日语 ko=韩语 ru=俄语 es=西班牙语 th=泰国语 it=意大利语 pt=葡萄牙语 ar=阿拉伯语 tr=土耳其语 hi=印度语

请求方法:POST

请求数据: q=待翻译文本,source=文本原始语言代码可填auto自动识别,target=要翻译到的目标语言代码

返回数据: 返回json数据,正确时 {translatedText:"翻译后结果"},出错时{error:"错误原因"}

python requests 请求示例


import requests

result=requests.post("http://127.0.0.1:9911/translate",json={"q":"你好啊我的朋友","source":"zh","target":"en"})
print(result.json())

# 输出如下
{'translatedText': 'Hello, my friend'}

# 错误时返回
{'error':'错误原因'}

Javascripts fetch请求

fetch("http://127.0.0.1:9911/translate", {
  method: "POST",
  body: JSON.stringify({
    q: "Hello!", // 请求翻译的文本
    source: "en",//原始语言,或者填写 'auto' 自动检测
    target: "zh"//目标语言
  }),
  headers: { "Content-Type": "application/json" }
});

// 返回json相应
{
    "translatedText": "你好!" // translatedText 字段中是翻译后的文本
}

# 错误时返回
{'error':'错误原因'}

Javascripts jQuery ajax 请求

$.post("http://127.0.0.1:9911/translate", {
    q: "Hello!", // 请求翻译的文本
    source: "en",//原始语言,或者填写 'auto' 自动检测
    target: "zh"//目标语言
  },
  function(res){
	console.log(res)
  }
);

// 返回json相应
{
    "translatedText": "你好!" // translatedText 字段中是翻译后的文本
}

# 错误时返回

{'error':'错误原因'}

php curl


$data = array(
    'q' => 'Hello',
    'source' => 'auto',
    'target' => 'zh'
);
 
$json = json_encode($data);
$url = 'http://127.0.0.1:9911/translate';
$ch = curl_init($url);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($json)
));
 
$response = curl_exec($ch);
if(curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo $response;
}
curl_close($ch);

#返回
{
    "translatedText": "你好!" // translatedText 字段中是翻译后的文本
}

# 错误时返回
{'error':'错误原因'}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions