Skip to content

Commit

Permalink
fix: adds chunking text to deal with translator api limits
Browse files Browse the repository at this point in the history
  • Loading branch information
a-drew committed Aug 1, 2024
1 parent 9abd487 commit 2b26b6a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Drivers/AmazonTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public function __construct(array $credentials, string $region, string $version

public function translate(string $text): string
{
if (strlen($text) > 10000) {
$strings = str_split($text, 10000);

return implode('', $this->translateBatch($strings));
}

$response = $this->sendTranslateRequest($text, [
'TargetLanguageCode' => $this->target,
'SourceLanguageCode' => $this->source,
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/GoogleV2Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public function __construct(string $key, string $format = 'html', string $model

public function translate(string $text): string
{
if (strlen($text) > 102400) {
$strings = str_split($text, 102400);

return implode('', $this->translateBatch($strings));
}

$response = $this->sendTranslateRequest($text, [
'target' => $this->target,
'source' => $this->source,
Expand Down
10 changes: 8 additions & 2 deletions src/Drivers/StichozaTranslate.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ public function __construct()
$this->client = new GoogleTranslate();
}

public function translate(string $string): string
public function translate(string $text): string
{
return $this->client->setSource($this->source)->setTarget($this->target)->translate($string);
if (strlen($text) > 15000) {
$strings = str_split($text, 15000);

return implode('', $this->translateBatch($strings));
}

return $this->client->setSource($this->source)->setTarget($this->target)->translate($text);
}
}

0 comments on commit 2b26b6a

Please sign in to comment.