Skip to content

Commit

Permalink
Merge pull request #34 from mobinzk/fix/maximum-characters
Browse files Browse the repository at this point in the history
Check maximum number of characters before urlencode
  • Loading branch information
statickidz committed Apr 12, 2021
2 parents 833841e + cf0f07a commit 584b1ee
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/GoogleTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public static function translate($source, $target, $text)
*/
protected static function requestTranslation($source, $target, $text)
{

if (strlen($text) >= 5000)
throw new \Exception("Maximum number of characters exceeded: 5000");

// Google translate URL
$url = "https://translate.googleapis.com/translate_a/single?client=gtx&dt=t";

Expand All @@ -73,9 +77,6 @@ protected static function requestTranslation($source, $target, $text)
'q' => urlencode($text)
);

if (strlen($fields['q']) >= 5000)
throw new \Exception("Maximum number of characters exceeded: 5000");

// URL-ify the data for the POST
$fields_string = "";
foreach ($fields as $key => $value) {
Expand Down

0 comments on commit 584b1ee

Please sign in to comment.