Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Deprecated DEFAULT_LANGUAGE constant and config value 'Config.languag…

…e' is not used for default.

Cleaned up L10n::_setLanguage(). Removed unused L10n::$found
  • Loading branch information...
commit 18cd3729e4a05fea8738f93ff651ec9368708171 1 parent a54c92f
@ADmad ADmad authored
Showing with 83 additions and 50 deletions.
  1. +34 −35 lib/Cake/I18n/L10n.php
  2. +49 −15 lib/Cake/Test/Case/I18n/L10nTest.php
View
69 lib/Cake/I18n/L10n.php
@@ -39,7 +39,7 @@ class L10n {
*
* @var array
*/
- public $languagePath = array('eng');
+ public $languagePath = array('en_us', 'eng');
/**
* ISO 639-3 for current locale
@@ -56,9 +56,11 @@ class L10n {
public $locale = 'en_us';
/**
- * Default ISO 639-3 language.
+ * Default language.
*
- * DEFAULT_LANGUAGE is defined in an application this will be set as a fall back
+ * If config value 'Config.language' is set in an application this will be set
+ * as a fall back else if DEFAULT_LANGUAGE it defined it will be used.
+ * Constant DEFAULT_LANGUAGE has been deprecated in 2.4
*
* @var string
*/
@@ -79,13 +81,6 @@ class L10n {
public $direction = 'ltr';
/**
- * Set to true if a locale is found
- *
- * @var string
- */
- public $found = false;
-
-/**
* Maps ISO 639-3 to I10n::_l10nCatalog
* The terminological codes (first one per language) should be used if possible.
* They are the ones building the path in `/APP/Locale/[code]/`
@@ -337,6 +332,10 @@ public function __construct() {
if (defined('DEFAULT_LANGUAGE')) {
$this->default = DEFAULT_LANGUAGE;
}
+ $default = Configure::read('Config.language');
+ if ($default) {
+ $this->default = $default;
+ }
}
/**
@@ -360,44 +359,44 @@ public function get($language = null) {
/**
* Sets the class vars to correct values for $language.
- * If $language is null it will use the DEFAULT_LANGUAGE if defined
+ * If $language is null it will use the L10n::$default if defined
*
- * @param string $language Language (if null will use DEFAULT_LANGUAGE if defined)
+ * @param string $language Language (if null will use L10n::$default if defined)
* @return mixed
*/
protected function _setLanguage($language = null) {
- $langKey = null;
- if ($language !== null && isset($this->_l10nMap[$language]) && isset($this->_l10nCatalog[$this->_l10nMap[$language]])) {
- $langKey = $this->_l10nMap[$language];
- } elseif ($language !== null && isset($this->_l10nCatalog[$language])) {
- $langKey = $language;
- } elseif (defined('DEFAULT_LANGUAGE')) {
- $langKey = $language = DEFAULT_LANGUAGE;
+ $catalog = false;
+ if ($language !== null) {
+ $catalog = $this->catalog($language);
+ }
+
+ if (!$catalog && $this->default) {
+ $language = $this->default;
+ $catalog = $this->catalog($language);
}
- if ($langKey !== null && isset($this->_l10nCatalog[$langKey])) {
- $this->language = $this->_l10nCatalog[$langKey]['language'];
- $this->languagePath = array(
- $this->_l10nCatalog[$langKey]['locale'],
- $this->_l10nCatalog[$langKey]['localeFallback']
- );
+ if ($catalog) {
+ $this->language = $catalog['language'];
+ $this->languagePath = array_unique(array(
+ $catalog['locale'],
+ $catalog['localeFallback']
+ ));
$this->lang = $language;
- $this->locale = $this->_l10nCatalog[$langKey]['locale'];
- $this->charset = $this->_l10nCatalog[$langKey]['charset'];
- $this->direction = $this->_l10nCatalog[$langKey]['direction'];
- } else {
+ $this->locale = $catalog['locale'];
+ $this->charset = $catalog['charset'];
+ $this->direction = $catalog['direction'];
+ } elseif ($language) {
$this->lang = $language;
$this->languagePath = array($language);
}
- if ($this->default) {
- if (isset($this->_l10nMap[$this->default]) && isset($this->_l10nCatalog[$this->_l10nMap[$this->default]])) {
- $this->languagePath[] = $this->_l10nCatalog[$this->_l10nMap[$this->default]]['localeFallback'];
- } elseif (isset($this->_l10nCatalog[$this->default])) {
- $this->languagePath[] = $this->_l10nCatalog[$this->default]['localeFallback'];
+ if ($this->default && $language !== $this->default) {
+ $catalog = $this->catalog($this->default);
+ $fallback = $catalog['localeFallback'];
+ if (!in_array($fallback, $this->languagePath)) {
+ $this->languagePath[] = $fallback;
}
}
- $this->found = true;
if (Configure::read('Config.language') === null) {
Configure::write('Config.language', $this->lang);
View
64 lib/Cake/Test/Case/I18n/L10nTest.php
@@ -28,6 +28,16 @@
class L10nTest extends CakeTestCase {
/**
+ * setUp method
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ Configure::delete('Config.language');
+ }
+
+/**
* testGet method
*
* @return void
@@ -40,14 +50,14 @@ public function testGet() {
$this->assertEquals('en', $lang);
$this->assertEquals('English', $localize->language);
- $this->assertEquals(array('eng', 'eng'), $localize->languagePath);
+ $this->assertEquals(array('eng'), $localize->languagePath);
$this->assertEquals('eng', $localize->locale);
// Map Entry
$localize->get('eng');
$this->assertEquals('English', $localize->language);
- $this->assertEquals(array('eng', 'eng'), $localize->languagePath);
+ $this->assertEquals(array('eng'), $localize->languagePath);
$this->assertEquals('eng', $localize->locale);
// Catalog Entry
@@ -58,8 +68,7 @@ public function testGet() {
$this->assertEquals('en_ca', $localize->locale);
// Default Entry
- define('DEFAULT_LANGUAGE', 'en-us');
-
+ $localize->default = 'en-us';
$lang = $localize->get('use_default');
$this->assertEquals('en-us', $lang);
@@ -70,14 +79,6 @@ public function testGet() {
$localize->get('es');
$localize->get('');
$this->assertEquals('en-us', $localize->lang);
-
- // Using $this->default
- $localize = new L10n();
-
- $localize->get('use_default');
- $this->assertEquals('English (United States)', $localize->language);
- $this->assertEquals(array('en_us', 'eng', 'eng'), $localize->languagePath);
- $this->assertEquals('en_us', $localize->locale);
}
/**
@@ -94,7 +95,7 @@ public function testGetAutoLanguage() {
$this->assertEquals('en-ca', $lang);
$this->assertEquals('English (Canadian)', $localize->language);
- $this->assertEquals(array('en_ca', 'eng', 'eng'), $localize->languagePath);
+ $this->assertEquals(array('en_ca', 'eng'), $localize->languagePath);
$this->assertEquals('en_ca', $localize->locale);
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx';
@@ -102,20 +103,53 @@ public function testGetAutoLanguage() {
$this->assertEquals('es-mx', $lang);
$this->assertEquals('Spanish (Mexican)', $localize->language);
- $this->assertEquals(array('es_mx', 'spa', 'eng'), $localize->languagePath);
+ $this->assertEquals(array('es_mx', 'spa'), $localize->languagePath);
$this->assertEquals('es_mx', $localize->locale);
+ $localize = new L10n();
+ $localize->default = 'en-us';
+ $lang = $localize->get();
+ $this->assertEquals(array('es_mx', 'spa', 'eng'), $localize->languagePath);
+
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en_xy,en_ca';
$localize->get();
$this->assertEquals('English', $localize->language);
- $this->assertEquals(array('eng', 'eng', 'eng'), $localize->languagePath);
+ $this->assertEquals(array('eng'), $localize->languagePath);
$this->assertEquals('eng', $localize->locale);
$_SERVER = $serverBackup;
}
/**
+ * testGet method with deprecated constant DEFAULT_LANGUAGE
+ *
+ * @return void
+ */
+ public function testGetWithDeprecatedConstant() {
+ $this->skipIf(defined('DEFAULT_LANGUAGE'), 'Cannot re-define already defined constant.');
+
+ define('DEFAULT_LANGUAGE', 'en-us');
+ $localize = new L10n();
+
+ $lang = $localize->get('use_default');
+
+ $this->assertEquals('en-us', $lang);
+ $this->assertEquals('English (United States)', $localize->language);
+ $this->assertEquals(array('en_us', 'eng'), $localize->languagePath);
+ $this->assertEquals('en_us', $localize->locale);
+
+ $localize = new L10n();
+
+ $lang = $localize->get();
+
+ $this->assertEquals('en-us', $lang);
+ $this->assertEquals('English (United States)', $localize->language);
+ $this->assertEquals(array('en_us', 'eng'), $localize->languagePath);
+ $this->assertEquals('en_us', $localize->locale);
+ }
+
+/**
* testMap method
*
* @return void
Please sign in to comment.
Something went wrong with that request. Please try again.