Skip to content

Commit 30661ed

Browse files
committed
Support to vsprintf in i18n aliases (__*() functions).
1 parent f4d4811 commit 30661ed

File tree

2 files changed

+158
-14
lines changed

2 files changed

+158
-14
lines changed

cake/basics.php

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -461,17 +461,24 @@ function stripslashes_deep($values) {
461461
* Returns a translated string if one is found; Otherwise, the submitted message.
462462
*
463463
* @param string $singular Text to translate
464+
* @param mixed $args Array with arguments or multiple arguments in function
464465
* @return mixed translated string
465466
* @link http://book.cakephp.org/view/1121/__
466467
*/
467-
function __($singular) {
468+
function __($singular, $args = null) {
468469
if (!$singular) {
469470
return;
470471
}
471472
if (!class_exists('I18n')) {
472473
App::import('Core', 'i18n');
473474
}
474-
return I18n::translate($singular);
475+
$translated = I18n::translate($singular);
476+
if ($args === null) {
477+
return $translated;
478+
} elseif (!is_array($args)) {
479+
$args = array_slice(func_get_args(), 1);
480+
}
481+
return vsprintf($translated, $args);
475482
}
476483

477484
/**
@@ -481,33 +488,47 @@ function __($singular) {
481488
* @param string $singular Singular text to translate
482489
* @param string $plural Plural text
483490
* @param integer $count Count
491+
* @param mixed $args Array with arguments or multiple arguments in function
484492
* @return mixed plural form of translated string
485493
*/
486-
function __n($singular, $plural, $count) {
494+
function __n($singular, $plural, $count, $args = null) {
487495
if (!$singular) {
488496
return;
489497
}
490498
if (!class_exists('I18n')) {
491499
App::import('Core', 'i18n');
492500
}
493-
return I18n::translate($singular, $plural, null, 6, $count);
501+
$translated = I18n::translate($singular, $plural, null, 6, $count);
502+
if ($args === null) {
503+
return $translated;
504+
} elseif (!is_array($args)) {
505+
$args = array_slice(func_get_args(), 3);
506+
}
507+
return vsprintf($translated, $args);
494508
}
495509

496510
/**
497511
* Allows you to override the current domain for a single message lookup.
498512
*
499513
* @param string $domain Domain
500514
* @param string $msg String to translate
515+
* @param mixed $args Array with arguments or multiple arguments in function
501516
* @return translated string
502517
*/
503-
function __d($domain, $msg) {
518+
function __d($domain, $msg, $args = null) {
504519
if (!$msg) {
505520
return;
506521
}
507522
if (!class_exists('I18n')) {
508523
App::import('Core', 'i18n');
509524
}
510-
return I18n::translate($msg, null, $domain);
525+
$translated = I18n::translate($msg, null, $domain);
526+
if ($args === null) {
527+
return $translated;
528+
} elseif (!is_array($args)) {
529+
$args = array_slice(func_get_args(), 2);
530+
}
531+
return vsprintf($translated, $args);
511532
}
512533

513534
/**
@@ -519,16 +540,23 @@ function __d($domain, $msg) {
519540
* @param string $singular Singular string to translate
520541
* @param string $plural Plural
521542
* @param integer $count Count
543+
* @param mixed $args Array with arguments or multiple arguments in function
522544
* @return plural form of translated string
523545
*/
524-
function __dn($domain, $singular, $plural, $count) {
546+
function __dn($domain, $singular, $plural, $count, $args = null) {
525547
if (!$singular) {
526548
return;
527549
}
528550
if (!class_exists('I18n')) {
529551
App::import('Core', 'i18n');
530552
}
531-
return I18n::translate($singular, $plural, $domain, 6, $count);
553+
$translated = I18n::translate($singular, $plural, $domain, 6, $count);
554+
if ($args === null) {
555+
return $translated;
556+
} elseif (!is_array($args)) {
557+
$args = array_slice(func_get_args(), 4);
558+
}
559+
return vsprintf($translated, $args);
532560
}
533561

534562
/**
@@ -551,16 +579,23 @@ function __dn($domain, $singular, $plural, $count) {
551579
* @param string $domain Domain
552580
* @param string $msg Message to translate
553581
* @param integer $category Category
582+
* @param mixed $args Array with arguments or multiple arguments in function
554583
* @return translated string
555584
*/
556-
function __dc($domain, $msg, $category) {
585+
function __dc($domain, $msg, $category, $args = null) {
557586
if (!$msg) {
558587
return;
559588
}
560589
if (!class_exists('I18n')) {
561590
App::import('Core', 'i18n');
562591
}
563-
return I18n::translate($msg, null, $domain, $category);
592+
$translated = I18n::translate($msg, null, $domain, $category);
593+
if ($args === null) {
594+
return $translated;
595+
} elseif (!is_array($args)) {
596+
$args = array_slice(func_get_args(), 3);
597+
}
598+
return vsprintf($translated, $args);
564599
}
565600

566601
/**
@@ -587,16 +622,23 @@ function __dc($domain, $msg, $category) {
587622
* @param string $plural Plural
588623
* @param integer $count Count
589624
* @param integer $category Category
625+
* @param mixed $args Array with arguments or multiple arguments in function
590626
* @return plural form of translated string
591627
*/
592-
function __dcn($domain, $singular, $plural, $count, $category) {
628+
function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
593629
if (!$singular) {
594630
return;
595631
}
596632
if (!class_exists('I18n')) {
597633
App::import('Core', 'i18n');
598634
}
599-
return I18n::translate($singular, $plural, $domain, $category, $count);
635+
$translated = I18n::translate($singular, $plural, $domain, $category, $count);
636+
if ($args === null) {
637+
return $translated;
638+
} elseif (!is_array($args)) {
639+
$args = array_slice(func_get_args(), 5);
640+
}
641+
return vsprintf($translated, $args);
600642
}
601643

602644
/**
@@ -615,16 +657,23 @@ function __dcn($domain, $singular, $plural, $count, $category) {
615657
*
616658
* @param string $msg String to translate
617659
* @param integer $category Category
660+
* @param mixed $args Array with arguments or multiple arguments in function
618661
* @return translated string
619662
*/
620-
function __c($msg, $category) {
663+
function __c($msg, $category, $args = null) {
621664
if (!$msg) {
622665
return;
623666
}
624667
if (!class_exists('I18n')) {
625668
App::import('Core', 'i18n');
626669
}
627-
return I18n::translate($msg, null, null, $category);
670+
$translated = I18n::translate($msg, null, null, $category);
671+
if ($args === null) {
672+
return $translated;
673+
} elseif (!is_array($args)) {
674+
$args = array_slice(func_get_args(), 2);
675+
}
676+
return vsprintf($translated, $args);
628677
}
629678

630679
/**

cake/tests/cases/basics.test.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,30 @@ public function test__() {
367367
$result = __('Plural Rule 1 (from core)');
368368
$expected = 'Plural Rule 1 (from core translated)';
369369
$this->assertEqual($result, $expected);
370+
371+
$result = __('Some string with %s', 'arguments');
372+
$expected = 'Some string with arguments';
373+
$this->assertEqual($result, $expected);
374+
375+
$result = __('Some string with %s %s', 'multiple', 'arguments');
376+
$expected = 'Some string with multiple arguments';
377+
$this->assertEqual($result, $expected);
378+
379+
$result = __('Some string with %s %s', array('multiple', 'arguments'));
380+
$expected = 'Some string with multiple arguments';
381+
$this->assertEqual($result, $expected);
382+
383+
$result = __('Testing %2$s %1$s', 'order', 'different');
384+
$expected = 'Testing different order';
385+
$this->assertEqual($result, $expected);
386+
387+
$result = __('Testing %2$s %1$s', array('order', 'different'));
388+
$expected = 'Testing different order';
389+
$this->assertEqual($result, $expected);
390+
391+
$result = __('Testing %.2f number', 1.2345);
392+
$expected = 'Testing 1.23 number';
393+
$this->assertEqual($result, $expected);
370394
}
371395

372396
/**
@@ -388,6 +412,18 @@ public function test__n() {
388412
$result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
389413
$expected = '%d = 0 or > 1 (from core translated)';
390414
$this->assertEqual($result, $expected);
415+
416+
$result = __n('%d item.', '%d items.', 1, 1);
417+
$expected = '1 item.';
418+
$this->assertEqual($result, $expected);
419+
420+
$result = __n('%d item for id %s', '%d items for id %s', 2, 2, '1234');
421+
$expected = '2 items for id 1234';
422+
$this->assertEqual($result, $expected);
423+
424+
$result = __n('%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
425+
$expected = '2 items for id 1234';
426+
$this->assertEqual($result, $expected);
391427
}
392428

393429
/**
@@ -409,6 +445,18 @@ public function test__d() {
409445
$result = __d('core', 'Plural Rule 1 (from core)');
410446
$expected = 'Plural Rule 1 (from core translated)';
411447
$this->assertEqual($result, $expected);
448+
449+
$result = __d('core', 'Some string with %s', 'arguments');
450+
$expected = 'Some string with arguments';
451+
$this->assertEqual($result, $expected);
452+
453+
$result = __d('core', 'Some string with %s %s', 'multiple', 'arguments');
454+
$expected = 'Some string with multiple arguments';
455+
$this->assertEqual($result, $expected);
456+
457+
$result = __d('core', 'Some string with %s %s', array('multiple', 'arguments'));
458+
$expected = 'Some string with multiple arguments';
459+
$this->assertEqual($result, $expected);
412460
}
413461

414462
/**
@@ -435,6 +483,17 @@ public function test__dn() {
435483
$expected = '%d = 1 (translated)';
436484
$this->assertEqual($result, $expected);
437485

486+
$result = __dn('core', '%d item.', '%d items.', 1, 1);
487+
$expected = '1 item.';
488+
$this->assertEqual($result, $expected);
489+
490+
$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, 2, '1234');
491+
$expected = '2 items for id 1234';
492+
$this->assertEqual($result, $expected);
493+
494+
$result = __dn('core', '%d item for id %s', '%d items for id %s', 2, array(2, '1234'));
495+
$expected = '2 items for id 1234';
496+
$this->assertEqual($result, $expected);
438497
}
439498

440499
/**
@@ -452,6 +511,18 @@ public function test__c() {
452511
$result = __c('Plural Rule 1 (from core)', 6);
453512
$expected = 'Plural Rule 1 (from core translated)';
454513
$this->assertEqual($result, $expected);
514+
515+
$result = __c('Some string with %s', 6, 'arguments');
516+
$expected = 'Some string with arguments';
517+
$this->assertEqual($result, $expected);
518+
519+
$result = __c('Some string with %s %s', 6, 'multiple', 'arguments');
520+
$expected = 'Some string with multiple arguments';
521+
$this->assertEqual($result, $expected);
522+
523+
$result = __c('Some string with %s %s', 6, array('multiple', 'arguments'));
524+
$expected = 'Some string with multiple arguments';
525+
$this->assertEqual($result, $expected);
455526
}
456527

457528
/**
@@ -477,6 +548,18 @@ public function test__dc() {
477548
$result = __dc('core', 'Plural Rule 1 (from core)', 6);
478549
$expected = 'Plural Rule 1 (from core translated)';
479550
$this->assertEqual($result, $expected);
551+
552+
$result = __dc('core', 'Some string with %s', 6, 'arguments');
553+
$expected = 'Some string with arguments';
554+
$this->assertEqual($result, $expected);
555+
556+
$result = __dc('core', 'Some string with %s %s', 6, 'multiple', 'arguments');
557+
$expected = 'Some string with multiple arguments';
558+
$this->assertEqual($result, $expected);
559+
560+
$result = __dc('core', 'Some string with %s %s', 6, array('multiple', 'arguments'));
561+
$expected = 'Some string with multiple arguments';
562+
$this->assertEqual($result, $expected);
480563
}
481564

482565
/**
@@ -498,6 +581,18 @@ public function test__dcn() {
498581
$result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 6);
499582
$expected = '%d = 0 or > 1';
500583
$this->assertEqual($result, $expected);
584+
585+
$result = __dcn('core', '%d item.', '%d items.', 1, 6, 1);
586+
$expected = '1 item.';
587+
$this->assertEqual($result, $expected);
588+
589+
$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, 2, '1234');
590+
$expected = '2 items for id 1234';
591+
$this->assertEqual($result, $expected);
592+
593+
$result = __dcn('core', '%d item for id %s', '%d items for id %s', 2, 6, array(2, '1234'));
594+
$expected = '2 items for id 1234';
595+
$this->assertEqual($result, $expected);
501596
}
502597

503598
/**

0 commit comments

Comments
 (0)