Skip to content

Commit

Permalink
Merge pull request #6 from am-mokhtari/fix/translation-keywords
Browse files Browse the repository at this point in the history
improve `translateRecursive` func
  • Loading branch information
alisalehi1380 authored Sep 9, 2024
2 parents e52f481 + e72ceb1 commit c18e097
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/Services/TranslateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,37 @@ private function setUpGoogleTranslate(): GoogleTranslate
private function translateLangFiles(array $content): array
{
$google = $this->setUpGoogleTranslate();

if (!empty($content)) {
return $this->translateRecursive($content, $google);
}

if (empty($content))
return [];

return $this->translateRecursive($content, $google);
}

private function translateRecursive($content, $google) : array
{
$trans_data = [];

foreach ($content as $key => $value) {
if (!is_array($value)) {
$trans_data[$key] = $google->translate($value);
} else {
if (is_array($value)) {
$trans_data[$key] = $this->translateRecursive($value, $google);
continue;
}

$hasProps = str_contains($value, ':');
$modifiedValue = $hasProps
? preg_replace_callback(
'/(:\w+)/',
fn($match) => '{' . $match[0] . '}',
$value
)
: $value;

$translatedValue = $google->translate($modifiedValue);

$trans_data[$key] = $hasProps
? str_replace(['{', '}'], '', $translatedValue)
: $translatedValue;
}

return $trans_data;
Expand Down

0 comments on commit c18e097

Please sign in to comment.