From 8957ed49740d5708e1d1a84927f8a2a61155598a Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 20:09:05 +0100 Subject: [PATCH 01/15] auto translate command using google translate for free! --- composer.json | 3 +- resources/lang/en/translation.php | 1 + .../Commands/AutoTranslateKeysCommand.php | 48 +++++++++++++++++++ src/Drivers/Translation.php | 42 ++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/Console/Commands/AutoTranslateKeysCommand.php diff --git a/composer.json b/composer.json index e2e7993..ef25c15 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "require": { "php": "^8.0", "illuminate/support": "^8.0||^9.0||^10.0", - "laravel/legacy-factories": "^1.3" + "laravel/legacy-factories": "^1.3", + "stichoza/google-translate-php": "^4.1" }, "require-dev": { "orchestra/testbench": "^6.0|^8.0", diff --git a/resources/lang/en/translation.php b/resources/lang/en/translation.php index d933a57..b2eb2cc 100644 --- a/resources/lang/en/translation.php +++ b/resources/lang/en/translation.php @@ -17,6 +17,7 @@ 'language_key_added' => 'New language key added successfully 👏', 'no_missing_keys' => 'There are no missing translation keys in the app 🎉', 'keys_synced' => 'Missing keys synchronised successfully 🎊', + 'auto_translated' => 'Automated Translation completed successfully 🎊', 'search' => 'Search all translations', 'translations' => 'Translation', 'language_name' => 'Name', diff --git a/src/Console/Commands/AutoTranslateKeysCommand.php b/src/Console/Commands/AutoTranslateKeysCommand.php new file mode 100644 index 0000000..7f983af --- /dev/null +++ b/src/Console/Commands/AutoTranslateKeysCommand.php @@ -0,0 +1,48 @@ +argument('language') ?: false; + try { + // if we have a language, pass it in, if not the method will + // automagically translate all languages + $this->translation->autoTranslate($language); + + return $this->info(__('translation::translation.auto_translated')); + } catch (\Exception $e) { + return $this->error($e->getMessage()); + } + + + } +} diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index 5ec47ef..c7ec659 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Event; use Illuminate\Support\Str; use JoeDixon\Translation\Events\TranslationAdded; +use Stichoza\GoogleTranslate\GoogleTranslate; abstract class Translation { @@ -51,6 +52,47 @@ public function saveMissingTranslations($language = false) } } + /** + * Save all of the translations in the app without translation for a given language. + * + * @param string $language + * @return void + */ + public function autoTranslate($language = false) + { + $languages = $language ? [$language => $language] : $this->allLanguages(); + + foreach ($languages as $language => $name) { + $this->saveMissingTranslations($language); + $this->translateLanguage($language); + } + } + + public function getGoogleTranslate($language,$token){ + $tr = new GoogleTranslate($language); + return $tr->translate($token); + } + + public function translateLanguage($language){ + $translations = $this->allTranslationsFor($language); + foreach ($translations as $type => $groups) { + foreach ($groups as $group => $translations) { + foreach ($translations as $key => $value) { + if (in_array($value,["",null])){ + $new_value=$this->getGoogleTranslate($language,$key); + if (Str::contains($group, 'single')) { + $this->addSingleTranslation($language, $group, $key,$new_value); + } else { + $this->addGroupTranslation($language, $group, $key,$new_value); + } + } + + } + } + } + } + + /** * Get all translations for a given language merged with the source language. * From a3dbb3bc7b10e883d72a01388d29876086764f36 Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 21:49:38 +0100 Subject: [PATCH 02/15] register auto translate command --- src/TranslationServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php index 5684574..c1815ec 100644 --- a/src/TranslationServiceProvider.php +++ b/src/TranslationServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Support\ServiceProvider; use JoeDixon\Translation\Console\Commands\AddLanguageCommand; use JoeDixon\Translation\Console\Commands\AddTranslationKeyCommand; +use JoeDixon\Translation\Console\Commands\AutoTranslateKeysCommand; use JoeDixon\Translation\Console\Commands\ListLanguagesCommand; use JoeDixon\Translation\Console\Commands\ListMissingTranslationKeys; use JoeDixon\Translation\Console\Commands\SynchroniseMissingTranslationKeys; @@ -151,6 +152,7 @@ private function registerCommands() ListMissingTranslationKeys::class, SynchroniseMissingTranslationKeys::class, SynchroniseTranslationsCommand::class, + AutoTranslateKeysCommand::class, ]); } } From 6ee520f318791667c69c236103b96e6130e81fcf Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 22:01:00 +0100 Subject: [PATCH 03/15] comments on methods --- readme.md | 10 ++++++++++ .../Commands/AutoTranslateKeysCommand.php | 2 +- src/Drivers/Translation.php | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 1850045..cce28b4 100644 --- a/readme.md +++ b/readme.md @@ -199,3 +199,13 @@ This command will scan your project (using the paths supplied in the configuration file) and create all of the missing translation keys. This can be run for all languages or a single language. + +### Automated Translation Using [Stichoza Google Translate Package](https://github.com/Stichoza/google-translate-php) + +``` +translation:translation:auto-translate +``` +This command will scan your project (using the paths supplied in the +configuration file) and create all of the missing translation keys. This can be +run for all languages or a single language. + diff --git a/src/Console/Commands/AutoTranslateKeysCommand.php b/src/Console/Commands/AutoTranslateKeysCommand.php index 7f983af..80f12ac 100644 --- a/src/Console/Commands/AutoTranslateKeysCommand.php +++ b/src/Console/Commands/AutoTranslateKeysCommand.php @@ -23,7 +23,7 @@ class AutoTranslateKeysCommand extends BaseCommand * * @var string */ - protected $description = 'Auto translate key using google translate'; + protected $description = 'Auto translate keys using google translate'; /** * Execute the console command. diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index c7ec659..27c86d7 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -53,7 +53,7 @@ public function saveMissingTranslations($language = false) } /** - * Save all of the translations in the app without translation for a given language. + * Translate all the token into it's respective language. * * @param string $language * @return void @@ -68,11 +68,26 @@ public function autoTranslate($language = false) } } + /** + * + * Translate text using Google Translate + * + * @param $language + * @param $token + * @return string|null + * @throws \ErrorException + */ public function getGoogleTranslate($language,$token){ $tr = new GoogleTranslate($language); return $tr->translate($token); } - + + /** + * + * Loop through all the keys and get translated text from google + * + * @param $language + */ public function translateLanguage($language){ $translations = $this->allTranslationsFor($language); foreach ($translations as $type => $groups) { From 6909956a4e9af1cd5c94915119acbc3df4be57a3 Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 22:01:26 +0100 Subject: [PATCH 04/15] remove unused classes --- src/Console/Commands/AutoTranslateKeysCommand.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Console/Commands/AutoTranslateKeysCommand.php b/src/Console/Commands/AutoTranslateKeysCommand.php index 80f12ac..e366ff2 100644 --- a/src/Console/Commands/AutoTranslateKeysCommand.php +++ b/src/Console/Commands/AutoTranslateKeysCommand.php @@ -3,11 +3,6 @@ namespace JoeDixon\Translation\Console\Commands; use Illuminate\Console\Command; -use Illuminate\Filesystem\Filesystem; -use JoeDixon\Translation\Drivers\Database; -use JoeDixon\Translation\Drivers\File; -use JoeDixon\Translation\Drivers\Translation; -use JoeDixon\Translation\Scanner; class AutoTranslateKeysCommand extends BaseCommand { From b5ce8afb0d6c694ab8d06286fb66f3c4a19898ca Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 22:07:46 +0100 Subject: [PATCH 05/15] how to use auto translate --- readme.md | 3 +++ src/Drivers/Translation.php | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index cce28b4..effc0cb 100644 --- a/readme.md +++ b/readme.md @@ -209,3 +209,6 @@ This command will scan your project (using the paths supplied in the configuration file) and create all of the missing translation keys. This can be run for all languages or a single language. +It will then translate all the tokens using Google Translate for FREE! + +You can edit these the auto translated texts using the [User interface](#User interface) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index 27c86d7..2070691 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -53,7 +53,8 @@ public function saveMissingTranslations($language = false) } /** - * Translate all the token into it's respective language. + * Save all of the translations in the app without translation for a given language then + * Translate all the tokens into it's respective language using google translate * * @param string $language * @return void From 55a8d64aeb2596133358391f30ed946e02519a49 Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 22:10:10 +0100 Subject: [PATCH 06/15] how to use auto translate --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index effc0cb..cc55896 100644 --- a/readme.md +++ b/readme.md @@ -211,4 +211,4 @@ run for all languages or a single language. It will then translate all the tokens using Google Translate for FREE! -You can edit these the auto translated texts using the [User interface](#User interface) +You can edit these the auto translated texts using the [User interface](#Userinterface) From cc5af142ea7d55e8998564987ed628e58be62e1b Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 22:10:42 +0100 Subject: [PATCH 07/15] how to use auto translate --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index cc55896..cc684d1 100644 --- a/readme.md +++ b/readme.md @@ -211,4 +211,4 @@ run for all languages or a single language. It will then translate all the tokens using Google Translate for FREE! -You can edit these the auto translated texts using the [User interface](#Userinterface) +You can edit these the auto translated texts using the [User interface](#user-interface) From 0ca3ea4ac382f515e631ce0392108f38e32f7164 Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Thu, 10 Aug 2023 22:46:35 +0100 Subject: [PATCH 08/15] Auto translate command --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index cc684d1..1cc930e 100644 --- a/readme.md +++ b/readme.md @@ -203,7 +203,7 @@ run for all languages or a single language. ### Automated Translation Using [Stichoza Google Translate Package](https://github.com/Stichoza/google-translate-php) ``` -translation:translation:auto-translate +translation:auto-translate ``` This command will scan your project (using the paths supplied in the configuration file) and create all of the missing translation keys. This can be From 7081aaeadb3f29c311a452f4880651433d2fe12c Mon Sep 17 00:00:00 2001 From: q-- <2470175+q--@users.noreply.github.com> Date: Thu, 8 Feb 2024 16:16:02 +0100 Subject: [PATCH 09/15] auto-translate value instead of key This resolves the issue where, instead of translating the value in the language file of the source language (e.g. en.json), it translates the key from the source code. This causes issues if the key is not equal to the translatable text! --- src/Drivers/Translation.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index 2070691..e7c8483 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -70,9 +70,9 @@ public function autoTranslate($language = false) } /** - * + * * Translate text using Google Translate - * + * * @param $language * @param $token * @return string|null @@ -84,22 +84,25 @@ public function getGoogleTranslate($language,$token){ } /** - * - * Loop through all the keys and get translated text from google - * + * Loop through all the keys and get translated text from Google Translate + * * @param $language */ public function translateLanguage($language){ - $translations = $this->allTranslationsFor($language); + $translations = $this->getSourceLanguageTranslationsWith($language); + foreach ($translations as $type => $groups) { foreach ($groups as $group => $translations) { foreach ($translations as $key => $value) { - if (in_array($value,["",null])){ - $new_value=$this->getGoogleTranslate($language,$key); + $sourceLanguageValue = $value[$this->sourceLanguage]; + $targetLanguageValue = $value[$language]; + + if (in_array($targetLanguageValue, ["", null])) { + $new_value = $this->getGoogleTranslate($language, $sourceLanguageValue); if (Str::contains($group, 'single')) { - $this->addSingleTranslation($language, $group, $key,$new_value); + $this->addSingleTranslation($language, $group, $key, $new_value); } else { - $this->addGroupTranslation($language, $group, $key,$new_value); + $this->addGroupTranslation($language, $group, $key, $new_value); } } @@ -107,7 +110,6 @@ public function translateLanguage($language){ } } } - /** * Get all translations for a given language merged with the source language. From f316aa45c2400af1201ba9e0fe36e6ac7a5f5c45 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 10/15] 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 7df3ced8539c5ffc3dff9438a281e9b4f30a31ec 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 11/15] 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 7ef311e58720e071319b1b9d14325e923a27641b 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 12/15] 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])) { From 8b31883ec892bf3400c01a6137aca0061e6d3cd9 Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Tue, 30 Jul 2024 21:39:41 +0200 Subject: [PATCH 13/15] You do not need to pass $this->sourceLanguage to GoogleTranslate --- src/Drivers/Translation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index c8e1a19..e1d8bd2 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -78,7 +78,7 @@ public function autoTranslate($language = false) * @throws \ErrorException */ public function getGoogleTranslate($language,$token){ - $tr = new GoogleTranslate($language, $this->sourceLanguage); + $tr = new GoogleTranslate($language); return $tr->translate($token); } From eeb5f88f7aeb581f18b807aa976b3b96bbe728ad Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Tue, 30 Jul 2024 21:40:41 +0200 Subject: [PATCH 14/15] google translate comment --- src/Drivers/Translation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index e1d8bd2..14656ef 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -70,6 +70,7 @@ public function autoTranslate($language = false) } /** + * * Translate text using Google Translate * * @param $language From 1d758543745d18d6076ebae8fa3c302713070c7a Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Tue, 30 Jul 2024 21:52:36 +0200 Subject: [PATCH 15/15] comment --- src/Drivers/Translation.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Drivers/Translation.php b/src/Drivers/Translation.php index 14656ef..6e957b1 100644 --- a/src/Drivers/Translation.php +++ b/src/Drivers/Translation.php @@ -99,8 +99,6 @@ public function translateLanguage($language){ foreach ($translations as $type => $groups) { foreach ($groups as $group => $translations) { foreach ($translations as $key => $value) { - //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];