Skip to content

Commit

Permalink
removed locales from composition #1127
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Mar 7, 2017
1 parent d592b2f commit b9ae81c
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 70 deletions.
60 changes: 58 additions & 2 deletions core/traits/ApplicationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,78 @@ trait ApplicationTrait
* ```
*/
public $tags = [];

/**
* @var array Can override the localisation value used for php internal `setlocale()` method for specific language. For example
* the language is de but the it should use the locale charset `de_CH.utf` (locale -a will return all locales installed on the server)
* you can define them inside an array where key is the language and value the locale value to be used.
*
* ```php
* public $locales = [
* 'de' => 'de_CH.utf8',
* 'en' => 'en_GB.utf8',
* ];
* ```
*/
public $locales = [];

/**
* Add trace info to luya application trait
*/
public function init()
{
// call parent
parent::init();

// add trace info
Yii::trace('initialize LUYA Application', __METHOD__);

$this->setLocale($this->language);
}

/**
* Transform the $language into a locale sign to set php env settings.
*
* Example transform input `de` to `de_CH` when available $locales property.
*
* @param string $lang Find the locale POSIX for the provided $lang short code.
* @return string The localisation code for the provided lang short code.
*/
public function ensureLocale($lang)
{
if (array_key_exists($lang, $this->locales)) {
return $this->locales[$lang];
}

switch ($lang) {
case 'de':
return 'de_DE';
case 'fr':
return 'fr_FR';
case 'it':
return 'it_IT';
case 'ru':
return 'ru_RU';
default:
return 'en_EN';
}
}

/**
* Setter method ensures the locilations POSIX from {{ensureLocale}} for the provided lang
* and changes the Yii::$app->langauge and sets the `setlocale()` code from ensureLocale().
*
* @param string $lang The language short code to set the locale for.
*/
public function setLocale($lang)
{
$this->language = $lang;
setlocale(LC_ALL, $this->ensureLocale($lang).'.utf8', $this->ensureLocale($lang));
}

/**
* Read only property which is used in cli bootstrap process to set the @webroot alias
*
* ```
* ```php
* Yii::setAlias('@webroot', $app->webroot);
* ```
*/
Expand Down
64 changes: 13 additions & 51 deletions core/web/Composition.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Composition extends Component implements \ArrayAccess
* @var string This event will method will triggere after setKey method is proccessed
*/
const EVENT_AFTER_SET = 'EVENT_AFTER_SET';

/**
* @var string The Regular-Expression matching the var finder inside the url parts
*/
Expand All @@ -41,18 +41,6 @@ class Composition extends Component implements \ArrayAccess
*/
public $hidden = false;

/**
* @var array Can override the localisation value used for php internal `setlocale()` method for specific language. For example
* the language is de but the it should use the locale charset `de_CH.utf` (locale -a will return all locales installed on the server)
* you can define them inside an array where key is the language and value the locale value to be used.
*
* ```php
* public $local = [
* 'de' => 'de_CH.utf',
* ];
*/
public $locales = [];

/**
* @var string Url matching prefix, which is used for all the modules (e.g. an e-store requireds a language
* as the cms needs this informations too). After proccessing this informations, they will be removed
Expand Down Expand Up @@ -88,12 +76,12 @@ class Composition extends Component implements \ArrayAccess
* The above configuration must be defined in your compostion componeont configuration in your config file.
*/
public $hostInfoMapping = [];

/**
* @var array Read-Only property, contains all composition key value paringins
*/
private $_composition = [];

private $_compositionKeys = [];

/**
Expand Down Expand Up @@ -124,16 +112,16 @@ public function getDefaultLangShortCode()
public function init()
{
parent::init();

// check if the required key langShortCode exist in the default array.
if (!array_key_exists('langShortCode', $this->default)) {
throw new Exception("The composition default rule must contain a langShortCode.");
}

if (array_key_exists($this->request->hostInfo, $this->hostInfoMapping)) {
$this->default = $this->hostInfoMapping[$this->request->hostInfo];
}

// atach event to component
$this->on(self::EVENT_AFTER_SET, [$this, 'eventAfterSet']);
// resolved data
Expand All @@ -155,11 +143,10 @@ public function init()
public function eventAfterSet($event)
{
if ($event->key == 'langShortCode') {
Yii::$app->language = $event->value;
setlocale(LC_ALL, $this->getLocale().'.utf8', $this->getLocale());
Yii::$app->setLocale($event->value);
}
}

/**
* Resolve the current url request and retun an array contain resolved route and the resolved values.
*
Expand Down Expand Up @@ -260,7 +247,7 @@ public function getFull()
{
return $this->createRouteEnsure();
}

/**
* create a route but ensures if composition is hidden anywho.
*
Expand All @@ -271,7 +258,7 @@ public function createRouteEnsure(array $overrideKeys = [])
{
return ($this->hidden) ? '' : $this->createRoute($overrideKeys);
}

/**
* Create compositon route based on the provided keys (to override), if no keys provided
* all the default values will be used.
Expand All @@ -282,13 +269,13 @@ public function createRouteEnsure(array $overrideKeys = [])
public function createRoute(array $overrideKeys = [])
{
$composition = $this->_composition;

foreach ($overrideKeys as $key => $value) {
if (in_array($key, $this->_compositionKeys)) {
$composition[$key] = $value;
}
}

return implode('/', $composition);
}

Expand All @@ -304,7 +291,7 @@ public function prependTo($route, $prefix = null)
if ($prefix === null) {
$prefix = $this->getFull();
}

if (empty($prefix)) {
return $route;
}
Expand All @@ -331,31 +318,6 @@ public function removeFrom($route)
return preg_replace("#$pattern#", '', $route, 1);
}

/**
* Transform the langShortCode into a locale sign to set php env settings.
*
* @return string
*/
public function getLocale()
{
if (array_key_exists($this->language, $this->locales)) {
return $this->locales[$this->language];
}

switch ($this->language) {
case 'de':
return 'de_DE';
case 'fr':
return 'fr_FR';
case 'it':
return 'it_IT';
case 'ru':
return 'ru_RU';
default:
return 'en_EN';
}
}

/**
* Wrapper for `getKey('langShortCode')` to load language to set php env settings.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/core/console/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class ApplicationTest extends LuyaConsoleTestCase
{
public function testInvalidCommandException()
{
$this->setExpectedException('yii\console\Exception');
$this->expectException('yii\console\Exception');
Yii::$app->runAction('luya/luya/luya');
}

public function testInvalidRouteCommand()
{
$this->setExpectedException('yii\console\Exception');
$this->expectException('yii\console\Exception');
Yii::$app->runAction('consolemodule/test-command/notavailable');
}

Expand Down
13 changes: 13 additions & 0 deletions tests/core/traits/ApplicationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ public function testCountFrontendModules()
{
$this->assertSame(3, count($this->trait->getFrontendModules()));
}

public function testLocalisation()
{
$app = Yii::$app;
// default
$this->assertContains('en_EN', $app->getLocale());

$app->locales = ['de' => 'de_CH.utf'];

$app->setLocale('de');

$this->assertEquals('de_CH.utf', $app->getLocale());
}
}
15 changes: 0 additions & 15 deletions tests/core/web/CompositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,4 @@ public function testRemoval()

$this->assertEquals('this-should/be-left', $comp->removeFrom('en/this-should/be-left'));
}

public function testLocalisation()
{
$request = new Request();
$comp = new Composition($request);

// default
$this->assertEquals('en_EN', $comp->getLocale());

$comp->locales = ['de' => 'de_CH.utf'];

$comp->setKey('langShortCode', 'de');

$this->assertEquals('de_CH.utf', $comp->getLocale());
}
}

0 comments on commit b9ae81c

Please sign in to comment.