Skip to content

Commit

Permalink
Merge pull request #6543 from PGBI/issue6542
Browse files Browse the repository at this point in the history
3.0 - make I18N shells extract task differentiate similar msgs with different contexts
  • Loading branch information
lorenzo committed May 12, 2015
2 parents 94ccc30 + e4c089f commit f10d283
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 43 deletions.
78 changes: 37 additions & 41 deletions src/Shell/Task/ExtractTask.php
Expand Up @@ -243,31 +243,26 @@ public function main()
*
* @param string $domain The domain
* @param string $msgid The message string
* @param array $details The file and line references
* @param array $details Context and plural form if any, file and line references
* @return void
*/
protected function _addTranslation($domain, $msgid, $details = [])
{
if (empty($this->_translations[$domain][$msgid])) {
$this->_translations[$domain][$msgid] = [
'msgid_plural' => false,
'msgctxt' => ''
$context = isset($details['msgctxt']) ? $details['msgctxt'] : "";

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

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

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

Expand Down Expand Up @@ -449,35 +444,36 @@ protected function _buildFiles()
$paths = $this->_paths;
$paths[] = realpath(APP) . DS;
foreach ($this->_translations as $domain => $translations) {
foreach ($translations as $msgid => $details) {
$plural = $details['msgid_plural'];
$context = $details['msgctxt'];
$files = $details['references'];
$occurrences = [];
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 = [];
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($domain, $header, $sentence);
if ($domain !== 'default' && $this->_merge) {
$this->_store('default', $header, $sentence);
$this->_store($domain, $header, $sentence);
if ($domain !== 'default' && $this->_merge) {
$this->_store('default', $header, $sentence);
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions tests/TestCase/Shell/Task/ExtractTaskTest.php
Expand Up @@ -108,8 +108,14 @@ 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:31\n';
$pattern .= 'msgctxt "mail"/';
$pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
$pattern .= 'msgctxt "mail"\n';
$pattern .= 'msgid "letter"/';
$this->assertRegExp($pattern, $result);

$pattern = '/\#: (\\\\|\/)extract\.ctp:\d+\n';
$pattern .= 'msgctxt "alphabet"\n';
$pattern .= 'msgid "letter"/';
$this->assertRegExp($pattern, $result);

// extract.ctp - reading the domain.pot
Expand Down
3 changes: 3 additions & 0 deletions tests/test_app/TestApp/Template/Pages/extract.ctp
Expand Up @@ -29,3 +29,6 @@ __('Hot features!'

// Context
echo __x('mail', 'letter');

// Duplicated message with different context
echo __x('alphabet', 'letter');

0 comments on commit f10d283

Please sign in to comment.