Skip to content

Commit f68e102

Browse files
Merge pull request #6 from Wisembly/no-comments
Dumper | Option to dump without comments to avoid git conflicts
2 parents 7a7b4d8 + 211bf4c commit f68e102

7 files changed

Lines changed: 102 additions & 12 deletions

File tree

bin/xgettext

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ $defaults = array(
3232
'-c' => 'UTF-8',
3333
'-e' => array('js', 'html'),
3434
'-l' => 'Javascript',
35+
'--no-comments' => false,
3536
);
3637

3738
while ($i < count($argv)) {
39+
// handle arguments
3840
if (preg_match('#^-[a-z]$#i', $argv[$i])) {
3941
$val = isset($argv[$i+1]) ? trim($argv[$i+1]) : true;
4042

@@ -44,25 +46,35 @@ while ($i < count($argv)) {
4446

4547
$input[$argv[$i]] = is_array($defaults[$argv[$i]]) ? array_merge($input[$argv[$i]], array($val)) : $val;
4648
$i += 2;
47-
} else {
48-
if (is_dir($argv[$i])) {
49-
$input['files'] = array_merge($defaults['files'], Finder::findr($argv[$i], $defaults['-e']));
50-
} else {
51-
$input['files'][] = $argv[$i];
52-
}
49+
continue;
50+
}
51+
52+
// handle modifiers
53+
if (preg_match('#^--[a-z-]+$#i', $argv[$i])) {
54+
$input[$argv[$i]] = true;
5355
$i++;
56+
continue;
5457
}
58+
59+
// handle file list
60+
if (is_dir($argv[$i])) {
61+
$input['files'] = array_merge($defaults['files'], Finder::findr($argv[$i], $defaults['-e']));
62+
} else {
63+
$input['files'][] = $argv[$i];
64+
}
65+
66+
$i++;
5567
}
5668

5769
$input = array_merge($defaults, $input);
5870

5971
try {
60-
new Xgettext($input['files'], $input['-o'], $input['-k'], $input['-l'], $input['-c'], true);
72+
new Xgettext($input['files'], $input['-o'], $input['-k'], $input['-l'], $input['-c'], true, !!$input['--no-comments']);
6173
} catch (\Exception $e) {
6274
fwrite(STDOUT, $e->getMessage() . <<<EOT
6375
6476
65-
Usage xgettext -o [OUTPUT] -k [KEYWORDS] -c [ENCODING] [FILES]
77+
Usage xgettext -o [OUTPUT] -k [KEYWORDS] -c [ENCODING] [--no-comments] [FILES]
6678
6779
-o [OUTPUT]
6880
REQUIRED
@@ -80,6 +92,8 @@ Usage xgettext -o [OUTPUT] -k [KEYWORDS] -c [ENCODING] [FILES]
8092
specify which parser you want to use
8193
eg: -l "Handlebars"
8294
default: "Javascript"
95+
--no-comments
96+
do not dump .po comments with file and line for each msgid occurence
8397
[FILES]
8498
REQUIRED
8599
files list to be parsed

src/Xgettext/Dumper/PoeditDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct($file)
3131
*
3232
* @return boolean
3333
*/
34-
public function dump(PoeditFile $file, $filename = null, $sort = false, $charset = 'UTF-8')
34+
public function dump(PoeditFile $file, $filename = null, $sort = false, $charset = 'UTF-8', $noComments = false)
3535
{
3636
$filename = null !== $filename ? $filename : $this->file;
3737
$content = $file->getHeaders() . PHP_EOL;
@@ -40,7 +40,7 @@ public function dump(PoeditFile $file, $filename = null, $sort = false, $charset
4040
$strings = true === $sort ? $file->sortStrings()->getStrings() : $file->getStrings();
4141

4242
foreach ($strings as $string) {
43-
$content .= true === $sort ? $string->sortReferences()->sortComments()->sortExtracteds()->sortFlags() : $string;
43+
$content .= true === $sort ? $string->sortReferences()->sortComments()->sortExtracteds()->sortFlags()->dump($noComments) : $string->dump($noComments);
4444
}
4545

4646
// ensure that path and file exists

src/Xgettext/Poedit/PoeditPluralString.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ function __construct($key, $pluralForm, array $plurals = array(), array $comment
2626
);
2727
}
2828

29+
public function dump($noComments = false)
30+
{
31+
$string = '';
32+
33+
if (!$noComments) {
34+
$string .= $this->dumpComments();
35+
}
36+
37+
$string .= $this->dumpString(($this->isDeprecated() ? '#~ ' : '') . 'msgid "', $this->key, '"' . PHP_EOL);
38+
$string .= $this->dumpString(($this->isDeprecated() ? '#~ ' : '') . 'msgid_plural "', $this->pluralForm, '"' . PHP_EOL);
39+
$string .= $this->dumpPlurals();
40+
41+
$string .= PHP_EOL;
42+
43+
return $string;
44+
}
45+
2946
public function __toString()
3047
{
3148
$string = '';

src/Xgettext/Poedit/PoeditString.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ function __construct($key, $value = '', array $comments = array(), array $extrac
2424
);
2525
}
2626

27+
public function dump($noComments = false)
28+
{
29+
$string = '';
30+
31+
if (!$noComments) {
32+
$string .= $this->dumpComments();
33+
}
34+
35+
$string .= $this->dumpString(($this->isDeprecated() ? '#~ ' : '') . 'msgid "', $this->key, '"' . PHP_EOL);
36+
$string .= $this->dumpString(($this->isDeprecated() ? '#~ ' : '') . 'msgstr "', $this->value, '"' . PHP_EOL);
37+
$string .= PHP_EOL;
38+
39+
return $string;
40+
}
41+
2742
public function __toString()
2843
{
2944
$string = '';

src/Xgettext/Xgettext.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010

1111
class Xgettext
1212
{
13-
public function __construct(array $files, $output, array $keywords = array('_'), $parser = 'javascript', $enc = 'UTF-8', $cli = false)
13+
public function __construct(
14+
array $files,
15+
$output,
16+
array $keywords = array('_'),
17+
$parser = 'javascript',
18+
$enc = 'UTF-8',
19+
$cli = false,
20+
$noComments = false
21+
)
1422
{
1523
$this->cli = $cli;
1624
$parser = 'Xgettext\\Parser\\' . ucfirst(strtolower($parser)) . 'Parser';
@@ -35,6 +43,6 @@ public function __construct(array $files, $output, array $keywords = array('_'),
3543
}
3644

3745
$poeditDumper = new PoeditDumper($output);
38-
$poeditDumper->dump($poeditFile, null, false, $enc);
46+
$poeditDumper->dump($poeditFile, null, false, $enc, $noComments);
3947
}
4048
}

tests/Xgettext/Tests/Dumper/PoeditDumperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,27 @@ public function testPluralDump()
8787
$this->assertTrue($file->hasString('foo'));
8888
$this->assertEquals($file->getString('foo')->getPlurals(), array('plural one', 'plural two', 'last plural'));
8989
}
90+
91+
public function testDupWithoutComments()
92+
{
93+
$this->file = new PoeditFile();
94+
$this->file->addHeader('"Language: fr\n"');
95+
$this->file->addString(new PoeditString('foo', 'bar', array('baz', 'foo:56', 'foo:35','bar')));
96+
$this->file->addString(new PoeditPluralString('One foo', '{{ count }} foos', array('Un foo', '{{ count }} fifoos'), array('baz', 'foo:56', 'foo:35', 'bar')));
97+
98+
$filename = $this->generateRandomFileName('json');
99+
$basePath = __DIR__ . '/../Resources/dump';
100+
$output = $basePath . '/' . $filename;
101+
102+
$dumper = new PoeditDumper($output);
103+
$dumper->dump($this->file, null, false, 'UTF-8', true);
104+
105+
$parser = new PoeditParser($output);
106+
$content = file_get_contents($output);
107+
unlink($output);
108+
109+
$expect = file_get_contents(__DIR__ . '/../Resources/testDump.po');
110+
111+
$this->assertEquals($content, $expect);
112+
}
90113
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
msgid ""
2+
msgstr """Language: fr\n"
3+
4+
"Content-Type: text/plain; charset=UTF-8\n"
5+
6+
msgid "foo"
7+
msgstr "bar"
8+
9+
msgid "One foo"
10+
msgid_plural "{{ count }} foos"
11+
msgstr[0] "Un foo"
12+
msgstr[1] "{{ count }} fifoos"
13+

0 commit comments

Comments
 (0)