Skip to content

Commit

Permalink
Use correct context when parsing MO file.
Browse files Browse the repository at this point in the history
  • Loading branch information
garas committed Dec 18, 2015
1 parent 0668cc6 commit 4101a96
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
Expand Up @@ -478,6 +478,7 @@ protected function _bindTextDomain($domain) {
* *
* @param string $filename Binary .mo file to load * @param string $filename Binary .mo file to load
* @return mixed Array of translations on success or false on failure * @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) { public static function loadMo($filename) {
$translations = false; $translations = false;
Expand All @@ -486,7 +487,6 @@ public static function loadMo($filename) {
// Binary files extracted makes non-standard local variables // Binary files extracted makes non-standard local variables
if ($data = file_get_contents($filename)) { if ($data = file_get_contents($filename)) {
$translations = array(); $translations = array();
$context = null;
$header = substr($data, 0, 20); $header = substr($data, 0, 20);
$header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header); $header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
extract($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)); $r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
$msgid = substr($data, $r["offs"], $r["len"]); $msgid = substr($data, $r["offs"], $r["len"]);
unset($msgid_plural); unset($msgid_plural);
$context = null;


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


msgctxt "money" msgctxt "money"
msgid "balance" 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 4101a96

Please sign in to comment.