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

Allow to change default translation model namespace from config file #508

Merged
merged 5 commits into from
Aug 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,21 @@ public function getTranslationModelName()
*/
public function getTranslationModelNameDefault()
{
return get_class($this).config('translatable.translation_suffix', 'Translation');
$modelName = get_class($this);

if ($namespace = $this->getTranslationModelNamespace()) {
$modelName = $namespace.'\\'.class_basename(get_class($this));
}

return $modelName.config('translatable.translation_suffix', 'Translation');
}

/**
* @return string|null
*/
public function getTranslationModelNamespace()
{
return config('translatable.translation_model_namespace');
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@
*/
'fallback_locale' => 'en',

/*
|--------------------------------------------------------------------------
| Translation Model Namespace
|--------------------------------------------------------------------------
|
| Defines the default 'Translation' class namespace. For example, if
| you want to use App\Translations\CountryTranslation instead of App\CountryTranslation
| set this to 'App\Translations'.
|
*/
'translation_model_namespace' => null,

/*
|--------------------------------------------------------------------------
| Translation Suffix
Expand Down
9 changes: 9 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ public function test_it_finds_the_default_translation_class()
$country->getTranslationModelNameDefault());
}

public function test_it_finds_the_translation_class_with_namespace_set()
{
$this->app->make('config')->set('translatable.translation_model_namespace', 'App\Models\Translations');
$country = new Country();
$this->assertEquals(
'App\Models\Translations\CountryTranslation',
$country->getTranslationModelNameDefault());
}

public function test_it_finds_the_translation_class_with_suffix_set()
{
App::make('config')->set('translatable.translation_suffix', 'Trans');
Expand Down