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/readme.md b/readme.md index 1850045..1cc930e 100644 --- a/readme.md +++ b/readme.md @@ -199,3 +199,16 @@ 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: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. + +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/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..e366ff2 --- /dev/null +++ b/src/Console/Commands/AutoTranslateKeysCommand.php @@ -0,0 +1,43 @@ +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..6e957b1 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,70 @@ public function saveMissingTranslations($language = false) } } + /** + * 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 + */ + public function autoTranslate($language = false) + { + $languages = $language ? [$language => $language] : $this->allLanguages(); + + foreach ($languages as $language => $name) { + $this->saveMissingTranslations($language); + $this->translateLanguage($language); + } + } + + /** + * + * 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 Translate + * + * @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) { + foreach ($groups as $group => $translations) { + foreach ($translations as $key => $value) { + $sourceLanguageValue = in_array($value[$this->sourceLanguage], ["", null]) ? $key : $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); + } else { + $this->addGroupTranslation($language, $group, $key, $new_value); + } + } + + } + } + } + } + /** * Get all translations for a given language merged with the source language. * 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, ]); } }