Skip to content
Permalink
Browse files

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

  • Loading branch information...
jrbasso committed Dec 5, 2010
1 parent f4d4811 commit 30661ed154835bb14f1eb77af8421a5e29bd70e9
Showing with 158 additions and 14 deletions.
  1. +63 −14 cake/basics.php
  2. +95 −0 cake/tests/cases/basics.test.php
@@ -461,17 +461,24 @@ function stripslashes_deep($values) {
* Returns a translated string if one is found; Otherwise, the submitted message.
*
* @param string $singular Text to translate
* @param mixed $args Array with arguments or multiple arguments in function
* @return mixed translated string
* @link http://book.cakephp.org/view/1121/__
*/
function __($singular) {
function __($singular, $args = null) {
if (!$singular) {
return;
}
if (!class_exists('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,33 +488,47 @@ function __($singular) {
* @param string $singular Singular text to translate
* @param string $plural Plural text
* @param integer $count Count
* @param mixed $args Array with arguments or multiple arguments in function
* @return mixed plural form of translated string
*/
function __n($singular, $plural, $count) {
function __n($singular, $plural, $count, $args = null) {
if (!$singular) {
return;
}
if (!class_exists('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);
}
/**
* Allows you to override the current domain for a single message lookup.
*
* @param string $domain Domain
* @param string $msg String to translate
* @param mixed $args Array with arguments or multiple arguments in function
* @return translated string
*/
function __d($domain, $msg) {
function __d($domain, $msg, $args = null) {
if (!$msg) {
return;
}
if (!class_exists('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 @@ function __d($domain, $msg) {
* @param string $singular Singular string to translate
* @param string $plural Plural
* @param integer $count Count
* @param mixed $args Array with arguments or multiple arguments in function
* @return plural form of translated string
*/
function __dn($domain, $singular, $plural, $count) {
function __dn($domain, $singular, $plural, $count, $args = null) {
if (!$singular) {
return;
}
if (!class_exists('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 @@ function __dn($domain, $singular, $plural, $count) {
* @param string $domain Domain
* @param string $msg Message to translate
* @param integer $category Category
* @param mixed $args Array with arguments or multiple arguments in function
* @return translated string
*/
function __dc($domain, $msg, $category) {
function __dc($domain, $msg, $category, $args = null) {
if (!$msg) {
return;
}
if (!class_exists('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 @@ function __dc($domain, $msg, $category) {
* @param string $plural Plural
* @param integer $count Count
* @param integer $category Category
* @param mixed $args Array with arguments or multiple arguments in function
* @return plural form of translated string
*/
function __dcn($domain, $singular, $plural, $count, $category) {
function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
if (!$singular) {
return;
}
if (!class_exists('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 @@ function __dcn($domain, $singular, $plural, $count, $category) {
*
* @param string $msg String to translate
* @param integer $category Category
* @param mixed $args Array with arguments or multiple arguments in function
* @return translated string
*/
function __c($msg, $category) {
function __c($msg, $category, $args = null) {
if (!$msg) {
return;
}
if (!class_exists('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);
}
/**
@@ -367,6 +367,30 @@ public function test__() {
$result = __('Plural Rule 1 (from core)');
$expected = 'Plural Rule 1 (from core translated)';
$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 @@ public function test__n() {
$result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
$expected = '%d = 0 or > 1 (from core translated)';
$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 @@ public function test__d() {
$result = __d('core', 'Plural Rule 1 (from core)');
$expected = 'Plural Rule 1 (from core translated)';
$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 @@ public function test__dn() {
$expected = '%d = 1 (translated)';
$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 @@ public function test__c() {
$result = __c('Plural Rule 1 (from core)', 6);
$expected = 'Plural Rule 1 (from core translated)';
$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 @@ public function test__dc() {
$result = __dc('core', 'Plural Rule 1 (from core)', 6);
$expected = 'Plural Rule 1 (from core translated)';
$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 @@ public function test__dcn() {
$result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
$expected = '%d = 0 or > 1';
$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);
}
/**

0 comments on commit 30661ed

Please sign in to comment.
You can’t perform that action at this time.