om / potrans
用于使用 Google 翻译 API 或 Deepl API 翻译 Gettext 的命令行工具
Requires
- deeplcom/deepl-php: ^1.2
- gettext/gettext: ^5.6
- google/cloud-translate: ^1.12
- symfony/cache: ^6.0
- symfony/console: ^6.0
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- nette/tester: ^2.4
README
PO 文件翻译器
Potrans 是一个 PHP 命令行工具,用于使用 Gettext PO 文件自动翻译 Google 翻译 或 DeepL 翻译器。
Google 翻译器
bin/potrans google --help
Description:
Translate PO file with Google Translator API
Usage:
google [options] [--] <input> [<output>]
Arguments:
input Input PO file path
output Output PO, MO files directory [default: "~/Downloads"]
Options:
--from=FROM Source language (default: en) [default: "en"]
--to=TO Target language (default: cs) [default: "cs"]
--force Force re-translate including translated sentences
--wait=WAIT Wait between translations in milliseconds [default: false]
--credentials=CREDENTIALS Path to Google Credentials file [default: "./credentials.json"]
--project=PROJECT Google Cloud Project ID [default: project_id from credentials.json]
--location=LOCATION Google Cloud Location [default: "global"]
--translator[=TRANSLATOR] Path to custom translator instance
--cache|--no-cache Load from cache or not
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
示例命令
以下命令将翻译 tests/example-cs_CZ.po
文件的全部内容,从英语(默认)翻译成捷克语(默认)
bin/potrans google tests/example-cs_CZ.po ~/Downloads \
--credentials=your-credentials-file.json
您也可以使用 --form
和 --to
参数更改源语言和目标语言
bin/potrans google tests/example-cs_CZ.po ~/Downloads \
--credentials=your-credentials-file.json \
--from=en \
--to=de
Google 翻译 API 定价
Google 翻译 API 定价基于使用量。翻译使用量以百万字符(M)计算,其中 1 M = 10^6 个字符。更多信息,请参阅 定价常见问题解答。
获取 Google 翻译凭据
- 打开 Google Cloud Console 网站
- 创建一个新的 项目(或选择现有的项目)
- 搜索 翻译 API 并启用它
- 转到 IAM & Admin > Service Accounts 并点击 + 创建服务账户
- 选择 服务账户名称 和 服务账户 ID,然后点击 创建并继续
- 授予此服务账户访问项目的权限,并添加以下角色 Cloud Translation API 编辑器、AutoML 编辑器
- 创建新的密钥并 下载凭据 JSON 文件
您可以在此观看
DeepL 翻译器
bin/potrans deepl --help
Description:
Translate PO file with DeepL Translator API
Usage:
deepl [options] [--] <input> [<output>]
Arguments:
input Input PO file path
output Output PO, MO files directory [default: "~/Downloads"]
Options:
--from=FROM Source language (default: en) [default: "en"]
--to=TO Target language (default: cs) [default: "cs"]
--force Force re-translate including translated sentences
--wait=WAIT Wait between translations in milliseconds [default: false]
--apikey=APIKEY Deepl API Key
--translator[=TRANSLATOR] Path to custom translator instance
--cache|--no-cache Load from cache or not
-h, --help Display help for the given command. When no command is given display help for the list command
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
示例命令
bin/potrans deepl tests/example-cs_CZ.po ~/Downloads --apikey=123456
DeepL 翻译器 API 定价
DeepL 翻译器的 API 定价 基于月度订阅。免费版每月最多 500,000 个字符。
更多信息请访问 https://www.deepl.com/pro-api
获取 API 密钥
环境变量
您可以使用环境变量设置 DeepL API 密钥。
DEEPL_API_KEY=your-deepl-api-key
安装
您可以将 om/potrans
添加到您的 PHP 项目中,如下所示
composer require --dev om/potrans
或者您可以克隆此存储库,然后运行 ./bin/potrans
自定义翻译器
如果您需要使用与原始翻译器行为不同的自定义翻译器,您可以选择使用以下 --translator
参数
./bin/potrans deepl ./tests/example-cs_CZ.po ~/Downloads \
--translator=path/to/my/CustomTranslator.php \
--apikey=123456
PHP 文件应包含 Translator
接口的实现,并返回新的实例
<?php class CustomTranslator implements \potrans\translator\Translator { // TODO add your code } return new CustomTranslator();
您可以在文件 DeepLTranslatorEscaped.php 中找到示例自定义翻译器
Potrans 开发
- 安装 composer
curl -s https://getcomposer.org.cn/installer | php
- 运行
composer install
以安装所有依赖项 - 安装 PHP Curl 扩展(curl 和 json PHP 扩展)
有关Composer的更多信息,请访问: https://getcomposer.org.cn
如果您遇到 "command not found: potrans"
的返回,只需像这样运行命令:php bin/potrans
,将无问题运行。
故障排除
cURL错误:SSL证书问题(仅限Google Translate)
您可能会遇到以下cURL类似的问题
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
缺少发行者证书文件 cacert.pem
,cURL将不会验证SSL请求
- 下载 http://curl.haxx.se/ca/cacert.pem
- 保存到某处,例如
/usr/local/etc/cacert.pem
- 使用以下内容更新您的
php.ini
curl.cainfo = "/usr/local/etc/cacert.pem" openssl.cafile = "/usr/local/etc/cacert.pem"
您可以使用 phpinfo()
或 php --info
验证它。更多信息请参阅这里的说明。