Use correct context when parsing MO file.

This commit is contained in:
Edgaras Janušauskas 2015-12-18 12:59:00 +02:00
parent 0668cc6ff1
commit 4101a96a95
4 changed files with 9 additions and 2 deletions

View file

@ -478,6 +478,7 @@ class I18n {
* *
* @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;
@ -486,7 +487,6 @@ class I18n {
// 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);
@ -496,6 +496,7 @@ class I18n {
$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);

View file

@ -2054,6 +2054,9 @@ class I18nTest extends CakeTestCase {
$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'));
} }
/** /**

View file

@ -27,3 +27,6 @@ msgstr "balans"
msgctxt "money" msgctxt "money"
msgid "balance" msgid "balance"
msgstr "saldo" msgstr "saldo"
msgid "zoo"
msgstr "dierentuin"