Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto translate using Translation Engine #284

Open
wants to merge 17 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions resources/lang/en/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
43 changes: 43 additions & 0 deletions src/Console/Commands/AutoTranslateKeysCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace JoeDixon\Translation\Console\Commands;

use Illuminate\Console\Command;

class AutoTranslateKeysCommand extends BaseCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'translation:auto-translate {language?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Auto translate keys using google translate';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$language = $this->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());
}


}
}
65 changes: 65 additions & 0 deletions src/Drivers/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't strictly necessary to pass the source language, but it's my understanding that if you don't, Google Translate will try to guess the source language (it's set to auto). That can sometimes lead to wrong results if two languages have the same word with a different meaning, e.g. the word 'brand' means fire in Dutch.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see. that is correct.
I didn't look at that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Thanks for your part of writing this pull request

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is my pleasure.
It was quite helpful to me and I thought to let others use it also

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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,6 +152,7 @@ private function registerCommands()
ListMissingTranslationKeys::class,
SynchroniseMissingTranslationKeys::class,
SynchroniseTranslationsCommand::class,
AutoTranslateKeysCommand::class,
]);
}
}
Expand Down