diff --git a/readme.md b/readme.md index 2e31e54..351ec8b 100644 --- a/readme.md +++ b/readme.md @@ -201,7 +201,7 @@ class Country extends Eloquent ### Available methods ```php -// This is how we determine the current locale. +// Before we get started, this is how we determine the current locale. // It is set by laravel or other packages. App::getLocale(); // 'fr' diff --git a/src/Translatable/Translatable.php b/src/Translatable/Translatable.php index 2059f77..af38bd9 100644 --- a/src/Translatable/Translatable.php +++ b/src/Translatable/Translatable.php @@ -12,7 +12,7 @@ trait Translatable /** * Alias for getTranslation() * - * @param strign|null $locale + * @param string|null $locale * @param bool $withFallback * * @return \Illuminate\Database\Eloquent\Model|null @@ -316,17 +316,35 @@ protected function isKeyALocale($key) */ protected function getLocales() { - $config = App::make('config'); - $locales = (array) $config->get('translatable.locales', []); + $localesConfig = (array) App::make('config')->get('translatable.locales'); - if (empty($locales)) { + if (empty($localesConfig)) { throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '. ' and that the locales configuration is defined.'); } + $locales = []; + foreach ($localesConfig as $key => $locale) { + if (is_array($locale)) { + $locales[] = $key; + foreach ($locale as $countryLocale) { + $locales[] = $key.$this->getLocaleSeparator().$countryLocale; + } + } else { + $locales[] = $locale; + } + } + return $locales; } + /** + * @return string + */ + protected function getLocaleSeparator() { + return App::make('config')->get('translatable.locale_separator', '-'); + } + /** * @return bool */ diff --git a/src/config/translatable.php b/src/config/translatable.php index 1febd0e..8873ab6 100644 --- a/src/config/translatable.php +++ b/src/config/translatable.php @@ -10,7 +10,26 @@ | Contains an array with the applications available locales. | */ - 'locales' => ['en', 'es'], + 'locales' => [ + 'en', + 'fr', + 'es' => [ + 'MX', // mexican spanish + 'CO', // colombian spanish + ], + ], + + /* + |-------------------------------------------------------------------------- + | Locale separator + |-------------------------------------------------------------------------- + | + | This is a string used to glue the language and the country when defining + | the available locales. Example: if set to '-', then the locale for + | colombian spanish will be saved as 'es-CO' into the database. + | + */ + 'locale_separator' => '-', /* |-------------------------------------------------------------------------- diff --git a/tests/TranslatableTest.php b/tests/TranslatableTest.php index 78ab280..91b36fd 100644 --- a/tests/TranslatableTest.php +++ b/tests/TranslatableTest.php @@ -3,6 +3,7 @@ use Dimsav\Translatable\Test\Model\Country; use Dimsav\Translatable\Test\Model\CountryStrict; use Dimsav\Translatable\Test\Model\CountryWithCustomLocaleKey; +use Dimsav\Translatable\Test\Model\Food; class TranslatableTest extends TestsBase { @@ -365,4 +366,33 @@ public function test_config_overrides_apps_locale() $this->assertSame('Griechenland', $country->name); } + + public function test_locales_as_array_keys_are_properly_detected() + { + $this->app->config->set('translatable.locales', ['en' => ['US','GB']]); + + $data = [ + 'en' => ['name' => 'French fries'], + 'en-US' => ['name' => 'American french fries'], + 'en-GB' => ['name' => 'Chips'], + ]; + $frenchFries = Food::create($data); + + $this->assertSame('French fries', $frenchFries->getTranslation('en')->name); + $this->assertSame('Chips', $frenchFries->getTranslation('en-GB')->name); + $this->assertSame('American french fries', $frenchFries->getTranslation('en-US')->name); + } + + public function test_locale_separator_can_be_configured() + { + $this->app->config->set('translatable.locales', ['en' => ['GB']]); + $this->app->config->set('translatable.locale_separator', '_'); + $data = [ + 'en_GB' => ['name' => 'Chips'], + ]; + $frenchFries = Food::create($data); + + $this->assertSame('Chips', $frenchFries->getTranslation('en_GB')->name); + } + } diff --git a/tests/migrations/2013_11_28_152610_create_tables.php b/tests/migrations/2013_11_28_152610_create_tables.php index b50ef94..d53b6eb 100644 --- a/tests/migrations/2013_11_28_152610_create_tables.php +++ b/tests/migrations/2013_11_28_152610_create_tables.php @@ -56,6 +56,21 @@ public function up() $table->timestamps(); }); + Schema::create('foods', function (Blueprint $table) { + $table->increments('id'); + $table->timestamps(); + }); + + Schema::create('food_translations', function (Blueprint $table) { + $table->increments('id'); + $table->integer('food_id')->unsigned(); + $table->string('name'); + $table->string('locale')->index(); + + $table->unique(['food_id', 'locale']); + $table->foreign('food_id')->references('id')->on('foods')->onDelete('cascade'); + }); + Schema::create('continent_translations', function (Blueprint $table) { $table->increments('id'); $table->integer('continent_id')->unsigned(); @@ -80,5 +95,7 @@ public function down() Schema::dropIfExists('continent_translations'); Schema::dropIfExists('continents'); + Schema::dropIfExists('food_translations'); + Schema::dropIfExists('foods'); } } diff --git a/tests/models/Food.php b/tests/models/Food.php new file mode 100644 index 0000000..400a13a --- /dev/null +++ b/tests/models/Food.php @@ -0,0 +1,49 @@ + CountryTranslation + */ + public $translationModel; + + /** + * This is the foreign key used to define the translation relationship. + * Set this if you want to overwrite the laravel default for foreign keys. + * + * @var + */ + public $translationForeignKey; + + /** + * Add your translated attributes here if you want + * fill them with mass assignment. + * + * @var array + */ + public $fillable = ['name']; + + /** + * The database field being used to define the locale parameter in the translation model. + * Defaults to 'locale'. + * + * @var string + */ + public $localeKey; +} diff --git a/tests/models/FoodTranslation.php b/tests/models/FoodTranslation.php new file mode 100644 index 0000000..b60ea3d --- /dev/null +++ b/tests/models/FoodTranslation.php @@ -0,0 +1,8 @@ +