From 9aee607d9bdb419f0eca7f3e1bcff83b24d21412 Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:42:59 +0100 Subject: [PATCH 1/3] Skip auto-translate if source language=target language --- src/Drivers/Translation.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index e7c8483..b113162 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -89,6 +89,11 @@ public function getGoogleTranslate($language,$token){ * @param $language */ public function translateLanguage($language){ + //No need to translate e.g. English to English + if ($language === $this->sourceLanguage) { + return; + } + $translations = $this->getSourceLanguageTranslationsWith($language); foreach ($translations as $type => $groups) { From 8c9aded07f876e41fc5ac1a94f5b11ac8a2e0878 Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:45:06 +0100 Subject: [PATCH 2/3] Tell Google Translate what the source language is Instead of defaulting to 'auto' and relying on Google guessing this correctly. --- src/Drivers/Translation.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index b113162..a456767 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -70,7 +70,6 @@ public function autoTranslate($language = false) } /** - * * Translate text using Google Translate * * @param $language @@ -79,7 +78,7 @@ public function autoTranslate($language = false) * @throws \ErrorException */ public function getGoogleTranslate($language,$token){ - $tr = new GoogleTranslate($language); + $tr = new GoogleTranslate($language, $this->sourceLanguage); return $tr->translate($token); } From c62777530b82ee5faf0785bf9a00617f00085a5c Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:52:12 +0100 Subject: [PATCH 3/3] Fall back to key if no source language value getSourceLanguageTranslationsWith() only takes strings from the language files, not from the application source code. If you call auto-translate with a language parameter, it'll add missing keys to the target language, but not to the source language files. We work around this by falling back to the key if no value is set in the source language. --- src/Drivers/Translation.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index a456767..c8e1a19 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -98,7 +98,9 @@ public function translateLanguage($language){ foreach ($translations as $type => $groups) { foreach ($groups as $group => $translations) { foreach ($translations as $key => $value) { - $sourceLanguageValue = $value[$this->sourceLanguage]; + //Value will be empty if it's found in the app source code but not in the source language files + //We fall back to $key in that case + $sourceLanguageValue = in_array($value[$this->sourceLanguage], ["", null]) ? $key : $value[$this->sourceLanguage]; $targetLanguageValue = $value[$language]; if (in_array($targetLanguageValue, ["", null])) {