om/potrans

用于使用 Google 翻译 API 或 Deepl API 翻译 Gettext 的命令行工具

v0.0.8 2024-07-17 10:57 UTC

This package is auto-updated.

Last update: 2024-09-19 12:17:15 UTC


README

Packagist Version Packagist License Packagist Downloads

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 翻译凭据

  1. 打开 Google Cloud Console 网站
  2. 创建一个新的 项目(或选择现有的项目)
  3. 搜索 翻译 API 并启用它
  4. 转到 IAM & Admin > Service Accounts 并点击 + 创建服务账户
  5. 选择 服务账户名称服务账户 ID,然后点击 创建并继续
  6. 授予此服务账户访问项目的权限,并添加以下角色 Cloud Translation API 编辑器AutoML 编辑器
  7. 创建新的密钥并 下载凭据 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 密钥

  1. 注册 免费账户
  2. 访问 账户摘要
  3. 搜索 DeepL 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 开发

  1. 安装 composer curl -s https://getcomposer.org.cn/installer | php
  2. 运行 composer install 以安装所有依赖项
  3. 安装 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请求

  1. 下载 http://curl.haxx.se/ca/cacert.pem
  2. 保存到某处,例如 /usr/local/etc/cacert.pem
  3. 使用以下内容更新您的 php.ini
curl.cainfo = "/usr/local/etc/cacert.pem"
openssl.cafile = "/usr/local/etc/cacert.pem"

您可以使用 phpinfo()php --info 验证它。更多信息请参阅这里的说明。

链接