Skip to content

Commit

Permalink
Removing the concept of category out of ExtractTask
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 26, 2014
1 parent 8e2dd71 commit 447ca25
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 118 deletions.
163 changes: 71 additions & 92 deletions src/Console/Command/Task/ExtractTask.php
Expand Up @@ -42,7 +42,7 @@ class ExtractTask extends Shell {
protected $_files = [];

/**
* Merge all domain and category strings into the default.pot file
* Merge all domain strings into the default.pot file
*
* @var bool
*/
Expand Down Expand Up @@ -70,7 +70,7 @@ class ExtractTask extends Shell {
protected $_tokens = [];

/**
* Extracted strings indexed by category and domain.
* Extracted strings indexed by domain.
*
* @var array
*/
Expand Down Expand Up @@ -210,7 +210,7 @@ public function main() {
$this->_merge = !(strtolower($this->params['merge']) === 'no');
} else {
$this->out();
$response = $this->in(__d('cake_console', 'Would you like to merge all domain and category strings into the default.pot file?'), ['y', 'n'], 'n');
$response = $this->in(__d('cake_console', 'Would you like to merge all domain strings into the default.pot file?'), ['y', 'n'], 'n');
$this->_merge = strtolower($response) === 'y';
}

Expand All @@ -232,33 +232,32 @@ public function main() {
*
* Takes care of duplicate translations
*
* @param string $category The category
* @param string $domain The domain
* @param string $msgid The message string
* @param array $details The file and line references
* @return void
*/
protected function _addTranslation($category, $domain, $msgid, $details = []) {
if (empty($this->_translations[$category][$domain][$msgid])) {
$this->_translations[$category][$domain][$msgid] = [
protected function _addTranslation($domain, $msgid, $details = []) {
if (empty($this->_translations[$domain][$msgid])) {
$this->_translations[$domain][$msgid] = [
'msgid_plural' => false,
'msgctxt' => ''
];
}

if (isset($details['msgid_plural'])) {
$this->_translations[$category][$domain][$msgid]['msgid_plural'] = $details['msgid_plural'];
$this->_translations[$domain][$msgid]['msgid_plural'] = $details['msgid_plural'];
}
if (isset($details['msgctxt'])) {
$this->_translations[$category][$domain][$msgid]['msgctxt'] = $details['msgctxt'];
$this->_translations[$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[$domain][$msgid]['references'][$details['file']][] = $line;
}
}

Expand Down Expand Up @@ -301,7 +300,7 @@ public function getOptionParser() {
])->addOption('paths', [
'help' => __d('cake_console', 'Comma separated list of paths.')
])->addOption('merge', [
'help' => __d('cake_console', 'Merge all domain and category strings into the default.po file.'),
'help' => __d('cake_console', 'Merge all domain strings into the default.po file.'),
'choices' => ['yes', 'no']
])->addOption('output', [
'help' => __d('cake_console', 'Full path to output directory.')
Expand Down Expand Up @@ -359,10 +358,7 @@ protected function _extractTokens() {
$this->_parse('__', array('singular'));
$this->_parse('__n', array('singular', 'plural'));
$this->_parse('__d', array('domain', 'singular'));
$this->_parse('__c', array('singular', 'category'));
$this->_parse('__dc', array('domain', 'singular', 'category'));
$this->_parse('__dn', array('domain', 'singular', 'plural'));
$this->_parse('__dcn', array('domain', 'singular', 'plural', 'count', 'category'));
$this->_parse('__x', array('context', 'singular'));
}
}
Expand All @@ -371,12 +367,11 @@ protected function _extractTokens() {
* Parse tokens
*
* @param string $functionName Function name that indicates translatable string (e.g: '__')
* @param array $map Array containing what variables it will find (e.g: category, domain, singular, plural)
* @param array $map Array containing what variables it will find (e.g: domain, singular, plural)
* @return void
*/
protected function _parse($functionName, $map) {
$count = 0;
$categories = ['LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'];
$tokenCount = count($this->_tokens);

while (($tokenCount - $count) > 1) {
Expand Down Expand Up @@ -406,9 +401,6 @@ protected function _parse($functionName, $map) {

if ($mapCount === count($strings)) {
extract(array_combine($map, $strings));
$category = isset($category) ? $category : 6;
$category = intval($category);
$categoryName = $categories[$category];
$domain = isset($domain) ? $domain : 'default';
$details = [
'file' => $this->_file,
Expand All @@ -420,7 +412,7 @@ protected function _parse($functionName, $map) {
if (isset($context)) {
$details['msgctxt'] = $context;
}
$this->_addTranslation($categoryName, $domain, $singular, $details);
$this->_addTranslation($domain, $singular, $details);
} else {
$this->_markerError($this->_file, $line, $functionName, $count);
}
Expand All @@ -437,38 +429,36 @@ protected function _parse($functionName, $map) {
protected function _buildFiles() {
$paths = $this->_paths;
$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 = [];
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 ($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";

$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($domain, $header, $sentence);
if ($domain !== 'default' && $this->_merge) {
$this->_store('default', $header, $sentence);
}
}
}
Expand All @@ -477,23 +467,19 @@ protected function _buildFiles() {
/**
* Prepare a file to be stored
*
* @param string $category The category
* @param string $domain The domain
* @param string $header The header content.
* @param string $sentence The sentence to store.
* @return void
*/
protected function _store($category, $domain, $header, $sentence) {
if (!isset($this->_storage[$category])) {
$this->_storage[$category] = [];
}
if (!isset($this->_storage[$category][$domain])) {
$this->_storage[$category][$domain] = [];
protected function _store($domain, $header, $sentence) {
if (!isset($this->_storage[$domain])) {
$this->_storage[$domain] = [];
}
if (!isset($this->_storage[$category][$domain][$sentence])) {
$this->_storage[$category][$domain][$sentence] = $header;
if (!isset($this->_storage[$domain][$sentence])) {
$this->_storage[$domain][$sentence] = $header;
} else {
$this->_storage[$category][$domain][$sentence] .= $header;
$this->_storage[$domain][$sentence] .= $header;
}
}

Expand All @@ -507,42 +493,35 @@ protected function _writeFiles() {
if (!empty($this->params['overwrite'])) {
$overwriteAll = true;
}
foreach ($this->_storage as $category => $domains) {
foreach ($domains as $domain => $sentences) {
$output = $this->_writeHeader();
foreach ($sentences as $sentence => $header) {
$output .= $header . $sentence;
}
foreach ($this->_storage as $domain => $sentences) {
$output = $this->_writeHeader();
foreach ($sentences as $sentence => $header) {
$output .= $header . $sentence;
}

$filename = $domain . '.pot';
if ($category === 'LC_MESSAGES') {
$File = new File($this->_output . $filename);
} else {
new Folder($this->_output . $category, true);
$File = new File($this->_output . $category . DS . $filename);
}
$response = '';
while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
$this->out();
$response = $this->in(
__d('cake_console', 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', $filename),
['y', 'n', 'a'],
'y'
);
if (strtoupper($response) === 'N') {
$response = '';
while (!$response) {
$response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
$File = new File($this->_output . $response);
$filename = $response;
}
} elseif (strtoupper($response) === 'A') {
$overwriteAll = true;
$filename = $domain . '.pot';
$File = new File($this->_output . $filename);
$response = '';
while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
$this->out();
$response = $this->in(
__d('cake_console', 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', $filename),
['y', 'n', 'a'],
'y'
);
if (strtoupper($response) === 'N') {
$response = '';
while (!$response) {
$response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
$File = new File($this->_output . $response);
$filename = $response;
}
} elseif (strtoupper($response) === 'A') {
$overwriteAll = true;
}
$File->write($output);
$File->close();
}
$File->write($output);
$File->close();
}
}

Expand Down
27 changes: 1 addition & 26 deletions tests/TestCase/Console/Command/Task/ExtractTaskTest.php
Expand Up @@ -104,7 +104,7 @@ 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 = '/\#: (\\\\|\/)extract\.ctp:31\n';
$pattern .= 'msgctxt "mail"/';
$this->assertRegExp($pattern, $result);

Expand All @@ -122,31 +122,6 @@ public function testExecute() {
$this->assertRegExp($pattern, $result);
}

/**
* testExtractCategory method
*
* @return void
*/
public function testExtractCategory() {
$this->Task->interactive = false;

$this->Task->params['paths'] = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages';
$this->Task->params['output'] = $this->path . DS;
$this->Task->params['extract-core'] = 'no';
$this->Task->params['merge'] = 'no';
$this->Task->expects($this->never())->method('err');
$this->Task->expects($this->any())->method('in')
->will($this->returnValue('y'));
$this->Task->expects($this->never())->method('_stop');

$this->Task->main();
$this->assertTrue(file_exists($this->path . DS . 'LC_TIME' . DS . 'default.pot'));

$result = file_get_contents($this->path . DS . 'default.pot');

$this->assertNotContains('You have a new message (category: LC_TIME).', $result);
}

/**
* test exclusions
*
Expand Down

0 comments on commit 447ca25

Please sign in to comment.