setTarget() public method

Set target language.
public setTarget ( string $target ) : mixed
$target string Language code
return mixed
 public function testErrorInResponseThrowsException()
 {
     $text = 'Hello, World!';
     $response = m::mock(ResponseInterface::class);
     $this->guzzle->shouldReceive('request')->once()->with('GET', 'https://www.googleapis.com/language/translate/v2', ['query' => ['key' => config('translation.clients.api_key'), 'format' => 'html', 'source' => 'en', 'target' => 'es', 'q' => $text]])->andReturn($response);
     $response->shouldReceive('getBody')->andReturn(json_encode(['error' => 'foo']));
     $this->client->setSource('en');
     $this->client->setTarget('es');
     $this->setExpectedException(\ErrorException::class);
     $this->client->translate($text);
 }
Esempio n. 2
0
 /**
  * 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()) {
         $this->client->setSource($parentTranslation->locale->code);
         $this->client->setTarget($locale->code);
         try {
             $text = $this->client->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;
         }
     }
     if ($parentTranslation) {
         // If a parent translation is given we're looking for it's child translation.
         $translation = $this->translationModel->firstOrNew([$locale->getForeignKey() => $locale->getKey(), $this->translationModel->getForeignKey() => $parentTranslation->getKey()]);
     } else {
         // Otherwise we're creating the parent translation.
         $translation = $this->translationModel->firstOrNew([$locale->getForeignKey() => $locale->getKey(), 'translation' => $text]);
     }
     if (empty($translation->getAttribute('translation'))) {
         // We need to make sure we don't overwrite the translation
         // if it exists already in case it was modified.
         $translation->setAttribute('translation', $text);
     }
     if ($translation->isDirty()) {
         $translation->save();
     }
     // Cache the translation so it's retrieved faster next time
     $this->setCacheTranslation($translation);
     return $translation;
 }