Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

Commit

Permalink
Add possibility to fetch a specific translation.
Browse files Browse the repository at this point in the history
Add support to fetch a specific translation with the translate method.
  • Loading branch information
vinkla committed Jan 29, 2015
1 parent d23039b commit 3490850
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Vinkla/Translator/TranslatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,23 @@ trait TranslatorTrait {
/**
* Prepare a translator instance and fetch translations.
*
* @throws TranslatorException
* @param null $locale
* @return mixed
* @throws TranslatorException
*/
public function translate()
public function translate($locale = null)
{
return $this->getTranslation();
return $this->getTranslation($locale);
}

/**
* Fetch the translation by their relations and locale.
*
* @throws TranslatorException
* @param null $locale
* @return mixed
* @throws TranslatorException
*/
private function getTranslation()
private function getTranslation($locale = null)
{
if (!$this->translator || !class_exists($this->translator))
{
Expand All @@ -59,7 +61,7 @@ private function getTranslation()
}

// Fetch the translation by their locale id.
$translation = $this->getTranslationByLocale($this->getLocaleId());
$translation = $this->getTranslationByLocale($this->getLocaleId($locale));

if (!$translation)
{
Expand Down Expand Up @@ -189,14 +191,16 @@ public function getAttribute($key)
/**
* Get the current locale set within the app.
*
* @param null $locale
* @return mixed
* @throws TranslatorException
*/
public function getLocaleId()
public function getLocaleId($locale = null)
{
$localeInstance = $this->getLocaleInstance();

$key = $this->getLocaleColumn();
$value = App::getLocale();
$value = $locale ?: App::getLocale();

return $localeInstance->where($key, $value)->first()->id;
}
Expand Down

0 comments on commit 3490850

Please sign in to comment.