PHPackages                             om/potrans - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Localization &amp; i18n](/categories/localization)
4. /
5. om/potrans

ActiveLibrary[Localization &amp; i18n](/categories/localization)

om/potrans
==========

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

v1.0.1(4mo ago)10515.0k↓15.8%36[1 issues](https://github.com/OzzyCzech/potrans/issues)3MITPHP

Since Mar 3Pushed 1mo ago7 watchersCompare

[ Source](https://github.com/OzzyCzech/potrans)[ Packagist](https://packagist.org/packages/om/potrans)[ RSS](/packages/om-potrans/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (7)Versions (13)Used By (3)

[![Packagist Version](https://camo.githubusercontent.com/447df850b4518c353ab69d95a776d6688db83c93485cab55632b0134eec1ab5c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6d2f706f7472616e733f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/447df850b4518c353ab69d95a776d6688db83c93485cab55632b0134eec1ab5c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6d2f706f7472616e733f7374796c653d666f722d7468652d6261646765)[![Packagist License](https://camo.githubusercontent.com/a669343986faea595f80c1d1181611313fbfca6cec67fd6a73b9dd53713d90ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f6d2f706f7472616e733f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/a669343986faea595f80c1d1181611313fbfca6cec67fd6a73b9dd53713d90ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f6d2f706f7472616e733f7374796c653d666f722d7468652d6261646765)[![Packagist Downloads](https://camo.githubusercontent.com/2d4e8809729724e6248bfebdf4f74a549a4d2c173c3a81959e18ea3c7c398626/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6f6d2f706f7472616e733f7374796c653d666f722d7468652d6261646765)](https://camo.githubusercontent.com/2d4e8809729724e6248bfebdf4f74a549a4d2c173c3a81959e18ea3c7c398626/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6f6d2f706f7472616e733f7374796c653d666f722d7468652d6261646765)

PO file translator
==================

[](#po-file-translator)

Potrans is a PHP command line tool for automatic translation of [Gettext](https://www.gnu.org/software/gettext/) PO files with [Google Translator](https://cloud.google.com/translate) or [DeepL Translator](https://www.deepl.com/).

Install
-------

[](#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
-----------------

[](#google-translator)

```
bin/potrans google --help
```

```
Description:
  Translate PO file with Google Translator API

Usage:
  google [options] [--]  []

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

[](#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)

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](https://cloud.google.com/translate/pricing).

- [Translaton API](https://cloud.google.com/translate)
- [Quick Starts](https://cloud.google.com/translate/docs/quickstarts)
- [Supported languages](https://developers.google.com/translate/v2/using_rest#language-params)

### Getting Google Translation Credentials

[](#getting-google-translation-credentials)

1. Open [Google Cloud Console](https://console.cloud.google.com/) website
2. Create a new **Project** (or select existing one)
3. Search for [translate API](https://cloud.google.com/translate/docs/apis) and enable it then
4. Go to [IAM &amp; Admin](https://console.cloud.google.com/iam-admin/iam) &gt; *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:

[![](https://camo.githubusercontent.com/5eda8b513c23106b826ec7af523da129006bf822355dca1b92451fb726fc1dee/68747470733a2f2f696d672e796f75747562652e636f6d2f76692f5343795031414e322d45452f6d617872657364656661756c742e6a7067)](https://www.youtube.com/watch?v=SCyP1AN2-EE)

DeepL Translator
----------------

[](#deepl-translator)

```
bin/potrans deepl --help
```

```
Description:
  Translate PO file with DeepL Translator API

Usage:
  deepl [options] [--]  []

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

[](#example-commands-1)

```
bin/potrans deepl tests/example-cs_CZ.po ~/Downloads --from=en --to=cs --apikey= -vvv
```

### DeepL Translator API pricing

[](#deepl-translator-api-pricing)

DeepL Translator [API pricing](https://www.deepl.com/pro-api) is based on a monthly subscription. There is a maximum of 500,000 characters/month for free.

For more information, visit [DeepL Pro API](https://www.deepl.com/pro-api).

### Getting Api Key

[](#getting-api-key)

1. Register [free Account](https://www.deepl.com/pro)
2. Visit [Account summary](https://www.deepl.com/pro-account/summary)
3. Find the Authentication Key for the DeepL API.

### Environment variables

[](#environment-variables)

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

```
DEEPL_API_KEY=
```

Custom translator
-----------------

[](#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=
```

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

```
