untoreh / mtr
Multi api translator
0.1.0
2016-12-30 11:52 UTC
Requires
- campo/random-user-agent: >=1.2.0
- guzzlehttp/guzzle: >=6.0.0
This package is not auto-updated.
Last update: 2024-05-11 18:01:52 UTC
README
Multi language translator api wrapper in PHP, translate or compare strings or arrays of strings with language pairs supported by multiple services.
Install
Dependencies
- APC USer Cache
Using composer composer require untoreh/mtr
on the command line, then include the autoload file in PHP
require "../vendor/autoload.php"; use Mtr\Mtr;
Usage
Pass source/target language and a string or array of strings
$mtr = new Mtr(); $mtr->tr('en', 'fr', 'the fox hides quickly'); // returns : Le renard se cache rapidement $mtr->tr('en', 'fr', ['the fox', 'hides quickly']); // returns : ['le renard', 'Se cache rapidement']
Array keys are preserved.
List of base usable language codes, the priority is to google codes which means if you want
to translate chinese you should use zh-TW
or zh-CN
$mtr->supLangs(); // returns : [ 'en', 'fr', ... ]
Choose which services to use
$mtr->tr('en', 'fr', 'the fox hides quickly', ['google', 'bing']);
Add a weight to it to specify how many times a service should be chosen over the others
$mtr->tr('en', 'fr', 'the fox hides quickly', ['google' => 50, 'bing' => 5]);
Custom http options
$mtr = new Mtr(['request' => ['handler' => $stack]]);
Api keys
$mtr = new Mtr(['systran' => ['key' => $key]);
Conventions
- It is recommended to not rely on the translation to return consistent punctuation, therefore input text should be as atomic as possible.
- Some services arbitrarily encode/decode html or even add html tags themselves, such aggressive services have active decoding before the output.
Notes
- Requests are limited to
1000~
chars, strings and arrays get split or merged up to this size to try to make uniform requests. - All the parts of a request are run concurrently, pools are not used (yet).
- Default services
weight
is30
for google, bing, yandex and 10 for the rest. - Cached keys start with
mtr_
- Because services may be fickle, they will be dropped as they go down or block access.
- Not all services supports all the languages, the group of services used is transparently trimmed to the ones that support the requested language pair.
TODO/Improvements
In the issues
Credits
- guzzle/guzzle - http client
- Stichoza/google-translate-php - google token generator
- leodido/langcode-conv - base for the language code converter
- joecampo/random-user-agent - user-agent strings