chriskonnertz / deeply Goto Github PK
View Code? Open in Web Editor NEWPHP client for the DeepL.com translation API (unofficial)
License: MIT License
PHP client for the DeepL.com translation API (unofficial)
License: MIT License
I always got this info
Server side error during DeepLy API call: HTTP code 429, message: "{"jsonrpc": "2.0","error":{"code":1042902,"message":"Too many requests."}}"
Is it able to fix?
Thanks in advance.
I'm doing my testing on a localhost machine using the demo_translate.php front end.
I've added a var_dump command at the end of the callApi function to help me understand the calls being made and how they are processed.
curl_close($curl);
var_dump($rawResponseData);
return $rawResponseData;
I am seeing a separate api call on every line input in the html text box. Even on empty lines.
I'm not a programmer by trade, not an api expert. I'm just wondering why so many calls like this? Can the entire text be sent as an array of lines with the 'to' and 'from' params set?
Update: At first it appeared like it might be something perhaps unintentional but appears the logic is as you have designed. I think just for my long term application I would try to split the text myself, if necessary and send the entire thing with one call.
Would really speed up page loading and reduce number of consumed api credits.
Note: not urgent, just trying it out.
$text = <<<EOS
The Center for American Progress puts it simply: “Americans work longer hours than workers in most other developed countries, including Japan, where there is a word, ‘karoshi’, for ‘death by overwork.’ The typical American middle-income family puts in an average of 11 more hours a week in 2006 than it did in 1979.” The end result is that we all live in a structurally toxic work world to use the title of an article written by Anne-Marie Slaughter, the author of “Unfinished Business: Women Men Work Family” published in 2015. The situation is bad for men and even worse for women. So what can we as individuals do about it without jeopardizing our job and reputation? Scott McDowell offers a useful starting point in a FastCompany article: Steps We Can All Take To Defy Our Culture Of Overwork:
If you are a boss, reduce your team’s hours.
Take your vacations.
Schedule free time.
Let your mind wander.
Stop talking how busy you are.
“In the past labor unions fought for worker sanity,” Scott McDowell adds. “In our current work culture we have to take individual responsibility for our well-being and for that of those around us. It’s time to alter the norms from within; our economy, health, productivity, and happiness depend on it.”
EOS;
$translatedText = $deepLy->translate($text, DeepLy::LANG_EN, DeepLy::LANG_FR);
Returns:
( ! ) Notice: Undefined offset: 0 in /var/www/html/libs/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/ResponseBag/TranslationBag.php on line 81
Call Stack
1 0.0006 367048 {main}( ) .../tryme.php:0
2 0.0103 1837440 ChrisKonnertz\DeepLy\DeepLy->translate( ) .../tryme.php:35
3 3.2932 1864856 ChrisKonnertz\DeepLy\ResponseBag\TranslationBag->getTranslation( ) .../DeepLy.php:239
( ! ) Notice: Trying to get property of non-object in /var/www/html/libs/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/ResponseBag/TranslationBag.php on line 87
Call Stack
1 0.0006 367048 {main}( ) .../tryme.php:0
2 0.0103 1837440 ChrisKonnertz\DeepLy\DeepLy->translate( ) .../tryme.php:35
3 3.2932 1864856 ChrisKonnertz\DeepLy\ResponseBag\TranslationBag->getTranslation( ) .../DeepLy.php:239
Le Center for American Progress le dit simplement:"Les Américains travaillent plus d'heures que les travailleurs dans la plupart des autres pays développés, y compris au Japon, où il y a un mot," karoshi ", pour" mort par surmenage "; la famille américaine typique à revenu moyen consacre en moyenne 11 heures de plus par semaine en 2006 qu'en 1979;" Le résultat final est que nous vivons tous dans un monde du travail structurellement toxique pour utiliser le titre d'un article écrit par Anne-Marie Sla ". La situation est mauvaise pour les hommes et pire encore pour les femmes. Que pouvons-nous donc faire, en tant qu'individus, pour y remédier sans mettre en péril notre travail et notre réputation? Prenez vos vacances. Prévoir du temps libre. Laisse ton esprit vagabonder. Arrête de dire à quel point tu es occupé. Dans le passé, les syndicats se sont battus pour la santé mentale des travailleurs ", ajoute Scott McDowell. Dans notre culture de travail actuelle, nous devons assumer la responsabilité individuelle de notre bien-être et de celui de ceux qui nous entourent. Il est temps de changer les normes de l'intérieur; notre économie, notre santé, notre productivité et notre bonheur en dépendent."
Hello Chriskonnertz,
This could be a general question.
Our organization wanna open a translator for all employees, therefore they want to keep the translator link in the intranet.
So once the user opens the translator page, he/she must open the page with DeepL Pro (without explicit login) - how can we achieve that? is there any automatic OAuth API provided by DeepL?
Thanks in advance.
Best regards.
As Travis CI is no logner an option, move test running to GitHub.
Branch: https://github.com/chriskonnertz/DeepLy/tree/feature-keep-line-breaks
Todo:
You can do an update! the code of Deepl has changed you can review and see if it can still be used to make an update !! Please!!
Hi,
I've a problem with the translations.
e.g if I translate the word "ciao" form italian to english, it still returns "ciao" . But if I translate "ciao amici miei" it's correct.
I'm using the latest version of your class.
PHP Fatal error: Uncaught exception 'ChrisKonnertz\DeepLy\ResponseBag\BagException' with message 'DeepLy API call resulted in a malformed result - beams array is empty in translation 0' in /home/developing/hn_bld/deepl/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/ResponseBag/TranslationBag.php:58
Stack trace:
#0 /home/developing/hn_bld/deepl/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/ResponseBag/AbstractBag.php(29): ChrisKonnertz\DeepLy\ResponseBag\TranslationBag->verifyResponseContent(Object(stdClass))
#1 /home/developing/hn_bld/deepl/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/DeepLy.php(279): ChrisKonnertz\DeepLy\ResponseBag\AbstractBag->__construct(Object(stdClass))
#2 /home/developing/hn_bld/deepl/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/DeepLy.php(299): ChrisKonnertz\DeepLy\DeepLy->requestTranslation('Kotlin/Native v...', 'PL', 'EN', false)
#3 /home/developing/hn_bld/bot.php(150): ChrisKonnertz\DeepLy\DeepLy->translate('Kotlin/Native v...', 'PL', 'EN')
#4 /home/developing/hn_bld/bo in /home/developing/hn_bld/deepl/vendor/chriskonnertz/deeply/src/ChrisKonnertz/DeepLy/ResponseBag/TranslationBag.php on line 58
Hi Chris, just to let you know, the API is currently limited. I still have to figure out if they put a hard limit on the absolute request count from an IP or are just limiting the number of requests within a certain timespan.
One should not currently rely on the API for production use.
Error message: Server side error during DeepLy API call: HTTP code 429, message: "{"jsonrpc": "2.0","error":{"code":1042901,"message":"Too many requests."}}"
translateSentences
method that directly translates sentences without splliting a text into sentences -> this means the additional API call is no longer necessaryHttpClientInterface
Deeply::splitText
method --> create a separate bag classWhen I call $deepLy->translate($text, 'EN', 'DE') deepLy returns french text when the original text ist aready english or mixed english/german (I don't do language checks, because sometimes we have mixed language texts.
Test case:
$deepLy = new ChrisKonnertz\DeepLy\DeepLy();
echo $deepLy->translate("Doors: 8pm Father Sky Mother Earth has released two monumental meditative albums since the beginning in autumn 2016. „Across the River of Time“ is an atmospheric journey through your own universe. Dark, intense and ecstatic: The Drone Doom Duo let’s you forget space and time. MAENTIS ist eine Doom Sludge Band aus Hildesheim. Das 2015 gegründete Quartett (Alexander Seidel & Jan Gibron – Gitarre, Björn Behrens – Bass, Andy Krüßel – Schlagzeug + Stimme) lässt massive Klangwände mit kalter, klirrender Psychedelik verschmelzen: der Zuhörer verfängt sich in einem Sog der transzendentalen Dunkelheit. Das Debut-Album I HAVE TASTED DEVIL’S BLOOD wird als eines der besten instrumentalen Sludge/Doom-Veröffentlichungen 2016 angesehen.",'EN','DE');
This also happens when I set source language to auto or EN.
And it also happens when I pass english strings with auto source or EN as source:
$deepLy = new ChrisKonnertz\DeepLy\DeepLy();
echo $deepLy->translate("Thunder and lightning",'EN');
echo $deepLy->translate("Thunder and lightning",'EN','EN');
This leaves the short source string in english, but doesnt work for the mixed longer text above:
echo $deepLy->translate("Thunder and lightning",'EN','DE');
The DeepL docs says that there will be an API key. DeepLy 2 will support the usage of this API key.
Development will happen on this branch:
https://github.com/chriskonnertz/DeepLy/tree/2.0
Nice but very granurality. over 10 class... 👎
Simply is Best.
One simply function.
$rta = translate('Hola todos, espero que hoy tengan un buen dia.'); print_r($rta);
`
function translate($text, $from='LANG_ES', $to='LANG_EN'){
$params = ['lang' => [ 'ser_preferred_langs' => ['LANG_ES', 'LANG_EN'], 'source_lang_user_selected' => 'LANG_ES', 'target_lang' => 'LANG_EN', ] ];
$params['jobs'][0] = [ 'kind' => 'default', 'raw_en_sentence' => $text ];
$data = ['jsonrpc' => '2.0', 'method' => 'LMT_handle_jobs', 'params' => $params, 'id' => '2', ];
$data = json_encode($data);
$curl = curl_init('https://www.deepl.com/jsonrpc/');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)) );
$rawResponseData = curl_exec($curl);
$rawResponseData = json_decode($rawResponseData);
return $rawResponseData;
}`
This project seems really interesting but I wasn't able to test it on my web server.
I have a virtual server with Plesk 12.5 and I tried to follow the directions on https://docs.plesk.com/en-US/12.5/administrator-guide/website-management/websites-and-domains/web-applications/managing-php-project-dependencies-with-composer.74969/ but without good results.
Can you tell me how I should proceed, step by step, starting from the DeepLy-master.zip file?
Sometimes it simply do not translate the text,
for example
With this sentence: "Diatec Trentino-Cucine Lube Civitanova (Play Off, Finale - gara 1)"
from IT to EN it works perfectly.
but if I translate this one " Trentino Diatec-Tours Vb (Andata di Finale di 2017 CEV Cup)"
it simply return the exact same sentence not translated.
The tests of version 2 are not covering much code.
Hello,
I was using your lib with deepl but now I have purchased DeepL Pro, will your code work with pro ? And can you please tell me where I can use Auth Key in your code ?
Thanks.
Hello,
I am getting 429 request limit error. After how many time I can re-request for translation. When this error gone ?
Hi i use your plugin to translate my website, and I love it!
From time to time i get an error "Too many request", if I used the API to much. After some time it worked again.
But since today, I get it from the start...
Do you think they changed something, so that this plugin is not working anymore?
Best
Beat
I'm providing an API key on instantiation and still getting this request limit.
What to do about it? I barely made 10 translations.
DeepL has published documentation of the API: https://www.deepl.com/docs/api-reference.html
I'm getting a mistake: {"jsonrpc": "2.0","error":{"code":1042902,"message":"Too many requests."}}
But there is a working option:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www2.deepl.com/jsonrpc');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"jsonrpc\":\"2.0\",\"method\": \"LMT_handle_jobs\",\"params\":{\"jobs\":[{\"kind\":\"default\",\"raw_en_sentence\":\"Too many requests.\",\"raw_en_context_before\":[],\"raw_en_context_after\":[],\"preferred_num_beams\":4}],\"lang\":{\"user_preferred_langs\":[\"DE\",\"ZH\",\"RU\",\"EN\"],\"source_lang_computed\":\"EN\",\"target_lang\":\"RU\"},\"priority\":1,\"commonJobParams\":{},\"timestamp\":1589020424674},\"id\":27440005}");
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Authority: www2.deepl.com';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36';
$headers[] = 'Content-Type: text/plain';
$headers[] = 'Accept: */*';
$headers[] = 'Origin: https://www.deepl.com';
$headers[] = 'Sec-Fetch-Site: same-site';
$headers[] = 'Sec-Fetch-Mode: cors';
$headers[] = 'Sec-Fetch-Dest: empty';
$headers[] = 'Referer: https://www.deepl.com/translator';
$headers[] = 'Accept-Language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6,zh;q=0.5';
$headers[] = 'Cookie: _ga=GA1.2.1530849196.1588933989; _gid=GA1.2.707299579.1588933989; LMTBID=067e4975-1add-4f13-8181-855b6f9d002c|6d2cbd1dedad11e5b2480e970569082c; _gat=1';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
print_r($result);
is there a way to fix the code?
Hello,
is this also working without an API-key from DeepL?
Greets
hi,
i add $class=str_replace('\', '/', $class); in your miniAutoloader-function ;)
kind regards,
andreas
DeepL now provides an API endpoint to list the supported languages: https://www.deepl.com/docs-api/other-functions/listing-supported-languages
Using this endpoint avoids having to adapt the library for every new language.
Note: With PT-BR
and PT-PT
, DeepL now also supports two variations: https://www.deepl.com/en/blog/20200402.html
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.