Skip to content

Commit 44756ea

Browse files
committed
Adding a new option to i18n shell to generate .pot files without sentence references
1 parent 47dd42a commit 44756ea

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

lib/Cake/Console/Command/Task/ExtractTask.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class ExtractTask extends AppShell {
4848
*/
4949
protected $_merge = false;
5050

51+
/**
52+
* Add headers for each sentence showing references
53+
*
54+
* @var bool
55+
*/
56+
protected $_headers = true;
57+
5158
/**
5259
* Current file being processed
5360
*
@@ -224,6 +231,10 @@ public function execute() {
224231
$this->_merge = strtolower($response) === 'y';
225232
}
226233

234+
if (isset($this->params['headers'])) {
235+
$this->_headers = !(strtolower($this->params['headers']) === 'no');
236+
}
237+
227238
if (empty($this->_files)) {
228239
$this->_searchFiles();
229240
}
@@ -316,6 +327,9 @@ public function getOptionParser() {
316327
))->addOption('merge', array(
317328
'help' => __d('cake_console', 'Merge all domain and category strings into the default.po file.'),
318329
'choices' => array('yes', 'no')
330+
))->addOption('headers', array(
331+
'help' => __d('cake_console', 'Add headers for each sentence showing references'),
332+
'choices' => array('yes', 'no')
319333
))->addOption('output', array(
320334
'help' => __d('cake_console', 'Full path to output directory.')
321335
))->addOption('files', array(
@@ -572,14 +586,17 @@ protected function _buildFiles() {
572586
foreach ($translations as $msgid => $contexts) {
573587
foreach ($contexts as $context => $details) {
574588
$plural = $details['msgid_plural'];
575-
$files = $details['references'];
576-
$occurrences = array();
577-
foreach ($files as $file => $lines) {
578-
$lines = array_unique($lines);
579-
$occurrences[] = $file . ':' . implode(';', $lines);
589+
$header = '';
590+
if ($this->_headers) {
591+
$files = $details['references'];
592+
$occurrences = array();
593+
foreach ($files as $file => $lines) {
594+
$lines = array_unique($lines);
595+
$occurrences[] = $file . ':' . implode(';', $lines);
596+
}
597+
$occurrences = implode("\n#: ", $occurrences);
598+
$header = '#: ' . str_replace(DS, '/', str_replace($paths, '', $occurrences)) . "\n";
580599
}
581-
$occurrences = implode("\n#: ", $occurrences);
582-
$header = '#: ' . str_replace(DS, '/', str_replace($paths, '', $occurrences)) . "\n";
583600

584601
$sentence = '';
585602
if ($context) {

lib/Cake/Test/Case/Console/Command/Task/ExtractTaskTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,35 @@ public function testExtractCategory() {
221221
$this->assertNotRegExp($pattern, $result);
222222
}
223223

224+
/**
225+
* testExtractWithoutHeaders method
226+
*
227+
* @return void
228+
*/
229+
public function testExtractWithoutHeaders() {
230+
$this->Task->interactive = false;
231+
232+
$this->Task->params['paths'] = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages';
233+
$this->Task->params['output'] = $this->path . DS;
234+
$this->Task->params['extract-core'] = 'no';
235+
$this->Task->params['merge'] = 'no';
236+
$this->Task->params['headers'] = 'no';
237+
238+
$this->Task->expects($this->never())->method('err');
239+
$this->Task->expects($this->any())->method('in')
240+
->will($this->returnValue('y'));
241+
$this->Task->expects($this->never())->method('_stop');
242+
243+
$this->Task->execute();
244+
$this->assertTrue(file_exists($this->path . DS . 'LC_NUMERIC' . DS . 'default.pot'));
245+
$this->assertFalse(file_exists($this->path . DS . 'LC_TIME' . DS . 'default.pot'));
246+
247+
$result = file_get_contents($this->path . DS . 'default.pot');
248+
249+
$pattern = '/\n\#: .*\n/';
250+
$this->assertNotRegExp($pattern, $result);
251+
}
252+
224253
/**
225254
* test exclusions
226255
*

0 commit comments

Comments
 (0)