/** * Creates a translation. * * @param Model $locale * @param string $text * @param Model $parentTranslation * * @return Model */ protected function firstOrCreateTranslation(Model $locale, $text, $parentTranslation = null) { // We'll check to see if there's a cached translation // first before we try and hit the database. $cachedTranslation = $this->getCacheTranslation($locale, $text); if ($cachedTranslation instanceof Model) { return $cachedTranslation; } // Check if auto translation is enabled. If so we'll run // the text through google translate and // save it, then cache it. if ($parentTranslation && $this->autoTranslateEnabled()) { $googleTranslate = new TranslateClient(); $googleTranslate->setSource($parentTranslation->locale->code); $googleTranslate->setTarget($locale->code); try { $text = $googleTranslate->translate($text); } catch (ErrorException $e) { // Request to translate failed, set the text // to the parent translation $text = $parentTranslation->translation; } catch (UnexpectedValueException $e) { // Looks like something other than text was passed in, // we'll set the text to the parent translation // for this exception as well $text = $parentTranslation->translation; } } $translation = $this->translationModel->firstOrCreate([$locale->getForeignKey() => $locale->getKey(), $this->translationModel->getForeignKey() => isset($parentTranslation) ? $parentTranslation->getKey() : null, 'translation' => $text]); // Cache the translation so it's retrieved faster next time $this->setCacheTranslation($translation); return $translation; }
function translate($text, $lang) { $tr = new TranslateClient(); // Default is from 'auto' to 'en' $tr->setSource('en'); // Translate from English $tr->setTarget($lang); // Translate to Georgian echo $tr->translate($text); }
function translateTest() { require __DIR__ . '/vendor/autoload.php'; echo "<h1>Translation</h1>"; $tr = new TranslateClient(); // Default is from 'auto' to 'en' $tr->setSource('it'); // Translate from English $tr->setTarget('en'); // Translate to Georgian echo "Ciao a tutti ==> "; echo $tr->translate('Ciao a tutti!'); }
/** * {@inheritdoc} */ public function setTarget($target) { return $this->client->setTarget($target); }
public static function fetchFromGoogle() { $trans_command = new Command(); $trans_command->option('i')->aka('in')->describedAs("Input Language")->required()->default("en"); $trans_command->option('o')->aka('out')->describedAs("Output Language"); echo "Translate from {$trans_command['in']} to " . (isset($trans_command['out']) ? $trans_command['out'] : 'all') . "\n"; $tr = new TranslateClient(); $tr->setSource($trans_command['in']); $tr->setTarget($trans_command['out']); $langauges = Language::search()->exec(); $untranslated_phrases = PhraseReplacement::search()->where('is_translated', 'No')->exec(); foreach ($untranslated_phrases as $i => $phrase) { /* @var $phrase PhraseReplacement */ /* @var $original PhraseOriginal */ /* @var $langauge Language */ $langauge = $langauges[$phrase->language_id]; $translator = $tr->setTarget($langauge->code); $original = PhraseOriginal::search()->where('original_id', $phrase->original_id)->execOne(); $output = $translator->translate($original->value); $phrase->value = $output; $phrase->is_translated = "Yes"; $phrase->save(); } }