Skip to content

Commit

Permalink
Merge pull request #7866 from garas/i18n-mo-loading-context
Browse files Browse the repository at this point in the history
Use correct context when parsing MO file.
  • Loading branch information
markstory committed Dec 20, 2015
2 parents d2153ac + 4101a96 commit f2f704d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/Cake/I18n/I18n.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ protected function _bindTextDomain($domain) {
*
* @param string $filename Binary .mo file to load
* @return mixed Array of translations on success or false on failure
* @link https://www.gnu.org/software/gettext/manual/html_node/MO-Files.html
*/
public static function loadMo($filename) {
$translations = false;
Expand All @@ -486,7 +487,6 @@ public static function loadMo($filename) {
// Binary files extracted makes non-standard local variables
if ($data = file_get_contents($filename)) {
$translations = array();
$context = null;
$header = substr($data, 0, 20);
$header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
extract($header);
Expand All @@ -496,6 +496,7 @@ public static function loadMo($filename) {
$r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
$msgid = substr($data, $r["offs"], $r["len"]);
unset($msgid_plural);
$context = null;

if (strpos($msgid, "\x04") !== false) {
list($context, $msgid) = explode("\x04", $msgid);
Expand Down
3 changes: 3 additions & 0 deletions lib/Cake/Test/Case/I18n/I18nTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2054,6 +2054,9 @@ public function testContextMoFile() {
$this->assertSame("danspartij", __x('social gathering', 'ball'));
$this->assertSame("balans", __('balance'));
$this->assertSame("saldo", __x('money', 'balance'));

// MO file is sorted by msgid, 'zoo' should be last
$this->assertSame("dierentuin", __('zoo'));
}

/**
Expand Down
5 changes: 4 additions & 1 deletion lib/Cake/Test/test_app/Locale/nld/LC_MESSAGES/default.po
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ msgstr "balans"

msgctxt "money"
msgid "balance"
msgstr "saldo"
msgstr "saldo"

msgid "zoo"
msgstr "dierentuin"
Binary file modified lib/Cake/Test/test_app/Locale/nld_mo/LC_MESSAGES/default.mo
Binary file not shown.

0 comments on commit f2f704d

Please sign in to comment.