Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Update for v4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsav committed Aug 21, 2014
2 parents a06747f + fa9bfdd commit 6abe7c7
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## Coding Guidelines

Translatable follows the [coding guidelines](https://github.com/laravel/framework/blob/master/CONTRIBUTING.md#coding-guidelines) used by Laravel.

Pull requests MUST be sent to the development branch.
12 changes: 9 additions & 3 deletions Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App;
use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Database\Eloquent\Model;

trait Translatable {

Expand All @@ -24,12 +25,16 @@ public function translateOrDefault($locale)
public function getTranslation($locale = null, $withFallback = false)
{
$locale = $locale ?: App::getLocale();
$withFallback = isset($this->useTranslationFallback) ? $this->useTranslationFallback : $withFallback;

if ($this->getTranslationByLocaleKey($locale))
{
$translation = $this->getTranslationByLocaleKey($locale);
}
elseif ($withFallback && App::make('config')->has('app.fallback_locale'))
elseif ($withFallback
&& App::make('config')->has('app.fallback_locale')
&& $this->getTranslationByLocaleKey(App::make('config')->get('app.fallback_locale'))
)
{
$translation = $this->getTranslationByLocaleKey(App::make('config')->get('app.fallback_locale'));
}
Expand Down Expand Up @@ -64,7 +69,8 @@ public function getTranslationModelName()

public function getTranslationModelNameDefault()
{
return get_class($this) . 'Translation';
$config = App::make('config');
return get_class($this) . $config->get('app.translatable_suffix', 'Translation');
}

public function getRelationKey()
Expand Down Expand Up @@ -203,7 +209,7 @@ protected function saveTranslations()
return $saved;
}

protected function isTranslationDirty($translation)
protected function isTranslationDirty(Model $translation)
{
$dirtyAttributes = $translation->getDirty();
unset($dirtyAttributes[$this->getLocaleKey()]);
Expand Down
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ return array(
// The default locale
'locale' => 'en',

// Override the default 'Translation' class suffix
// to use CountryTrans instead of CountryTranslation
'translatable_suffix' => 'Trans'

);
```

Expand All @@ -166,6 +170,12 @@ Translatable is fully compatible with all kinds of Eloquent extensions, includin

## Version History

### v. 4.3

* The `Translation` class suffix default can be overridden in the app config. See [7ecc0a75d](https://github.com/dimsav/laravel-translatable/commit/7ecc0a75dfcec58ebf694e0a7feb686294b49847)
* The `app.fallback_locale` setting can be overridden in each model separately. See [#33](https://github.com/dimsav/laravel-translatable/pull/33)
* Fallback translation is not returned if it is not defined.

### v. 4.2

* Fallback locale now is taken from `app.fallback_locale` config key.
Expand Down
40 changes: 40 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ public function it_finds_the_default_translation_class()
$country->getTranslationModelNameDefault());
}

/**
* @test
*/
public function it_finds_the_translation_class_with_suffix_set()
{
App::make('config')->set('app.translatable_suffix', 'Trans');
$country = new Country;
$this->assertEquals(
'Dimsav\Translatable\Test\Model\CountryTrans',
$country->getTranslationModelName());
}

/**
* @test
*/
Expand Down Expand Up @@ -206,4 +218,32 @@ public function it_returns_default_translation()
$this->assertEquals($country->getTranslation('ch', false)->name, null);
}

/**
* @test
*/
public function models_fallback_option_overrides_fallback_option_in_config()
{
$country = Country::find(1);
$this->assertEquals($country->getTranslation('ch', true)->locale, 'de');

$country = Country::find(1);
$country->useTranslationFallback = false;
$this->assertEquals($country->getTranslation('ch', true)->locale, 'ch');

$country = Country::find(1);
$country->useTranslationFallback = true;
$this->assertEquals($country->getTranslation('ch', true)->locale, 'de');
}

/**
* @test
*/
public function it_skips_fallback_if_fallback_is_not_defined()
{
App::make('config')->set('app.fallback_locale', 'ch');

$country = Country::find(1);
$this->assertEquals($country->getTranslation('pl', true)->locale, 'pl');
}

}

0 comments on commit 6abe7c7

Please sign in to comment.