Skip to content

Commit

Permalink
Allow setting of rendered locales
Browse files Browse the repository at this point in the history
  • Loading branch information
Propaganistas committed Sep 8, 2016
1 parent 94a1f8f commit 1a394ea
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/LocaleResolverTrait.php
Expand Up @@ -13,6 +13,20 @@ trait LocaleResolverTrait
*/
protected $definitionPath;

/**
* The default locale.
*
* @var string
*/
protected $defaultLocale = 'en';

/**
* The fallback locale.
*
* @var string
*/
protected $fallbackLocale = null;

/**
* Common locale aliases.
*
Expand Down Expand Up @@ -91,6 +105,7 @@ protected function resolveLocale($locale = null, $fallbackLocale = null)
// List all possible variants (i.e. en-US gives "en-US" and "en").
$localeVariants = $this->getLocaleVariants($locale);
// A fallback locale was provided, add it to the end of the chain.
$fallbackLocale = $fallbackLocale ?: $this->getFallbackLocale();
if (isset($fallbackLocale)) {
$localeVariants[] = $fallbackLocale;
}
Expand Down Expand Up @@ -168,9 +183,39 @@ protected function canonicalizeLocale($locale = null)
*
* @return string The default locale.
*/
protected function getDefaultLocale()
public function getDefaultLocale()
{
return $this->defaultLocale;
}

/**
* Sets the default locale.
*
* @return void
*/
public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}

/**
* Gets the fallback locale.
*
* @return string The fallback locale.
*/
public function getFallbackLocale()
{
return $this->fallbackLocale;
}

/**
* Sets the fallback locale.
*
* @return void
*/
public function setFallbackLocale($locale)
{
return 'en';
$this->fallbackLocale = $locale;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/LocaleResolverTest.php
Expand Up @@ -25,6 +25,34 @@ public function setUp()
$this->repository = new DummyRepository();
}

/**
* @covers ::getDefaultLocale
* @covers ::setDefaultLocale
*
* @uses \CommerceGuys\Intl\LocaleResolverTrait::getDefaultLocale
* @uses \CommerceGuys\Intl\LocaleResolverTrait::setDefaultLocale
*/
public function testDefaultLocale()
{
$this->assertEquals('en', $this->repository->getDefaultLocale());
$this->repository->setDefaultLocale('fr');
$this->assertEquals('fr', $this->repository->getDefaultLocale());
}

/**
* @covers ::getFallbackLocale
* @covers ::setFallbackLocale
*
* @uses \CommerceGuys\Intl\LocaleResolverTrait::getFallbackLocale
* @uses \CommerceGuys\Intl\LocaleResolverTrait::setFallbackLocale
*/
public function testFallbackLocale()
{
$this->assertNull($this->repository->getFallbackLocale());
$this->repository->setFallbackLocale('en');
$this->assertEquals('en', $this->repository->getFallbackLocale());
}

/**
* @covers ::resolveLocale
* @covers ::getDefaultLocale
Expand Down

0 comments on commit 1a394ea

Please sign in to comment.