-
Notifications
You must be signed in to change notification settings - Fork 245
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
base: 2.x
Are you sure you want to change the base?
Changes from 8 commits
8957ed4
a3dbb3b
6ee520f
6909956
b5ce8af
55a8d64
cc5af14
0ca3ea4
7081aae
3f00a8f
f316aa4
7df3ced
7ef311e
8b31883
eeb5f88
1d75854
82320bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
} | ||
|
||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,63 @@ 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 | ||
* | ||
* @param $language | ||
*/ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W.r.t. the issue I identified earlier,
I fixed this function to pass the value from the source language instead: /**
* Loop through all the keys and get translated text from Google Translate
*
* @param $language
*/
public function translateLanguage($language){
$translations = $this->getSourceLanguageTranslationsWith($language);
foreach ($translations as $type => $groups) {
foreach ($groups as $group => $translations) {
foreach ($translations as $key => $value) {
$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);
} else {
$this->addGroupTranslation($language, $group, $key,$new_value);
}
}
}
}
}
} It now works correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've created a pull request for your auto-translate branch to fix this, please check it out! |
||
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. | ||
* | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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