定制 om/potrans 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

om/potrans

Composer 安装命令:

composer require om/potrans

包简介

Command line tool for translate Gettext with Google Translator API or DeepL API

README 文档

README

Packagist Version Packagist License Packagist Downloads

PO file translator

Potrans is a PHP command line tool for automatic translation of Gettext PO files with Google Translator or DeepL Translator.

Install

You can add om/potrans to your PHP project as follows:

composer require --dev om/potrans

or you can clone this repository and then run ./bin/potrans

Google Translator

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: input file directory]

Options:
      --from=FROM                Source language (default: en) [default: "en"]
      --to=TO                    Target language (default: cs) [default: derived from input file name]
      --dir=DIR                  Root directory (default: current working directory)
      --force                    Force re-translate including translated sentences
      --only                     Create only PO file, no MO file
      --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

Example commands

Follow command will translate whole content of tests/example-cs_CZ.po from English (default) to Czech language ( default):

bin/potrans google tests/example-cs_CZ.po ~/Downloads \
  --credentials=your-credentials-file.json

You can also change source and target language with --form and --to parametters:

bin/potrans google tests/example-cs_CZ.po ~/Downloads \
  --credentials=your-credentials-file.json \
  --from=en \
  --to=de

Google Translate API Pricing

Google Translate API pricing is based on usage. Translation usage is calculated in millions of characters (M), where 1 M = 10^6 characters. For more information, see the Pricing FAQ.

Getting Google Translation Credentials

  1. Open Google Cloud Console website
  2. Create a new Project (or select existing one)
  3. Search for translate API and enable it then
  4. Go to IAM & Admin > Service Accounts and click to + Create service account
  5. Chose Service account name and Service account ID and click to Create and continue
  6. Grant this service account access to project and add follow roles Cloud Translation API Editor, AutoML Editor
  7. Create new Keys and download credentials JSON file

You can watch it here:

DeepL Translator

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: input file directory]

Options:
      --from=FROM                Source language (default: en) [default: "en"]
      --to=TO                    Target language (default: cs) [default: derived from input file name]
      --dir=DIR                  Root directory (default: current working directory)
      --force                    Force re-translate including translated sentences
      --only                     Create only PO file, no MO file
      --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

Example commands

bin/potrans deepl tests/example-cs_CZ.po ~/Downloads --from=en --to=cs --apikey=<your-deepl-api-key> -vvv

DeepL Translator API pricing

DeepL Translator API pricing is based on a monthly subscription. There is a maximum of 500,000 characters/month for free.

For more information, visit DeepL Pro API.

Getting Api Key

  1. Register free Account
  2. Visit Account summary
  3. Find the Authentication Key for the DeepL API.

Environment variables

You can use environment variables to set the DeepL API key.

DEEPL_API_KEY=<your-deepl-api-key>

Custom translator

If you need to use a custom translator that behaves differently than the original translator, you have the option to use the --translator parameter as follows:

./bin/potrans deepl ./tests/example-cs_CZ.po ~/Downloads \
    --translator=path/to/my/CustomTranslator.php \
    --apikey=<your-deepl-api-key>

PHP file should contain implementation of Translator interface and should return new instance:

<?php
class CustomTranslator implements \potrans\translator\Translator {
  // TODO add your code
}

return new CustomTranslator();

You can find an example custom translator in the file DeepLTranslatorEscaped.php

Potrans development

  1. Install composer curl -s http://getcomposer.org/installer | php
  2. Run composer install for install all dependencies
  3. Install PHP Curl extension (curl and json PHP extensions)

For more information about Composer, visit: https://getcomposer.org

If you get "command not found: potrans", just run the command like this: php bin/potrans and it will run without problems.

Troubleshooting

cURL error: SSL certificate issue (Google Translate only)

You may encounter a problem caused by cURL like the following:

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)

This is due to a missing issuer certificate cacert.pem file, and curl won't verify SSL requests:

  1. Download http://curl.haxx.se/ca/cacert.pem
  2. Save is somewhere, e.g. /usr/local/etc/cacert.pem
  3. Update your php.ini with the following:
curl.cainfo = "/usr/local/etc/cacert.pem"
openssl.cafile = "/usr/local/etc/cacert.pem"

You can verify it with phpinfo() or php --info. Read more detailed instruction here.

Links

om/potrans 适用场景与选型建议

om/potrans 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.78k 次下载、GitHub Stars 达 105, 最近一次更新时间为 2014 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「i18n」 「translation」 「translations」 「google」 「gettext」 「translator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 om/potrans 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 om/potrans 我们能提供哪些服务?
定制开发 / 二次开发

基于 om/potrans 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.78k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 105
  • 点击次数: 28
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 105
  • Watchers: 7
  • Forks: 35
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-22