Skip to content

Commit

Permalink
Re-added the translatable.fallback_locale config (#171)
Browse files Browse the repository at this point in the history
* Re-added the translatable.fallback_locale config

* Changelog
  • Loading branch information
royduin authored and freekmurze committed Aug 29, 2019
1 parent 153027c commit 5913ec5
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `laravel-translatable` will be documented in this file

## 4.1.4 - 2019-08-28

- re-added the `translatable.fallback_local` config which overrule `app.fallback_local` (see https://github.com/spatie/laravel-translatable/issues/170)

## 4.1.3 - 2019-06-16

- improve dependencies
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ You can install the package via composer:
composer require spatie/laravel-translatable
```

If you want to have another fallback_locale then the app fallback locale (see `config/app.php`), you could publish the config file:
```
php artisan vendor:publish --provider="Spatie\Translatable\TranslatableServiceProvider"
```

This is the contents of the published file:
```php
return [
'fallback_locale' => 'en',
];
```

## Making a model translatable

The required steps to make a model translatable are:
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,12 @@
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Spatie\\Translatable\\TranslatableServiceProvider"
]
}
}
}
9 changes: 9 additions & 0 deletions config/translatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

return [

/*
* If a translation has not been set for a given locale, use this locale instead.
*/
'fallback_locale' => 'en',
];
4 changes: 4 additions & 0 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ protected function normalizeLocale(string $key, string $locale, bool $useFallbac
return $locale;
}

if (! is_null($fallbackLocale = Config::get('translatable.fallback_locale'))) {
return $fallbackLocale;
}

if (! is_null($fallbackLocale = Config::get('app.fallback_locale'))) {
return $fallbackLocale;
}
Expand Down
20 changes: 20 additions & 0 deletions src/TranslatableServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Spatie\Translatable;

use Illuminate\Support\ServiceProvider;

class TranslatableServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__.'/../config/translatable.php' => config_path('translatable.php'),
], 'config');
}

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/translatable.php', 'translatable');
}
}
6 changes: 6 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Orchestra\Testbench\TestCase as Orchestra;
use Spatie\Translatable\TranslatableServiceProvider;

abstract class TestCase extends Orchestra
{
Expand All @@ -15,6 +16,11 @@ public function setUp(): void
$this->setUpDatabase();
}

protected function getPackageProviders($app)
{
return [TranslatableServiceProvider::class];
}

protected function setUpDatabase()
{
Schema::create('test_models', function (Blueprint $table) {
Expand Down
16 changes: 15 additions & 1 deletion tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ public function setUp(): void
}

/** @test */
public function it_will_return_fallback_locale_translation_when_getting_an_unknown_locale()
public function it_will_return_package_fallback_locale_translation_when_getting_an_unknown_locale()
{
$this->app['config']->set('app.fallback_locale', 'nl');
$this->app['config']->set('translatable.fallback_locale', 'en');

$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->save();

$this->assertSame('testValue_en', $this->testModel->getTranslation('name', 'fr'));
}

/** @test */
public function it_will_return_default_fallback_locale_translation_when_getting_an_unknown_locale()
{
$this->app['config']->set('app.fallback_locale', 'en');

Expand Down Expand Up @@ -53,6 +65,7 @@ public function it_will_return_fallback_locale_translation_when_getting_an_unkno
public function it_will_return_an_empty_string_when_getting_an_unknown_locale_and_fallback_is_not_set()
{
$this->app['config']->set('app.fallback_locale', '');
$this->app['config']->set('translatable.fallback_locale', '');

$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->save();
Expand All @@ -64,6 +77,7 @@ public function it_will_return_an_empty_string_when_getting_an_unknown_locale_an
public function it_will_return_an_empty_string_when_getting_an_unknown_locale_and_fallback_is_empty()
{
$this->app['config']->set('app.fallback_locale', '');
$this->app['config']->set('translatable.fallback_locale', '');

$this->testModel->setTranslation('name', 'en', 'testValue_en');
$this->testModel->save();
Expand Down

0 comments on commit 5913ec5

Please sign in to comment.