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

Commit

Permalink
Enabled country based locales.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimsav committed Oct 22, 2015
1 parent 3afa42a commit ef14288
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 6 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
26 changes: 22 additions & 4 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*/
Expand Down
21 changes: 20 additions & 1 deletion src/config/translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '-',

/*
|--------------------------------------------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

}
17 changes: 17 additions & 0 deletions tests/migrations/2013_11_28_152610_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -80,5 +95,7 @@ public function down()

Schema::dropIfExists('continent_translations');
Schema::dropIfExists('continents');
Schema::dropIfExists('food_translations');
Schema::dropIfExists('foods');
}
}
49 changes: 49 additions & 0 deletions tests/models/Food.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php namespace Dimsav\Translatable\Test\Model;

use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model as Eloquent;

class Food extends Eloquent
{
use Translatable;

/**
* Array with the fields translated in the Translation table.
*
* @var array
*/
public $translatedAttributes = ['name'];

/**
* Set $translationModel if you want to overwrite the convention
* for the name of the translation Model. Use full namespace if applied.
*
* The convention is to add "Translation" to the name of the class extending Translatable.
* Example: Country => 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;
}
8 changes: 8 additions & 0 deletions tests/models/FoodTranslation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php namespace Dimsav\Translatable\Test\Model;

use Illuminate\Database\Eloquent\Model as Eloquent;

class FoodTranslation extends Eloquent
{
public $timestamps = false;
}

0 comments on commit ef14288

Please sign in to comment.