Permalink
Browse files

handle loose % signs in __() function

While it's passed to sprintf and translation markers may contain
variable placeholders (%s, %d) that doesn't mean translators need to be
aware of all relevant rules. A % sign in a translation message shouldn't
cause a warning (and no return) it should be handled as a literal
percent sign.
  • Loading branch information...
1 parent 9de3418 commit 5334fe04c364100b031740eeea77b7d91c381d16 @AD7six AD7six committed Jan 2, 2014
Showing with 49 additions and 0 deletions.
  1. +35 −0 lib/Cake/Test/Case/BasicsTest.php
  2. +14 −0 lib/Cake/basics.php
@@ -398,6 +398,41 @@ public function testTranslate() {
}
/**
+ * testTranslatePercent
+ *
+ * @return void
+ */
+ public function testTranslatePercent() {
+ $result = __('%s are 100% real fruit', 'Apples');
+ $expected = 'Apples are 100% real fruit';
+ $this->assertEquals($expected, $result, 'Percent sign at end of word should be considered literal');
+
+ $result = __('%s are %d% real fruit', 'Apples', 100);
+ $expected = 'Apples are 100% real fruit';
+ $this->assertEquals($expected, $result, 'A digit marker should not be misinterpreted');
+
+ $result = __('%s are %s% real fruit', 'Apples', 100);
+ $expected = 'Apples are 100% real fruit';
+ $this->assertEquals($expected, $result, 'A string marker should not be misinterpreted');
+
+ $result = __('%nonsense %s', 'Apples');
+ $expected = '%nonsense Apples';
+ $this->assertEquals($expected, $result, 'A percent sign at the start of the string should be considered literal');
+
+ $result = __('%s are awesome%', 'Apples');
+ $expected = 'Apples are awesome%';
+ $this->assertEquals($expected, $result, 'A percent sign at the end of the string should be considered literal');
+
+ $result = __('%2$d %1$s entered the bowl', 'Apples', 2);
+ $expected = '2 Apples entered the bowl';
+ $this->assertEquals($expected, $result, 'Positional replacement markers should not be misinterpreted');
+
+ $result = __('%.2f% of all %s agree', 99.44444, 'Cats');
+ $expected = '99.44% of all Cats agree';
+ $this->assertEquals($expected, $result, 'significant-digit placeholder should not be misinterpreted');
+ }
+
+/**
* test __n()
*
* @return void
View
@@ -559,6 +559,8 @@ function __($singular, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 1);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@@ -589,6 +591,8 @@ function __n($singular, $plural, $count, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@@ -616,6 +620,8 @@ function __d($domain, $msg, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@@ -647,6 +653,8 @@ function __dn($domain, $singular, $plural, $count, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 4);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@@ -689,6 +697,8 @@ function __dc($domain, $msg, $category, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 3);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@@ -735,6 +745,8 @@ function __dcn($domain, $singular, $plural, $count, $category, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 5);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}
@@ -773,6 +785,8 @@ function __c($msg, $category, $args = null) {
} elseif (!is_array($args)) {
$args = array_slice(func_get_args(), 2);
}
+
+ $translated = preg_replace('/(?<!%)%(?![%bcdeEfFgGosuxX\d\.])/', '%%', $translated);
return vsprintf($translated, $args);
}

0 comments on commit 5334fe0

Please sign in to comment.