Skip to content

Commit

Permalink
Merge pull request #18 from ARCANEDEV/develop
Browse files Browse the repository at this point in the history
Adding Regional to locales
  • Loading branch information
arcanedev-maroc committed Nov 29, 2015
2 parents 66c5e10 + cf16be2 commit 8ed06ff
Show file tree
Hide file tree
Showing 15 changed files with 2,144 additions and 1,784 deletions.
3,705 changes: 1,997 additions & 1,708 deletions config/localization.php

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/Contracts/LocalizationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ public function getCurrentLocaleDirection();
*/
public function getCurrentLocaleNative();

/**
* Returns current locale regional.
*
* @return string
*/
public function getCurrentLocaleRegional();

/**
* Set and return current locale.
*
Expand Down
60 changes: 55 additions & 5 deletions src/Entities/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class Locale implements Arrayable, Jsonable, JsonSerializable
*/
private $native;

/**
* Locale regional.
*
* @var string
*/
private $regional;

/**
* Default locale.
*
Expand All @@ -75,6 +82,7 @@ public function __construct($key, array $data)
$this->setScript($data['script']);
$this->setDirection($data['dir']);
$this->setNative($data['native']);
$this->setRegional(isset($data['regional']) ? $data['regional'] : '');
}

/* ------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -210,6 +218,30 @@ private function setNative($native)
return $this;
}

/**
* Get locale regional.
*
* @return string
*/
public function regional()
{
return $this->regional;
}

/**
* Set Regional.
*
* @param string $regional
*
* @return self
*/
private function setRegional($regional)
{
$this->regional = $regional;

return $this;
}

/**
* Check if it is a default locale.
*
Expand All @@ -232,6 +264,23 @@ private function setDefault()
return $this;
}

/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Create Locale instance.
*
* @param string $key
* @param array $data
*
* @return self
*/
public static function make($key, array $data)
{
return new self($key, $data);
}

/* ------------------------------------------------------------------------------------------------
| Other Functions
| ------------------------------------------------------------------------------------------------
Expand All @@ -244,11 +293,12 @@ private function setDefault()
public function toArray()
{
return [
'key' => $this->key(),
'name' => $this->name(),
'script' => $this->script(),
'dir' => $this->direction(),
'native' => $this->native(),
'key' => $this->key(),
'name' => $this->name(),
'script' => $this->script(),
'dir' => $this->direction(),
'native' => $this->native(),
'regional' => $this->regional(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Entities/LocaleCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function loadFromArray(array $locales)
$this->reset();

foreach ($locales as $key => $locale) {
$this->put($key, new Locale($key, $locale));
$this->put($key, Locale::make($key, $locale));
}

return $this;
Expand Down
10 changes: 10 additions & 0 deletions src/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ public function getCurrentLocaleNative()
return $this->getCurrentLocaleEntity()->native();
}

/**
* Returns current locale regional.
*
* @return string
*/
public function getCurrentLocaleRegional()
{
return $this->getCurrentLocaleEntity()->regional();
}

/**
* Get all locales.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Utilities/LocalesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function setLocale($locale = null)
}

$this->app->setLocale($this->getCurrentLocale());
$this->updateRegional();

return $locale;
}
Expand Down Expand Up @@ -371,4 +372,16 @@ private function filterLocales(array $supportedLocales)
return in_array($locale->key(), $supportedLocales);
});
}

/**
* Update locale regional.
*/
private function updateRegional()
{
$currentLocale = $this->getCurrentLocaleEntity();

if ( ! empty($regional = $currentLocale->regional())) {
setlocale(LC_TIME, $regional . '.utf8');
}
}
}
4 changes: 2 additions & 2 deletions tests/Entities/LocaleCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public function setUp()

public function tearDown()
{
parent::tearDown();
unset($this->locales);

unset($locales);
parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
Expand Down
57 changes: 37 additions & 20 deletions tests/Entities/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function setUp()

public function tearDown()
{
parent::tearDown();

unset($this->locale);

parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
Expand All @@ -50,6 +50,8 @@ public function it_can_be_instantiated()
$this->assertEquals('Latin', $this->locale->script());
$this->assertEquals('ltr', $this->locale->direction());
$this->assertEquals('English', $this->locale->native());
$this->assertEquals('en_GB', $this->locale->regional());

$this->assertTrue($this->locale->isDefault());
}

Expand Down Expand Up @@ -96,33 +98,48 @@ public function it_can_convert_entity_to_json()
| Other Functions
| ------------------------------------------------------------------------------------------------
*/
/**
* Make a locale.
*
* @param string $key
*
* @return Locale
*/
private function makeLocale($key)
{
$data = $this->getLocale($key);

return new Locale($key, $data);
return Locale::make($key, $this->getLocale($key));
}

/**
* Get locale data.
*
* @param string $key
*
* @return array
*/
private function getLocale($key)
{
return array_get([
'ar' => [
'name' => 'Arabic',
'script' => 'Arab',
'dir' => 'rtl',
'native' => 'العربية',
'ar' => [
'name' => 'Arabic',
'script' => 'Arab',
'dir' => 'rtl',
'native' => 'العربية',
'regional' => 'ar_AE',
],
'en' => [
'name' => 'English',
'script' => 'Latin',
'dir' => 'ltr',
'native' => 'English',
'en' => [
'name' => 'English',
'script' => 'Latin',
'dir' => 'ltr',
'native' => 'English',
'regional' => 'en_GB',
],
'fr' => [
'name' => 'French',
'script' => 'Latin',
'dir' => 'ltr',
'native' => 'Français',
'fr' => [
'name' => 'French',
'script' => 'Latin',
'dir' => 'ltr',
'native' => 'Français',
'regional' => 'fr_FR',
],
], $key);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,22 @@ public function it_can_get_current_locale_native()
}
}

/** @test */
public function testGetCurrentLocaleRegional()
{
$locales = [
'en' => 'en_GB',
'es' => 'es_ES',
'fr' => 'fr_FR',
];

foreach ($locales as $locale => $regional) {
$this->refreshApplication($locale);

$this->assertEquals($regional, localization()->getCurrentLocaleRegional());
}
}

/** @test */
public function it_can_create_url_from_uri()
{
Expand Down
14 changes: 0 additions & 14 deletions tests/Middleware/LocaleCookieRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
*/
class LocaleCookieRedirectTest extends TestCase
{
/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
public function setUp()
{
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
| Test Functions
| ------------------------------------------------------------------------------------------------
Expand Down
14 changes: 0 additions & 14 deletions tests/Middleware/LocaleSessionRedirectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@
*/
class LocaleSessionRedirectTest extends TestCase
{
/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
public function setUp()
{
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
| Test Functions
| ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public function setUp()

public function tearDown()
{
parent::tearDown();

unset($this->router);

parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/Utilities/LocalesManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function setUp()

public function tearDown()
{
parent::tearDown();

unset($this->localesManager);

parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions tests/Utilities/NegotiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function setUp()

public function tearDown()
{
parent::tearDown();

unset($this->negotiator);

parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
Expand Down
14 changes: 0 additions & 14 deletions tests/Utilities/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@
*/
class UrlTest extends TestCase
{
/* ------------------------------------------------------------------------------------------------
| Main Functions
| ------------------------------------------------------------------------------------------------
*/
public function setUp()
{
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
}

/* ------------------------------------------------------------------------------------------------
| Test Functions
| ------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 8ed06ff

Please sign in to comment.