Support to vsprintf in i18n aliases (__*() functions).

This commit is contained in:
Juan Basso 2010-12-04 23:34:59 -02:00
parent f4d4811c96
commit 30661ed154
2 changed files with 158 additions and 14 deletions

View file

@ -461,17 +461,24 @@ if (!function_exists('sortByKey')) {
* Returns a translated string if one is found; Otherwise, the submitted message. * Returns a translated string if one is found; Otherwise, the submitted message.
* *
* @param string $singular Text to translate * @param string $singular Text to translate
* @param mixed $args Array with arguments or multiple arguments in function
* @return mixed translated string * @return mixed translated string
* @link http://book.cakephp.org/view/1121/__ * @link http://book.cakephp.org/view/1121/__
*/ */
function __($singular) { function __($singular, $args = null) {
if (!$singular) { if (!$singular) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($singular); $translated = I18n::translate($singular);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 1);
}
return vsprintf($translated, $args);
} }
/** /**
@ -481,16 +488,23 @@ if (!function_exists('sortByKey')) {
* @param string $singular Singular text to translate * @param string $singular Singular text to translate
* @param string $plural Plural text * @param string $plural Plural text
* @param integer $count Count * @param integer $count Count
* @param mixed $args Array with arguments or multiple arguments in function
* @return mixed plural form of translated string * @return mixed plural form of translated string
*/ */
function __n($singular, $plural, $count) { function __n($singular, $plural, $count, $args = null) {
if (!$singular) { if (!$singular) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($singular, $plural, null, 6, $count); $translated = I18n::translate($singular, $plural, null, 6, $count);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
return vsprintf($translated, $args);
} }
/** /**
@ -498,16 +512,23 @@ if (!function_exists('sortByKey')) {
* *
* @param string $domain Domain * @param string $domain Domain
* @param string $msg String to translate * @param string $msg String to translate
* @param mixed $args Array with arguments or multiple arguments in function
* @return translated string * @return translated string
*/ */
function __d($domain, $msg) { function __d($domain, $msg, $args = null) {
if (!$msg) { if (!$msg) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($msg, null, $domain); $translated = I18n::translate($msg, null, $domain);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
return vsprintf($translated, $args);
} }
/** /**
@ -519,16 +540,23 @@ if (!function_exists('sortByKey')) {
* @param string $singular Singular string to translate * @param string $singular Singular string to translate
* @param string $plural Plural * @param string $plural Plural
* @param integer $count Count * @param integer $count Count
* @param mixed $args Array with arguments or multiple arguments in function
* @return plural form of translated string * @return plural form of translated string
*/ */
function __dn($domain, $singular, $plural, $count) { function __dn($domain, $singular, $plural, $count, $args = null) {
if (!$singular) { if (!$singular) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($singular, $plural, $domain, 6, $count); $translated = I18n::translate($singular, $plural, $domain, 6, $count);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 4);
}
return vsprintf($translated, $args);
} }
/** /**
@ -551,16 +579,23 @@ if (!function_exists('sortByKey')) {
* @param string $domain Domain * @param string $domain Domain
* @param string $msg Message to translate * @param string $msg Message to translate
* @param integer $category Category * @param integer $category Category
* @param mixed $args Array with arguments or multiple arguments in function
* @return translated string * @return translated string
*/ */
function __dc($domain, $msg, $category) { function __dc($domain, $msg, $category, $args = null) {
if (!$msg) { if (!$msg) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($msg, null, $domain, $category); $translated = I18n::translate($msg, null, $domain, $category);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
return vsprintf($translated, $args);
} }
/** /**
@ -587,16 +622,23 @@ if (!function_exists('sortByKey')) {
* @param string $plural Plural * @param string $plural Plural
* @param integer $count Count * @param integer $count Count
* @param integer $category Category * @param integer $category Category
* @param mixed $args Array with arguments or multiple arguments in function
* @return plural form of translated string * @return plural form of translated string
*/ */
function __dcn($domain, $singular, $plural, $count, $category) { function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
if (!$singular) { if (!$singular) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($singular, $plural, $domain, $category, $count); $translated = I18n::translate($singular, $plural, $domain, $category, $count);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 5);
}
return vsprintf($translated, $args);
} }
/** /**
@ -615,16 +657,23 @@ if (!function_exists('sortByKey')) {
* *
* @param string $msg String to translate * @param string $msg String to translate
* @param integer $category Category * @param integer $category Category
* @param mixed $args Array with arguments or multiple arguments in function
* @return translated string * @return translated string
*/ */
function __c($msg, $category) { function __c($msg, $category, $args = null) {
if (!$msg) { if (!$msg) {
return; return;
} }
if (!class_exists('I18n')) { if (!class_exists('I18n')) {
App::import('Core', 'i18n'); App::import('Core', 'i18n');
} }
return I18n::translate($msg, null, null, $category); $translated = I18n::translate($msg, null, null, $category);
if ($args === null) {
return $translated;
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
return vsprintf($translated, $args);
} }
/** /**

View file

@ -367,6 +367,30 @@ class BasicsTest extends CakeTestCase {
$result = __('Plural Rule 1 (from core)'); $result = __('Plural Rule 1 (from core)');
$expected = 'Plural Rule 1 (from core translated)'; $expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __('Some string with %s', 'arguments');
$expected = 'Some string with arguments';
$this->assertEqual($result, $expected);
$result = __('Some string with %s %s', 'multiple', 'arguments');
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
$result = __('Some string with %s %s', array('multiple', 'arguments'));
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
$result = __('Testing %2$s %1$s', 'order', 'different');
$expected = 'Testing different order';
$this->assertEqual($result, $expected);
$result = __('Testing %2$s %1$s', array('order', 'different'));
$expected = 'Testing different order';
$this->assertEqual($result, $expected);
$result = __('Testing %.2f number', 1.2345);
$expected = 'Testing 1.23 number';
$this->assertEqual($result, $expected);
} }
/** /**
@ -388,6 +412,18 @@ class BasicsTest extends CakeTestCase {
$result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2); $result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
$expected = '%d = 0 or > 1 (from core translated)'; $expected = '%d = 0 or > 1 (from core translated)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __n('%d item.', '%d items.', 1, 1);
$expected = '1 item.';
$this->assertEqual($result, $expected);
$result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
$expected = '2 items for id 1234';
$this->assertEqual($result, $expected);
$result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
$expected = '2 items for id 1234';
$this->assertEqual($result, $expected);
} }
/** /**
@ -409,6 +445,18 @@ class BasicsTest extends CakeTestCase {
$result = __d('core', 'Plural Rule 1 (from core)'); $result = __d('core', 'Plural Rule 1 (from core)');
$expected = 'Plural Rule 1 (from core translated)'; $expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __d('core', 'Some string with %s', 'arguments');
$expected = 'Some string with arguments';
$this->assertEqual($result, $expected);
$result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
$result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
} }
/** /**
@ -435,6 +483,17 @@ class BasicsTest extends CakeTestCase {
$expected = '%d = 1 (translated)'; $expected = '%d = 1 (translated)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __dn('core', '%d item.', '%d items.', 1, 1);
$expected = '1 item.';
$this->assertEqual($result, $expected);
$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
$expected = '2 items for id 1234';
$this->assertEqual($result, $expected);
$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
$expected = '2 items for id 1234';
$this->assertEqual($result, $expected);
} }
/** /**
@ -452,6 +511,18 @@ class BasicsTest extends CakeTestCase {
$result = __c('Plural Rule 1 (from core)', 6); $result = __c('Plural Rule 1 (from core)', 6);
$expected = 'Plural Rule 1 (from core translated)'; $expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __c('Some string with %s', 6, 'arguments');
$expected = 'Some string with arguments';
$this->assertEqual($result, $expected);
$result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
$result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
} }
/** /**
@ -477,6 +548,18 @@ class BasicsTest extends CakeTestCase {
$result = __dc('core', 'Plural Rule 1 (from core)', 6); $result = __dc('core', 'Plural Rule 1 (from core)', 6);
$expected = 'Plural Rule 1 (from core translated)'; $expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __dc('core', 'Some string with %s', 6, 'arguments');
$expected = 'Some string with arguments';
$this->assertEqual($result, $expected);
$result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
$result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
$expected = 'Some string with multiple arguments';
$this->assertEqual($result, $expected);
} }
/** /**
@ -498,6 +581,18 @@ class BasicsTest extends CakeTestCase {
$result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6); $result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
$expected = '%d = 0 or > 1'; $expected = '%d = 0 or > 1';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
$expected = '1 item.';
$this->assertEqual($result, $expected);
$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
$expected = '2 items for id 1234';
$this->assertEqual($result, $expected);
$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
$expected = '2 items for id 1234';
$this->assertEqual($result, $expected);
} }
/** /**