mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Adding tests to L10n and refactoring to be more readable.
Removing duplicated entry of es-mx. Fixes #5087. git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7712 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
acce4fa54e
commit
e184838451
2 changed files with 194 additions and 50 deletions
|
@ -233,7 +233,6 @@ class L10n extends Object {
|
|||
'es-gt' => array('language' => 'Spanish (Guatemala)', 'locale' => 'es_gt', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
'es-hn' => array('language' => 'Spanish (Honduras)', 'locale' => 'es_hn', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
'es-mx' => array('language' => 'Spanish (Mexican)', 'locale' => 'es_mx', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
'es_mx' => array('language' => 'Spanish (Mexican)', 'locale' => 'es_mx', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
'es-ni' => array('language' => 'Spanish (Nicaragua)', 'locale' => 'es_ni', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
'es-pa' => array('language' => 'Spanish (Panama)', 'locale' => 'es_pa', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
'es-pe' => array('language' => 'Spanish (Peru)', 'locale' => 'es_pe', 'localeFallback' => 'spa', 'charset' => 'utf-8'),
|
||||
|
@ -336,10 +335,10 @@ class L10n extends Object {
|
|||
* @access public
|
||||
*/
|
||||
function get($language = null) {
|
||||
if (!is_null($language)) {
|
||||
if ($language !== null) {
|
||||
return $this->__setLanguage($language);
|
||||
} elseif ($this->__autoLanguage() === false) {
|
||||
return $this->__setLanguage($language);
|
||||
return $this->__setLanguage();
|
||||
}
|
||||
}
|
||||
/**
|
||||
|
@ -350,36 +349,35 @@ class L10n extends Object {
|
|||
* @access private
|
||||
*/
|
||||
function __setLanguage($language = null) {
|
||||
if ((!is_null($language)) && (isset($this->__l10nMap[$language])) && (isset($this->__l10nCatalog[$this->__l10nMap[$language]]))) {
|
||||
$this->language = $this->__l10nCatalog[$this->__l10nMap[$language]]['language'];
|
||||
$this->languagePath = array(0 => $this->__l10nCatalog[$this->__l10nMap[$language]]['locale'],
|
||||
1 => $this->__l10nCatalog[$this->__l10nMap[$language]]['localeFallback']);
|
||||
$this->lang = $language;
|
||||
$this->locale = $this->__l10nCatalog[$this->__l10nMap[$language]]['locale'];
|
||||
$this->charset = $this->__l10nCatalog[$this->__l10nMap[$language]]['charset'];
|
||||
$langKey = null;
|
||||
if ($language !== null && isset($this->__l10nMap[$language]) && isset($this->__l10nCatalog[$this->__l10nMap[$language]])) {
|
||||
$langKey = $this->__l10nMap[$language];
|
||||
} else if ($language !== null && isset($this->__l10nCatalog[$language])) {
|
||||
$langKey = $language;
|
||||
} else if (defined('DEFAULT_LANGUAGE')) {
|
||||
$langKey = DEFAULT_LANGUAGE;
|
||||
}
|
||||
|
||||
} elseif ((!is_null($language)) && (isset($this->__l10nCatalog[$language]))) {
|
||||
$this->language = $this->__l10nCatalog[$language]['language'];
|
||||
$this->languagePath = array(0 => $this->__l10nCatalog[$language]['locale'],
|
||||
1 => $this->__l10nCatalog[$language]['localeFallback']);
|
||||
if ($langKey !== null && isset($this->__l10nCatalog[$langKey])) {
|
||||
$this->language = $this->__l10nCatalog[$langKey]['language'];
|
||||
$this->languagePath = array(
|
||||
$this->__l10nCatalog[$langKey]['locale'],
|
||||
$this->__l10nCatalog[$langKey]['localeFallback']
|
||||
);
|
||||
$this->lang = $language;
|
||||
$this->locale = $this->__l10nCatalog[$language]['locale'];
|
||||
$this->charset = $this->__l10nCatalog[$language]['charset'];
|
||||
|
||||
} elseif (defined('DEFAULT_LANGUAGE')) {
|
||||
$this->language = $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['language'];
|
||||
$this->languagePath = array(0 => $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['locale'],
|
||||
1 => $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['localeFallback']);
|
||||
$this->lang = DEFAULT_LANGUAGE;
|
||||
$this->locale = $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['locale'];
|
||||
$this->charset = $this->__l10nCatalog[$this->__l10nMap[DEFAULT_LANGUAGE]]['charset'];
|
||||
$this->locale = $this->__l10nCatalog[$langKey]['locale'];
|
||||
$this->charset = $this->__l10nCatalog[$langKey]['charset'];
|
||||
} else {
|
||||
$this->lang = $language;
|
||||
$this->languagePath = array(0 => $language);
|
||||
$this->languagePath = array($language);
|
||||
}
|
||||
|
||||
if ($this->default) {
|
||||
$this->languagePath[2] = $this->__l10nCatalog[$this->__l10nMap[$this->default]]['localeFallback'];
|
||||
if (isset($this->__l10nMap[$this->default]) && isset($this->__l10nCatalog[$this->__l10nMap[$this->default]])) {
|
||||
$this->languagePath[] = $this->__l10nCatalog[$this->__l10nMap[$this->default]]['localeFallback'];
|
||||
} else if (isset($this->__l10nCatalog[$this->default])) {
|
||||
$this->languagePath[] = $this->__l10nCatalog[$this->default]['localeFallback'];
|
||||
}
|
||||
}
|
||||
$this->found = true;
|
||||
|
||||
|
@ -387,6 +385,7 @@ class L10n extends Object {
|
|||
Configure::write('Config.language', $this->lang);
|
||||
}
|
||||
Configure::write('charset', $this->charset);
|
||||
|
||||
if ($language) {
|
||||
return $language;
|
||||
}
|
||||
|
@ -398,24 +397,35 @@ class L10n extends Object {
|
|||
* @access private
|
||||
*/
|
||||
function __autoLanguage() {
|
||||
$_detectableLanguages = split ('[,;]', env('HTTP_ACCEPT_LANGUAGE'));
|
||||
$_detectableLanguages = split('[,;]', env('HTTP_ACCEPT_LANGUAGE'));
|
||||
foreach ($_detectableLanguages as $key => $langKey) {
|
||||
$langKey = strtolower($langKey);
|
||||
if (isset($this->__l10nCatalog[$langKey])) {
|
||||
if (strpos($langKey, '_') !== false) {
|
||||
$langKey = str_replace('_', '-', $langKey);
|
||||
}
|
||||
|
||||
if (isset($this->__l10nCatalog[$langKey])) {
|
||||
$this->language = $this->__l10nCatalog[$langKey]['language'];
|
||||
$this->languagePath = array(0 => $this->__l10nCatalog[$langKey]['locale'],
|
||||
1 => $this->__l10nCatalog[$langKey]['localeFallback']);
|
||||
$this->languagePath = array(
|
||||
$this->__l10nCatalog[$langKey]['locale'],
|
||||
$this->__l10nCatalog[$langKey]['localeFallback']
|
||||
);
|
||||
$this->lang = $langKey;
|
||||
$this->locale = $this->__l10nCatalog[$langKey]['locale'];
|
||||
$this->charset = $this->__l10nCatalog[$langKey]['charset'];
|
||||
|
||||
if ($this->default) {
|
||||
$this->languagePath[2] = $this->__l10nCatalog[$this->__l10nMap[$this->default]]['localeFallback'];
|
||||
if (isset($this->__l10nMap[$this->default]) && isset($this->__l10nCatalog[$this->__l10nMap[$this->default]])) {
|
||||
$this->languagePath[] = $this->__l10nCatalog[$this->__l10nMap[$this->default]]['localeFallback'];
|
||||
} else if (isset($this->__l10nCatalog[$this->default])) {
|
||||
$this->languagePath[] = $this->__l10nCatalog[$this->default]['localeFallback'];
|
||||
}
|
||||
}
|
||||
$this->found = true;
|
||||
|
||||
Configure::write('Config.language', $this->lang);
|
||||
Configure::write('charset', $this->charset);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -437,17 +447,11 @@ class L10n extends Object {
|
|||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (is_string($mixed)) {
|
||||
if (2 == strlen($mixed)) {
|
||||
if (in_array($mixed, $this->__l10nMap)) {
|
||||
return array_search($mixed, $this->__l10nMap);
|
||||
}
|
||||
} else {
|
||||
if (isset($this->__l10nMap[$mixed])) {
|
||||
return $this->__l10nMap[$mixed];
|
||||
}
|
||||
} else if (is_string($mixed)) {
|
||||
if (strlen($mixed) === 2 && in_array($mixed, $this->__l10nMap)) {
|
||||
return array_search($mixed, $this->__l10nMap);
|
||||
} else if (isset($this->__l10nMap[$mixed])) {
|
||||
return $this->__l10nMap[$mixed];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -468,9 +472,7 @@ class L10n extends Object {
|
|||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (is_string($language)) {
|
||||
} else if (is_string($language)) {
|
||||
if (isset($this->__l10nCatalog[$language])) {
|
||||
return $this->__l10nCatalog[$language];
|
||||
}
|
||||
|
|
|
@ -28,20 +28,162 @@
|
|||
*/
|
||||
App::import('Core', 'l10n');
|
||||
/**
|
||||
* Short description for class.
|
||||
* L10nTest class
|
||||
*
|
||||
* @package cake.tests
|
||||
* @subpackage cake.tests.cases.libs
|
||||
*/
|
||||
class L10nTest extends CakeTestCase {
|
||||
/**
|
||||
* skip method
|
||||
*
|
||||
* testGet method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function skip() {
|
||||
$this->skipif (true, 'L10nTest not implemented');
|
||||
function testGet() {
|
||||
$l10n =& new L10n();
|
||||
|
||||
// Catalog Entry
|
||||
$l10n->get('en');
|
||||
$result = $l10n->language;
|
||||
$expected = 'English';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('eng', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'eng';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
// Map Entry
|
||||
$l10n->get('eng');
|
||||
$result = $l10n->language;
|
||||
$expected = 'English';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('eng', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'eng';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
// Catalog Entry
|
||||
$l10n->get('en-ca');
|
||||
$result = $l10n->language;
|
||||
$expected = 'English (Canadian)';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('en_ca', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'en_ca';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
// Default Entry
|
||||
define('DEFAULT_LANGUAGE', 'en-us');
|
||||
|
||||
$l10n->get('use_default');
|
||||
$result = $l10n->language;
|
||||
$expected = 'English (United States)';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('en_us', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'en_us';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
// Using $this->default
|
||||
$l10n = new L10n();
|
||||
$l10n->get('use_default');
|
||||
$result = $l10n->language;
|
||||
$expected = 'English (United States)';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('en_us', 'eng', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'en_us';
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testGetAutoLanguage method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testGetAutoLanguage() {
|
||||
$__SERVER = $_SERVER;
|
||||
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
|
||||
|
||||
$l10n =& new L10n();
|
||||
$l10n->get();
|
||||
$result = $l10n->language;
|
||||
$expected = 'English (Canadian)';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('en_ca', 'eng', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'en_ca';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx';
|
||||
$l10n->get();
|
||||
$result = $l10n->language;
|
||||
$expected = 'Spanish (Mexican)';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->languagePath;
|
||||
$expected = array('es_mx', 'spa', 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$result = $l10n->locale;
|
||||
$expected = 'es_mx';
|
||||
$this->assertEqual($result, $expected);
|
||||
|
||||
$_SERVER = $__SERVER;
|
||||
}
|
||||
/**
|
||||
* testMap method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testMap() {
|
||||
$l10n =& new L10n();
|
||||
|
||||
$result = $l10n->map(array('eng', 'en', 'en-us'));
|
||||
$expected = array('eng' => 'en', 'en' => 'eng');
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
/**
|
||||
* testCatalog method
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
function testCatalog() {
|
||||
$l10n =& new L10n();
|
||||
|
||||
$result = $l10n->catalog(array('eng', 'en', 'en-us'));
|
||||
$expected = array(
|
||||
'en' => array('language' => 'English', 'locale' => 'eng', 'localeFallback' => 'eng', 'charset' => 'utf-8'),
|
||||
'en-us' => array('language' => 'English (United States)', 'locale' => 'en_us', 'localeFallback' => 'eng', 'charset' => 'utf-8')
|
||||
);
|
||||
$this->assertEqual($result, $expected);
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue