Introduced I18n category constants (#1894)

Replaced all hard coded category values
This commit is contained in:
func0der 2014-03-23 12:47:35 +01:00
parent 57b8008dbe
commit 06f47ee01f
3 changed files with 96 additions and 30 deletions

View file

@ -87,6 +87,68 @@ class I18n {
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
);
/**
* Constants for the translation categories.
*
* The constants may be used in translation fetching
* instead of hardcoded integers.
* Example:
* {{{
* I18n::translate('CakePHP is awesome.', null, null, I18n::LC_MESSAGES)
* }}}
*
* To keep the code more readable, I18n constants are preferred over
* hardcoded integers.
*/
/**
* Constant for LC_ALL.
*
* @var int
*/
const LC_ALL = 0;
/**
* Constant for LC_COLLATE.
*
* @var int
*/
const LC_COLLATE = 1;
/**
* Constant for LC_CTYPE.
*
* @var int
*/
const LC_CTYPE = 2;
/**
* Constant for LC_MONETARY.
*
* @var int
*/
const LC_MONETARY = 3;
/**
* Constant for LC_NUMERIC.
*
* @var int
*/
const LC_NUMERIC = 4;
/**
* Constant for LC_TIME.
*
* @var int
*/
const LC_TIME = 5;
/**
* Constant for LC_MESSAGES.
*
* @var int
*/
const LC_MESSAGES = 6;
/**
* Escape string
*
@ -129,7 +191,7 @@ class I18n {
* @return string translated string.
* @throws CakeException When '' is provided as a domain.
*/
public static function translate($singular, $plural = null, $domain = null, $category = 6, $count = null, $language = null) {
public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES, $count = null, $language = null) {
$_this = I18n::getInstance();
if (strpos($singular, "\r\n") !== false) {

View file

@ -1760,8 +1760,12 @@ class I18nTest extends CakeTestCase {
*/
public function testCategory() {
Configure::write('Config.language', 'po');
// Test with default (I18n constant) category.
$category = $this->_category();
$this->assertEquals('Monetary Po (translated)', $category);
// Test with category number represenation.
$category = $this->_category(3);
$this->assertEquals('Monetary Po (translated)', $category);
}
/**
@ -1844,11 +1848,11 @@ class I18nTest extends CakeTestCase {
public function testTranslateLanguageParam() {
Configure::write('Config.language', 'rule_0_po');
$result = I18n::translate('Plural Rule 1', null, null, 6);
$result = I18n::translate('Plural Rule 1', null, null, I18n::LC_MESSAGES);
$expected = 'Plural Rule 0 (translated)';
$this->assertEquals($expected, $result);
$result = I18n::translate('Plural Rule 1', null, null, 6, null, 'rule_1_po');
$result = I18n::translate('Plural Rule 1', null, null, I18n::LC_MESSAGES, null, 'rule_1_po');
$expected = 'Plural Rule 1 (translated)';
$this->assertEquals($expected, $result);
}
@ -1926,7 +1930,7 @@ class I18nTest extends CakeTestCase {
*
* @return void
*/
protected function _category($category = 3) {
protected function _category($category = I18n::LC_MONETARY) {
$singular = __c('Plural Rule 1', $category);
return $singular;
}

View file

@ -585,7 +585,7 @@ if (!function_exists('__n')) {
}
App::uses('I18n', 'I18n');
$translated = I18n::translate($singular, $plural, null, 6, $count);
$translated = I18n::translate($singular, $plural, null, I18n::LC_MESSAGES, $count);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
@ -647,7 +647,7 @@ if (!function_exists('__dn')) {
return;
}
App::uses('I18n', 'I18n');
$translated = I18n::translate($singular, $plural, $domain, 6, $count);
$translated = I18n::translate($singular, $plural, $domain, I18n::LC_MESSAGES, $count);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
@ -669,15 +669,15 @@ if (!function_exists('__dc')) {
* The category argument allows a specific category of the locale settings to be used for fetching a message.
* Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
*
* Note that the category must be specified with a numeric value, instead of the constant name. The values are:
* Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
*
* - LC_ALL 0
* - LC_COLLATE 1
* - LC_CTYPE 2
* - LC_MONETARY 3
* - LC_NUMERIC 4
* - LC_TIME 5
* - LC_MESSAGES 6
* - LC_ALL I18n::LC_ALL
* - LC_COLLATE I18n::LC_COLLATE
* - LC_CTYPE I18n::LC_CTYPE
* - LC_MONETARY I18n::LC_MONETARY
* - LC_NUMERIC I18n::LC_NUMERIC
* - LC_TIME I18n::LC_TIME
* - LC_MESSAGES I18n::LC_MESSAGES
*
* @param string $domain Domain
* @param string $msg Message to translate
@ -715,15 +715,15 @@ if (!function_exists('__dcn')) {
* The category argument allows a specific category of the locale settings to be used for fetching a message.
* Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
*
* Note that the category must be specified with a numeric value, instead of the constant name. The values are:
* Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
*
* - LC_ALL 0
* - LC_COLLATE 1
* - LC_CTYPE 2
* - LC_MONETARY 3
* - LC_NUMERIC 4
* - LC_TIME 5
* - LC_MESSAGES 6
* - LC_ALL I18n::LC_ALL
* - LC_COLLATE I18n::LC_COLLATE
* - LC_CTYPE I18n::LC_CTYPE
* - LC_MONETARY I18n::LC_MONETARY
* - LC_NUMERIC I18n::LC_NUMERIC
* - LC_TIME I18n::LC_TIME
* - LC_MESSAGES I18n::LC_MESSAGES
*
* @param string $domain Domain
* @param string $singular Singular string to translate
@ -758,15 +758,15 @@ if (!function_exists('__c')) {
* The category argument allows a specific category of the locale settings to be used for fetching a message.
* Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
*
* Note that the category must be specified with a numeric value, instead of the constant name. The values are:
* Note that the category must be specified with a class constant of I18n, instead of the constant name. The values are:
*
* - LC_ALL 0
* - LC_COLLATE 1
* - LC_CTYPE 2
* - LC_MONETARY 3
* - LC_NUMERIC 4
* - LC_TIME 5
* - LC_MESSAGES 6
* - LC_ALL I18n::LC_ALL
* - LC_COLLATE I18n::LC_COLLATE
* - LC_CTYPE I18n::LC_CTYPE
* - LC_MONETARY I18n::LC_MONETARY
* - LC_NUMERIC I18n::LC_NUMERIC
* - LC_TIME I18n::LC_TIME
* - LC_MESSAGES I18n::LC_MESSAGES
*
* @param string $msg String to translate
* @param integer $category Category