Skip to content

Commit

Permalink
Merge pull request #5490 from garas/2.6-i18n-ExtractTask-context
Browse files Browse the repository at this point in the history
Fix Extract task to correctly extract messages with context
  • Loading branch information
markstory committed Dec 25, 2014
2 parents 02ca7f5 + 3ae61e9 commit 11206d5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 41 deletions.
75 changes: 38 additions & 37 deletions lib/Cake/Console/Command/Task/ExtractTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ExtractTask extends AppShell {
protected $_tokens = array();

/**
* Extracted strings indexed by category and domain.
* Extracted strings indexed by category, domain, msgid and context.
*
* @var array
*/
Expand Down Expand Up @@ -249,26 +249,26 @@ public function execute() {
* @return void
*/
protected function _addTranslation($category, $domain, $msgid, $details = array()) {
if (empty($this->_translations[$category][$domain][$msgid])) {
$this->_translations[$category][$domain][$msgid] = array(
$context = '';
if (isset($details['msgctxt'])) {
$context = $details['msgctxt'];
}

if (empty($this->_translations[$category][$domain][$msgid][$context])) {
$this->_translations[$category][$domain][$msgid][$context] = array(
'msgid_plural' => false,
'msgctxt' => ''
);
}

if (isset($details['msgid_plural'])) {
$this->_translations[$category][$domain][$msgid]['msgid_plural'] = $details['msgid_plural'];
$this->_translations[$category][$domain][$msgid][$context]['msgid_plural'] = $details['msgid_plural'];
}
if (isset($details['msgctxt'])) {
$this->_translations[$category][$domain][$msgid]['msgctxt'] = $details['msgctxt'];
}

if (isset($details['file'])) {
$line = 0;
if (isset($details['line'])) {
$line = $details['line'];
}
$this->_translations[$category][$domain][$msgid]['references'][$details['file']][] = $line;
$this->_translations[$category][$domain][$msgid][$context]['references'][$details['file']][] = $line;
}
}

Expand Down Expand Up @@ -565,35 +565,36 @@ protected function _buildFiles() {
$paths[] = realpath(APP) . DS;
foreach ($this->_translations as $category => $domains) {
foreach ($domains as $domain => $translations) {
foreach ($translations as $msgid => $details) {
$plural = $details['msgid_plural'];
$context = $details['msgctxt'];
$files = $details['references'];
$occurrences = array();
foreach ($files as $file => $lines) {
$lines = array_unique($lines);
$occurrences[] = $file . ':' . implode(';', $lines);
}
$occurrences = implode("\n#: ", $occurrences);
$header = '#: ' . str_replace(DS, '/', str_replace($paths, '', $occurrences)) . "\n";
foreach ($translations as $msgid => $contexts) {
foreach ($contexts as $context => $details) {
$plural = $details['msgid_plural'];
$files = $details['references'];
$occurrences = array();
foreach ($files as $file => $lines) {
$lines = array_unique($lines);
$occurrences[] = $file . ':' . implode(';', $lines);
}
$occurrences = implode("\n#: ", $occurrences);
$header = '#: ' . str_replace(DS, '/', str_replace($paths, '', $occurrences)) . "\n";

$sentence = '';
if ($context) {
$sentence .= "msgctxt \"{$context}\"\n";
}
if ($plural === false) {
$sentence .= "msgid \"{$msgid}\"\n";
$sentence .= "msgstr \"\"\n\n";
} else {
$sentence .= "msgid \"{$msgid}\"\n";
$sentence .= "msgid_plural \"{$plural}\"\n";
$sentence .= "msgstr[0] \"\"\n";
$sentence .= "msgstr[1] \"\"\n\n";
}
$sentence = '';
if ($context) {
$sentence .= "msgctxt \"{$context}\"\n";
}
if ($plural === false) {
$sentence .= "msgid \"{$msgid}\"\n";
$sentence .= "msgstr \"\"\n\n";
} else {
$sentence .= "msgid \"{$msgid}\"\n";
$sentence .= "msgid_plural \"{$plural}\"\n";
$sentence .= "msgstr[0] \"\"\n";
$sentence .= "msgstr[1] \"\"\n\n";
}

$this->_store($category, $domain, $header, $sentence);
if (($category !== 'LC_MESSAGES' || $domain !== 'default') && $this->_merge) {
$this->_store('LC_MESSAGES', 'default', $header, $sentence);
$this->_store($category, $domain, $header, $sentence);
if (($category !== 'LC_MESSAGES' || $domain !== 'default') && $this->_merge) {
$this->_store('LC_MESSAGES', 'default', $header, $sentence);
}
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,20 @@ public function testExecute() {
$this->assertContains('msgid "double \\"quoted\\""', $result, 'Strings with quotes not handled correctly');
$this->assertContains("msgid \"single 'quoted'\"", $result, 'Strings with quotes not handled correctly');

$pattern = '/\#: (\\\\|\/)extract\.ctp:33\n';
$pattern .= 'msgctxt "mail"/';
$this->assertRegExp($pattern, $result);
$pattern = '/\#: (\\\\|\/)extract\.ctp:34\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Strings with context should not overwrite strings without context');

$pattern = '/\#: (\\\\|\/)extract\.ctp:35;37\nmsgctxt "A"\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Should contain string with context "A"');

$pattern = '/\#: (\\\\|\/)extract\.ctp:36\nmsgctxt "B"\nmsgid "letter"/';
$this->assertRegExp($pattern, $result, 'Should contain string with context "B"');

$pattern = '/\#: (\\\\|\/)extract\.ctp:38\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$this->assertRegExp($pattern, $result, 'Plural strings with context should not overwrite strings without context');

$pattern = '/\#: (\\\\|\/)extract\.ctp:39\nmsgctxt "A"\nmsgid "%d letter"\nmsgid_plural "%d letters"/';
$this->assertRegExp($pattern, $result, 'Should contain plural string with context "A"');

// extract.ctp - reading the domain.pot
$result = file_get_contents($this->path . DS . 'domain.pot');
Expand Down
8 changes: 7 additions & 1 deletion lib/Cake/Test/test_app/View/Pages/extract.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ __('Hot features!'
// Category
echo __c('You have a new message (category: LC_TIME).', 5);

echo __x('mail', 'letter');
// Context
echo __('letter');
echo __x('A', 'letter');
echo __x('B', 'letter');
echo __x('A', 'letter');
echo __n('%d letter', '%d letters', $count);
echo __xn('A', '%d letter', '%d letters', $count);

0 comments on commit 11206d5

Please sign in to comment.