From 8957ed49740d5708e1d1a84927f8a2a61155598a Mon Sep 17 00:00:00 2001 From: Timothy Soladoye Date: Mon, 7 Aug 2023 20:09:05 +0100 Subject: [PATCH 1/9] 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 2/9] 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 3/9] 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 4/9] 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 5/9] 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 6/9] 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 7/9] 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 8/9] 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 9/9] 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.