Qgoda/Command/Po.pm untested

filter: Statementstmt 0.0% 0 / 426 Branchbran 0.0% 0 / 122 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 38 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 619 CC 62 SCAR 38.3
CC
62
Cov
0%
_run
132.0
__makePOTFILES
110.0
__addMissingPACKAGE
90.0
CRAP 46.3
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Command::Po;
20 $Qgoda::Command::Po::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain 1.28 qw(qgoda);
24 use File::Spec;
25 use File::Temp;
26 use File::Copy qw(copy);
27 use File::Path qw(make_path);
28 use File::Globstar qw(globstar);
29 use Cwd qw(getcwd);
30  
31 use Qgoda;
32 use Qgoda::Util qw(empty write_file);
33 use Qgoda::Util::Translate qw(get_masters);
34 use Qgoda::CLI;
35 use Qgoda::Site;
36  
37 use base 'Qgoda::Command';
38  
39 my $seed_repo = 'file://' . $ENV{HOME} . '/perl/Template-Plugin-Gettext-Seed';
40  
41 sub _run {
42 my ($self, $args, $global_options, %options) = @_;
43  
44 Qgoda::CLI->commandUsageError('po', __"no target specified",
45 'po [OPTIONS] TARGET')
46 if !@$args;
47
48 Qgoda::CLI->commandUsageError('po', __"only one target may be specified",
49 'po [OPTIONS] TARGET')
50 if 1 != @$args;
51
52 my $target = $args->[0];
53  
54 my $qgoda = Qgoda->new($global_options);
55  
56 my $config = $qgoda->config;
57 my $logger = $qgoda->logger;
58  
59 my $textdomain = $config->{po}->{textdomain};
60 $logger->fatal(__"configuration variable 'po.textdomain' not set")
61 if empty $textdomain;
62 my $linguas = $config->{linguas};
63 $logger->fatal(__"configuration variable 'linguas' not set or empty")
64 if empty $linguas || !@$linguas;
65  
66 my @missing;
67 if ('reset' eq $target) {
68 @missing = '', qw(Makefile PACKAGE PLFILES GitIgnore QgodaINC);
69 } else {
70 @missing = $self->__checkFiles;
71 }
72  
73 foreach my $missing (@missing) {
74 my $method = '__addMissing' . $missing;
75 $self->$method;
76 }
77 return $self if 'reset' eq $target;
78  
79 my $here = getcwd;
80 if (!defined $here) {
81 $logger->fatal(__x("cannot get current working directory: {error}",
82 error => $!));
83 }
84  
85 my $podir = $config->{paths}->{po};
86 if (!chdir $podir) {
87 $logger->fatal(__x("cannot change directory to '{directory}': {error}",
88 directory => $podir, error => $!));
89 }
90  
91 return $self->__make($target) if !empty $config->{po}->{make};
92  
93 my %methods = (
94 potfiles => '__makePOTFILES',
95 pot => '__makePOT',
96 'update-po' => '__makeUpdatePO',
97 'update-mo' => '__makeUpdateMO',
98 install => '__makeInstall',
99 all => '__makeAll',
100 );
101  
102 my $method = $methods{lc $target};
103  
104 unless ($method && $self->can($method)) {
105 my $msg = __x("unsupported target '{target}'", target => $target);
106  
107 Qgoda::CLI->commandUsageError('po', $msg, 'po [OPTIONS] TARGET');
108 }
109  
110 return $self->$method($here);
111 }
112  
113 sub __checkFiles {
114 my ($self) = @_;
115  
116 my $qgoda = Qgoda->new;
117  
118 my $config = $qgoda->config;
119 my $logger = $qgoda->logger;
120  
121 my $podir = $config->{paths}->{po};
122  
123 my @missing;
124  
125 $logger->debug(__"checking for missing files in po directory");
126  
127 push @missing, '' if !-e $podir;
128  
129 my $makefile = File::Spec->catfile($podir, 'Makefile');
130 push @missing, 'Makefile' if !-e $makefile;
131  
132 my $package = File::Spec->catfile($podir, 'PACKAGE');
133 push @missing, 'PACKAGE' if !-e $package;
134  
135 my $plfiles = File::Spec->catfile($podir, 'PLFILES');
136 push @missing, 'PLFILES' if !-e $plfiles;
137  
138 my $git_ignore = File::Spec->catfile($podir, '.gitignore');
139 push @missing, 'GitIgnore' if !-e $git_ignore;
140  
141 my $qgoda_inc = File::Spec->catfile($podir, 'qgoda.inc');
142 push @missing, 'QgodaINC' if !-e $qgoda_inc;
143  
144 return @missing;
145 }
146  
147 # Unfortunate name ... it's for the directory itself.
148 sub __addMissing {
149 my ($self) = @_;
150  
151 my $qgoda = Qgoda->new;
152 my $logger = $qgoda->logger;
153 my $config = $qgoda->config;
154  
155 my $podir = $config->{paths}->{po};
156 $logger->info(__x("creating '{filename}'", filename => $podir));
157  
158 if (!mkdir $podir) {
159 $logger->fatal(__x("error creating directory '{directory}': {error}",
160 directory => $podir,
161 error => $!));
162 }
163  
164 return $self;
165 }
166  
167 sub __addMissingPACKAGE {
168 my ($self) = @_;
169  
170 my $qgoda = Qgoda->new;
171 my $logger = $qgoda->logger;
172 my $config = $qgoda->config;
173 my $po_config = $config->{po};
174  
175 my $podir = $config->{paths}->{po};
176  
177 my $package = File::Spec->catfile($podir, 'PACKAGE');
178 $logger->info(__x("creating '{filename}'", filename => $package));
179  
180 my $header_comment = $self->__comment(__(<<EOF));
181 Makefile snippet holding package-dependent information. Please adhere
182 to Makefile syntax!
183 EOF
184  
185 my $linguas_comment = $self->__comment(__(<<EOF));
186 Space-separated list of language codes. Omit the base language!
187 EOF
188 chomp $linguas_comment;
189  
190 my @linguas = @{$config->{linguas}};
191 shift @linguas;
192 my $linguas = join ' ', @linguas;
193  
194 my $textdomain_comment = $self->__comment(__(<<EOF));
195 Textdomain of the site, for example reverse domain name.
196 EOF
197 chomp $textdomain_comment;
198 my $textdomain = $po_config->{textdomain};
199  
200 my $msgid_bugs_address_comment = $self->__comment(__(<<EOF));
201 Where to send msgid bug reports?
202 EOF
203 chomp $msgid_bugs_address_comment;
204 my $msgid_bugs_address = $po_config->{msgid_bugs_address};
205 $msgid_bugs_address = __"Please set MSGID_BUGS_ADDRESS in 'PACKAGE'"
206 if empty $msgid_bugs_address;
207
208 my $copyright_holder_comment = $self->__comment(__(<<EOF));
209 Initial copyright holder added to pot and po files.
210 EOF
211 chomp $copyright_holder_comment;
212 my $copyright_holder = $po_config->{copyright_holder};
213 $copyright_holder = __"Please set COPYRIGHT_HOLDER in 'PACKAGE'"
214 if empty $copyright_holder;
215  
216 my $override_comment = $self->__comment(__(<<EOF));
217 Override these default values as needed.
218 EOF
219 chomp $override_comment;
220  
221 my $xgettext_line = empty $po_config->{xgettext}
222 ? "#XGETTEXT = xgettext" : "XGETTEXT = $config->{po}->{xgettext}";
223 my $xgettext_tt2_line = empty $po_config->{xgettext_tt2}
224 ? "#XGETTEXT_TT2 = xgettext-tt2"
225 : "XGETTEXT = $config->{po}->{xgettext_tt2}";
226 my $msgmerge_line = empty $po_config->{msgmerge}
227 ? "#MSGMERGE = msgmerge" : "MSGMERGE = $config->{po}->{msgmerge}";
228 my $msgfmt_line = empty $po_config->{msgfmt}
229 ? "#MSGFMT = msgfmt" : "MSGFMT = $config->{po}->{msgfmt}";
230 my $qgoda_line = empty $po_config->{qgoda}
231 ? "QGODA = qgoda" : "QGODA = $config->{po}->{qgoda}";
232  
233 my $contents = <<EOF;
234 $header_comment
235 $linguas_comment
236 LINGUAS = $linguas
237  
238 $textdomain_comment
239 TEXTDOMAIN = $textdomain
240  
241 $msgid_bugs_address_comment
242 MSGID_BUGS_ADDRESS = $msgid_bugs_address
243  
244 $copyright_holder_comment
245 COPYRIGHT_HOLDER = $copyright_holder
246  
247 $override_comment
248 $xgettext_line
249 $xgettext_tt2_line
250 $msgmerge_line
251 $msgfmt_line
252 $qgoda_line
253 EOF
254  
255 if (!write_file $package, $contents) {
256 $logger->fatal(__x("error writing '{filename}': {error}",
257 filename => $package,
258 error => $!));
259 }
260  
261 return $self;
262 }
263  
264 sub __addMissingMakefile {
265 my ($self) = @_;
266  
267 # Fail early if Git is not installed.
268 require Git;
269  
270 my $qgoda = Qgoda->new;
271 my $logger = $qgoda->logger;
272 my $config = $qgoda->config;
273 my $po_config = $config->{po};
274  
275 my $podir = $config->{paths}->{po};
276  
277 my $makefile = File::Spec->catfile($podir, 'Makefile');
278 $logger->info(__x("creating '{filename}'", filename => $makefile));
279 $logger->info(__x("cloning git repository '{repo}'",
280 repo => $seed_repo));
281  
282 my $tmp = File::Temp->newdir;
283 $logger->debug(__x("created temporary directory '{dir}'",
284 dir => $tmp));
285  
286 Git::command('clone', '--depth', '1', $seed_repo, $tmp);
287  
288 my $remote = File::Spec->catfile($tmp, 'po', 'Makefile');
289 $logger->debug(__x("copying '{from}' to '{to}'",
290 from => $remote, to => $makefile));
291  
292 if (!copy $remote, $podir) {
293 $logger->fatal(__x("error copying '{from}' to '{to}'",
294 from => $remote, to => $makefile))
295 }
296  
297 return $self;
298 }
299  
300 sub __addMissingPLFILES {
301 my ($self) = @_;
302  
303 my $qgoda = Qgoda->new;
304 my $logger = $qgoda->logger;
305 my $config = $qgoda->config;
306  
307 my $podir = $config->{paths}->{po};
308  
309 my $plfiles = File::Spec->catfile($podir, 'PLFILES');
310 $logger->info(__x("creating '{filename}'", filename => $plfiles));
311  
312 if (!write_file $plfiles, '') {
313 $logger->fatal(__x("error writing '{filename}': {error}",
314 filename => $plfiles,
315 error => $!));
316 }
317  
318 return $self;
319 }
320  
321 sub __addMissingGitIgnore {
322 my ($self) = @_;
323  
324 my $qgoda = Qgoda->new;
325 my $logger = $qgoda->logger;
326 my $config = $qgoda->config;
327  
328 my $podir = $config->{paths}->{po};
329  
330 my $gitignore = File::Spec->catfile($podir, '.gitignore');
331 $logger->info(__x("creating '{filename}'", filename => $gitignore));
332  
333 my $ignore_list = <<EOF;
334 /*.gmo
335 /*.mo
336 EOF
337  
338 if (!write_file $gitignore, $ignore_list) {
339 $logger->fatal(__x("error writing '{filename}': {error}",
340 filename => $gitignore,
341 error => $!));
342 }
343  
344 return $self;
345 }
346  
347 sub __addMissingQgodaINC {
348 my ($self) = @_;
349  
350 my $qgoda = Qgoda->new;
351 my $logger = $qgoda->logger;
352 my $config = $qgoda->config;
353  
354 my $podir = $config->{paths}->{po};
355  
356 my $qgoda_inc = File::Spec->catfile($podir, 'qgoda.inc');
357 $logger->info(__x("creating '{filename}'", filename => $qgoda_inc));
358  
359 my $include = <<'EOF';
360 # Makefile snippet for Qgoda. Extract all strings from Markdown files that
361 # serve as the base for translated documents.
362  
363 MDPOTFILES = $(srcdir)/MDPOTFILES \
364 $(shell cat $(srcdir)/MDPOTFILES)
365  
366 $(srcdir)/markdown.pot: $(srcdir)/MDPOTFILES $(MDPOTFILES)
367 $(QGODA) xgettext --output=$(srcdir)/markdown.pox --from-code="utf-8" \
368 --add-comments=TRANSLATORS: --files-from=$(srcdir)/MDPOTFILES \
369 --copyright-holder='$(COPYRIGHT_HOLDER)' --force-po \
370 --msgid-bugs-address='$(MSGID_BUGS_ADDRESS)' && \
371 rm -f $@ && mv $(srcdir)/markdown.pox $@
372 EOF
373  
374 if (!write_file $qgoda_inc, $include) {
375 $logger->fatal(__x("error writing '{filename}': {error}",
376 filename => $qgoda_inc,
377 error => $!));
378 }
379  
380 return $self;
381 }
382  
383 sub __comment {
384 my ($self, $text) = @_;
385  
386 $text =~ s/^/# /gm;
387  
388 return $text;
389 }
390  
391 sub __expandCommand {
392 my ($self, $cmd) = @_;
393  
394 if (ref $cmd) {
395 return @$cmd;
396 }
397  
398 return $cmd;
399 }
400  
401 sub __command {
402 my ($self, @args) = @_;
403  
404 my @pretty;
405 foreach my $arg (@args) {
406 my $pretty = $arg;
407 $pretty =~ s{(["\\\$])}{\\$1}g;
408 $pretty = qq{"$pretty"} if $pretty =~ /[ \t]/;
409 push @pretty, $pretty;
410 }
411  
412 my $pretty = join ' ', @pretty;
413 my $logger = Qgoda->new->logger;
414  
415 $logger->info(__x("execute: {cmd}", cmd => $pretty));;
416  
417 system @args;
418 }
419  
420 sub __fatalCommand {
421 my ($self, @args) = @_;
422  
423 return $self if 0 == $self->__command(@args);
424  
425 my @pretty;
426 foreach my $arg (@args) {
427 my $pretty = $arg;
428 $pretty =~ s{(["\\\$])}{\\$1}g;
429 $pretty = qq{"$pretty"} if $pretty =~ /[ \t]/;
430 push @pretty, $pretty;
431 }
432  
433 my $pretty = join ' ', @pretty;
434  
435 my $logger = Qgoda->new->logger;
436  
437 if ($? == -1) {
438 $logger->fatal(__x("{command}: failed to execute: {error}",
439 command => $pretty, error => $!));;
440 } elsif ($? & 127) {
441 $logger->fatal(__x("{command}: died with signal {signo}",
442 command => $pretty, signo => $? & 127));
443 }
444  
445 $logger->fatal(__x("{command}: terminated with exit code {code}",
446 command => $pretty, code => $? >> 8));
447 }
448  
449 sub __safeRename {
450 my ($self, $from, $to) = @_;
451  
452 my $qgoda = Qgoda->new;
453 my $logger = $qgoda->logger;
454  
455 $logger->info(__x("rename '{from}' to '{to}'",
456 from => $from, to => $to));
457
458 return $self if rename $from, $to;
459  
460 $logger->fatal(__x("error renaming '{from}' to '{to}': {error}",
461 from => $from, to => $to, error => $!));
462 }
463  
464 sub __outOfDate {
465 my ($self, $target, @deps) = @_;
466  
467 my @ref = stat $target or return 1;
468  
469 foreach my $dep (@deps) {
470 my @stat = stat $dep or return 1;
471 return 1 if $stat[9] > $ref[9];
472 }
473  
474 return;
475 }
476  
477 sub __filelist {
478 my ($self, $filelist) = @_;
479  
480 my $fh;
481 if (!open $fh, '<', $filelist) {
482 my $logger = Qgoda->new->logger;
483  
484 $logger->fatal(__x("error reading '{filename}': {error}",
485 filename => $filelist, error => $!));
486 }
487  
488 return grep { length }
489 map { my $u = $_; $u =~ s/^[ \r\t]*//; $u =~ s/[ \t\r\n]*$//; $u } <$fh>;
490 }
491  
492 sub __make {
493 my ($self, $target) = @_;
494  
495 my $qgoda = Qgoda->new;
496 my $config = $qgoda->config;
497 my $logger = $qgoda->logger;
498  
499 my $make = $config->{po}->{make};
500  
501 $self->__fatalCommand($make, $target);
502  
503 return $self;
504 }
505  
506 sub __makePOTFILES {
507 my ($self, $srcdir) = @_;
508  
509 my $qgoda = Qgoda->new;
510 my $config = $qgoda->config;
511 my $logger = $qgoda->logger;
512  
513 my $viewspatterns = $config->{po}->{views};
514 if (!$viewspatterns) {
515 my $pattern = $config->{paths}->{views};
516 $pattern =~ s{/+$}{};
517 die __"default views directory is '/', please configure 'po.views'"
518 if !length $pattern;
519 $pattern .= '/**';
520 $viewspatterns = [$pattern];
521 }
522  
523 my $podir = getcwd;
524 if (!chdir $srcdir) {
525 die __x("error changing working directory to '{dir}': {error}\n",
526 dir => $srcdir, error => $!);
527 }
528  
529 my %files;
530 foreach my $pattern (@$viewspatterns) {
531 my $negated = $pattern =~ s/^!//;
532 # Force path to be relative.
533 $pattern =~ s{^/+}{};
534 my @files = globstar $pattern;
535 foreach my $found (@files) {
536 if (-d $found) {
537 # Skip directory.
538 } elsif ($negated) {
539 $logger->debug(__x("removing template file '{filename}'",
540 filename => $found));
541 delete $files{$found};
542 } else {
543 $logger->debug(__x("adding template file '{filename}'",
544 filename => $found));
545 $files{$found} = 1;
546 }
547 }
548 }
549  
550 my @files;
551 foreach my $path (keys %files) {
552 my $abspath = File::Spec->rel2abs($path);
553 my $relpath = File::Spec->abs2rel($abspath, $podir);
554 if ($config->{scm}) {
555 next unless $qgoda->versionControlled($abspath, 1);
556 }
557 push @files, $relpath;
558 }
559  
560 push @files, 'markdown.pot', 'perl.pot';
561  
562 if (!chdir $podir) {
563 die __x("error changing working directory to '{dir}': {error}\n",
564 dir => $podir, error => $!);
565 }
566  
567 my $potfiles = join "\n", sort @files;
568 my $display_name = File::Spec->catfile($config->{paths}->{po},
569 'POTFILES');
570 $logger->info(__x("writing '{filename}'", filename => $display_name));
571 unless (write_file 'POTFILES', "$potfiles\n") {
572 $logger->fatal(__x("error writing '{filename}': {error}",
573 filename => $display_name, error => $!));
574 }
575  
576 $self->__makeMDPOTFILES($srcdir);
577  
578 return $self;
579 }
580  
581 sub __makeMDPOTFILES {
582 my ($self, $srcdir) = @_;
583  
584 my $qgoda = Qgoda->new;
585 my $config = $qgoda->config;
586 my $logger = $qgoda->logger;
587  
588 my $podir = getcwd;
589 if (!chdir $srcdir) {
590 die __x("error changing working directory to '{dir}': {error}\n",
591 dir => $srcdir, error => $!);
592 }
593  
594 my %masters = get_masters;
595 my @files;
596 foreach my $path (keys %masters) {
597 if ($config->{scm}) {
598 next unless $qgoda->versionControlled($path);
599 }
600 my $relpath = File::Spec->abs2rel($path, $podir);
601 push @files, $relpath;
602 }
603  
604 if (!chdir $podir) {
605 die __x("error changing working directory to '{dir}': {error}\n",
606 dir => $podir, error => $!);
607 }
608  
609 my $potfiles = join "\n", sort @files;
610 my $display_name = File::Spec->catfile($config->{paths}->{po},
611 'MDPOTFILES');
612 $logger->info(__x("writing '{filename}'", filename => $display_name));
613 unless (write_file 'MDPOTFILES', "$potfiles\n") {
614 $logger->fatal(__x("error writing '{filename}': {error}",
615 filename => $display_name, error => $!));
616 }
617  
618 return $self;
619 }
620  
621 sub __makePOT {
622 my ($self) = @_;
623  
624 my $qgoda = Qgoda->new;
625 my $logger = $qgoda->logger;
626 my $po_config = $qgoda->config->{po};
627  
628 # FIXME! Check dependencies!
629 my ($pox, $pot) = ('perl.pox', 'perl.pot');
630 my @options = split / /, Locale::TextDomain->options;
631 my @cmd = ($self->__expandCommand($po_config->{xgettext}),
632 "--output=$pox", "--from-code=utf-8",
633 "--add-comments=TRANSLATORS:", "--files-from=PLFILES",
634 "--copyright-holder='$po_config->{copyright_holder}'",
635 "--force-po",
636 "--msgid-bugs-address='$po_config->{msgid_bugs_address}'",
637 @options);
638 $self->__fatalCommand(@cmd);
639 $logger->info(__x("unlink '{filename}'", filename => $pot));
640 unlink $pot;
641 $self->__safeRename($pox, $pot);
642  
643 ($pox, $pot) = ("markdown.pox",
644 "markdown.pot");
645 @cmd = ($self->__expandCommand($po_config->{qgoda}), "xgettext",
646 "--output=$pox", "--from-code=utf-8",
647 "--add-comments=TRANSLATORS:", "--files-from=MDPOTFILES",
648 "--copyright-holder='$po_config->{copyright_holder}'",
649 "--force-po",
650 "--msgid-bugs-address='$po_config->{msgid_bugs_address}'");
651 $self->__fatalCommand(@cmd);
652 $logger->info(__x("unlink '{filename}'", filename => $pot));
653 unlink $pot;
654 $self->__safeRename($pox, $pot);
655  
656 ($pox, $pot) = ("$po_config->{textdomain}.pox",
657 "$po_config->{textdomain}.pot");
658 @cmd = ($self->__expandCommand($po_config->{xgettext_tt2}),
659 "--output=$pox", "--from-code=utf-8",
660 "--add-comments=TRANSLATORS:", "--files-from=POTFILES",
661 "--copyright-holder='$po_config->{copyright_holder}'",
662 "--force-po",
663 "--msgid-bugs-address='$po_config->{msgid_bugs_address}'");
664 $self->__fatalCommand(@cmd);
665 $logger->info(__x("unlink '{filename}'", filename => $pot));
666 unlink $pot;
667 $self->__safeRename($pox, $pot);
668  
669 return $self;
670 }
671  
672 sub __makeUpdatePO {
673 my ($self) = @_;
674  
675 my $qgoda = Qgoda->new;
676 my $logger = $qgoda->logger;
677 my $config = $qgoda->config;
678 my $po_config = $config->{po};
679  
680 my @deps = $self->__filelist('PLFILES');
681 push @deps, $self->__filelist('MDPOTFILES');
682 push @deps, $self->__filelist('POTFILES');
683 push @deps, 'PLFILES', 'MDPOTFILES', 'POTFILES';
684 $self->__makePOT if $self->__outOfDate("$config->{po}->{textdomain}.pot",
685 @deps);
686  
687 my @linguas = @{$config->{linguas}};
688 shift @linguas;
689  
690 my $errors = 0;
691 foreach my $lang (@linguas) {
692 $logger->info(__x("merging {filename}", filename => "$lang.po"));
693  
694 $self->__safeRename("$lang.po", "$lang.old.po");
695  
696 my @cmd = ($self->__expandCommand($po_config->{msgmerge}),
697 "$lang.old.po", "$po_config->{textdomain}.pot",
698 '--previous',
699 '-o', "$lang.po");
700 if (0 == $self->__command(@cmd)) {
701 $logger->info(__x("unlink '{filename}'",
702 filename => "$lang.old.po"));
703 unlink "$lang.old.po";
704 } else {
705 ++$errors;
706 $logger->error(__x("Merging {filename} failed",
707 filename => "$lang.po"));
708 $self->__safeRename("$lang.old.po", "$lang.po");
709 }
710 }
711  
712 exit 1 if $errors;
713  
714 return $self;
715 }
716  
717 sub __makeUpdateMO {
718 my ($self) = @_;
719  
720 my $qgoda = Qgoda->new;
721 my $logger = $qgoda->logger;
722 my $config = $qgoda->config;
723 my $po_config = $config->{po};
724  
725 my @linguas = @{$config->{linguas}};
726 shift @linguas;
727  
728 my @deps = $self->__filelist('PLFILES');
729 push @deps, $self->__filelist('MDPOTFILES');
730 push @deps, $self->__filelist('POTFILES');
731 push @deps, 'PLFILES', 'MDPOTFILES', 'POTFILES';
732  
733 foreach my $lang (@linguas) {
734 if ($self->__outOfDate("$lang.po", @deps)) {
735 $self->__makeUpdatePO;
736 last;
737 }
738 }
739  
740 foreach my $lang (@linguas) {
741 $self->__makeUpdatePO if $self->__outOfDate("$lang.po", @deps);
742 my @cmd = ($self->__expandCommand($po_config->{msgfmt}), "--check",
743 "--statistics", "--verbose",
744 '-o', "$lang.gmo", "$lang.po");
745 $self->__fatalCommand(@cmd);
746 }
747  
748 return $self;
749 }
750  
751 sub __makeInstall {
752 my ($self, $srcdir) = @_;
753  
754 my $qgoda = Qgoda->new;
755 my $logger = $qgoda->logger;
756 my $config = $qgoda->config;
757 my $po_config = $config->{po};
758  
759 my @linguas = @{$config->{linguas}};
760 shift @linguas;
761  
762 my @deps = $self->__filelist('PLFILES');
763 push @deps, $self->__filelist('MDPOTFILES');
764 push @deps, $self->__filelist('POTFILES');
765 push @deps, 'PLFILES', 'MDPOTFILES', 'POTFILES';
766 push @deps, map { "$_.po" } @linguas;
767  
768 foreach my $lang (@linguas) {
769 if ($self->__outOfDate("$lang.gmo", @deps)) {
770 $self->__makeUpdateMO;
771 last;
772 }
773 }
774  
775 my $targetdir = File::Spec->catfile($srcdir, 'LocaleData');
776 foreach my $lang (@linguas) {
777 my $destdir = File::Spec->catfile($targetdir, $lang, 'LC_MESSAGES');
778 if (!-e $destdir) {
779 $logger->info(__x("create directory '{directory}'",
780 directory => $destdir));
781 make_path $destdir if !-e $destdir;
782 }
783 my $dest = File::Spec->catfile($destdir, "$po_config->{textdomain}.mo");
784 $logger->info(__x("copy '{from}' to '{to}'",
785 from => "$lang.gmo", to => $dest));
786 if (!copy "$lang.gmo", "$dest") {
787 $logger->fatal(__x("cannot copy '{from}' to '{to}': {error}",
788 from => "$lang.gmo", to => $dest, error => $!));
789
790 }
791 }
792  
793 return $self;
794 }
795  
796 sub __makeAll {
797 my ($self, $srcdir) = @_;
798  
799 $self->__makePOTFILES($srcdir);
800 $self->__makePOT($srcdir);
801 $self->__makeUpdatePO($srcdir);
802 $self->__makeUpdateMO($srcdir);
803 $self->__makeInstall($srcdir);
804 }
805  
806 1;
807  
808 =head1 NAME
809  
810 qgoda po - Translation workflow based on PO files
811  
812 =head1 SYNOPSIS
813  
814 qgoda po [<global options>] potfiles
815 qgoda po [<global options>] pot
816 qgoda po [<global options>] update-po
817 qgoda po [<global options>] update-mo
818 qgoda po [<global options>] install
819  
820 Or for all of the above:
821  
822 qgoda po [<global options>] all
823  
824 Try 'qgoda --help' for a description of global options.
825  
826 =head1 DESCRIPTION
827  
828 You will find tutorial style information for the internationalization (i18n)
829 of Qgoda sites at L<http://www.qgoda.net/en/docs/i18n/>. The following
830 is rather meant as a quick reference.
831  
832 Updates, compiles and installs translations. You need the GNU gettext tools
833 as a prerequisite. They are available from your package manager as either
834 "gettext-tools", "gettext-dev", or just "gettext". A package "gettext-runtime"
835 is neither needed nor sufficient.
836  
837 Everything related to translations resides in the directory F<_po>,
838 respectively the directory that the configuration variable C<paths.po>
839 in F<_config.yaml> points to.
840  
841 The directory is autmatically created and populated when necessary. It is
842 necessary, if you use L<Template::Plugin::Gettext> in one of your templates:
843  
844 [% USE gtx = Gettext('com.example.www', asset.lingua) %]
845  
846 Creation of the F<_po> directory is also triggered, if one of your site's
847 markdown documents refers to another from the document variable C<master>
848  
849 ---
850 master: /en/about.md
851 ---
852  
853 Additionally, you must set the textdomain of your site in the configuration
854 variable C<po.textdomain>. The textdomain is
855 the name under which translations are saved, usually the reverse domain name
856 of your site. The textdomain for L<http://www.qgoda.net/> is for example
857 C<net.qgoda.www>.
858  
859 The F<_po> directory contains the following files:
860  
861 =over 4
862  
863 =item B<PACKAGE>
864  
865 Contains the basic configuration of your site suitable for a Makefile and it
866 is only needed if you use the Makefile. The file is
867 auto-generated but then left untouched so that you can edit it to your needs.
868  
869 In order to reset the file to the latest upstream version, delete it and then
870 run C<qgoda build> or C<qgoda watch> once. Alternatively, run C<qgoda po
871 reset>.
872  
873 The following configuration variables in F<_config.yaml> are evaluated for
874 the creation of F<PACKAGE>:
875  
876 =over 8
877  
878 =item B<linguas>
879  
880 An array of language codes, for example:
881  
882 linguas: [en, fr, de, bg]
883  
884 The first value is assumed to be the base language of your site.
885  
886 =item B<po.textdomain>
887  
888 Your site's textdomain, for example C<com.example.www>.
889  
890 =item B<po.msgid_bugs_address>
891  
892 An email address or web site for issuing errors in translatable strings.
893 Translators will use that address for reporting problems with translating
894 your site. See below for an example.
895  
896 =item B<po.copyright_holder>
897  
898 The copyright holder that should be put into the header of the master
899 translation file F<TEXTDOMAIN.pot>.
900  
901 =item B<po.xgettext>
902  
903 The xgettext(1) program on your system, defaults to just "xgettext".
904  
905 =item B<po.xgettext_tt2>
906  
907 The xgettext-tt2(1) program on your system, defaults to just "xgettext-tt2".
908  
909 =item B<po.qgoda>
910  
911 The qgoda(1) program on your system, defaults to just "qgoda".
912  
913 =item B<po.msgmerge>
914  
915 The msgmerge(1) program on your system, defaults to just "msgmerge".
916  
917 =item B<po.msgfmt>
918  
919 The msgfmt(1) program on your system, defaults to just "msgfmt".
920  
921 =back
922  
923 For all configuration variables above that expect a command name, you can
924 use a single value or a list, if you want to pass options to the command.
925  
926 Example configuration for F<PACKAGE>:
927  
928 linguas: [en, fr, de, bg]
929 po:
930 textdomain: com.example.www
931 msgid_bugs_address: John Doe <po-bugs@example.com>
932 copyright_holder: Acme Ltd. <http://www.example.com>
933  
934 When you change the configuration, you have to either delete the generated
935 files from the F<_po> directory or run C<qgoda po reset> in order to
936 update them.
937  
938 =item B<po-make.pl>
939  
940 This script is responsible for invoking the necessary helper programs as
941 needed. Try C<perl po-make.pl --help> for usage information if you want to
942 run the program yourself. The result will be the same as using
943 C<qgoda po TARGET>.
944  
945 =item B<Makefile>
946  
947 Does essentially the same as B<po-make.pl> but is smarter at dependency
948 handling. You can run the Makefile manually with the C<make(1)> command
949 or use the option C<--make> (see <L/OPTIONS>) or set the configuration
950 variable C<po.make>.
951  
952 =item B<PLFILES>
953  
954 A list of Perl source files for your site. If missing, an empty file is
955 automatically generated but you have to maintain it manually.
956  
957 =item B<MDPOTFILES>
958  
959 A list of Markdown documents found in your site that are referenced by
960 documents that have to be translated. This file gets generated with
961 the command "qgoda po potfiles".
962  
963 By default, all assets with front matter are taken into account for the
964 creation of this list. You can add additional search patterns to the
965 configuration variable C<po.mdextra>.
966  
967 =item B<POTFILES>
968  
969 By default, all template files from the view directory (configuration
970 variable C<paths.views>) which defaults to F<_views> are added. You
971 can override that setting with the configuration variable C<po.views>
972 (this is not a typo). The default setting is:
973  
974 po:
975 views:
976 - /_views/**
977  
978 You can extend or reduce that list like this:
979  
980 po:
981 views:
982 - /_views/**
983 - !/_views/**/*.bak
984  
985 Note, that this file will normally also contain a line for F<./MDPOTFILES>.
986 This line has the effect that all translatable strings in Markdown files
987 are also included in the master translation catalog.
988  
989 =item B<qgoda.inc>
990  
991 Contains additional Makefile snippets needed by the Qgoda translation workflow.
992 The file is only needed, when you use the F<Makefile>.
993  
994 =item B<*.po>
995  
996 For example F<fr.po>, F<de.po> and so on. You need one PO file for every
997 language you have configured. Failure to do so will result in an error.
998  
999 The simplest way of creating such a file from scratch is with the command:
1000  
1001 msginit --locale=fr --input=TEXTDOMAIN.pot
1002  
1003 Replace "fr" with the language you need and F<TEXTDOMAIN.pot> with the
1004 name of the master translation catalog that you have configured.
1005  
1006 Note that you have to run <qgoda po pot> at least once before you can
1007 create PO files.
1008  
1009 =item B<*.gmo>
1010  
1011 Contains compiled translations. These files are generated and you can safely
1012 delete them, whenever you want.
1013  
1014 =item B<.gitignore>
1015  
1016 Tells C<git(1)> which files to ignore. This file is auto-generated when
1017 missing but then left untouched.
1018  
1019 In order to reset the file to the latest upstream version, delete it and then
1020 run C<qgoda build> or C<qgoda watch> once. Alternatively, run C<qgoda po
1021 reset>.
1022  
1023 The master translation catalog F<TEXTDOMAIN.pot> is a generated file and
1024 generated files should normally be ignored by version control systems. No
1025 rule without an exception, master translation catalogs are conventionally not
1026 put into the ignore list. Rationale: Translators should not need any tools
1027 needed for creating F<.pot> and F<.po> files.
1028  
1029 =back
1030  
1031 =head1 TARGETS
1032  
1033 =over 4
1034  
1035 =item B<pot>
1036  
1037 Updates or creates the master translation catalog F<TEXTDOMAIN.pot>.
1038  
1039 =item B<update-po>
1040  
1041 Updates all F<.po> files by merging in the current strings found in
1042 F<TEXTDOMAIN.pot>. Note that you have to I<create> the F<.po> files
1043 yourself (try C<msginit --help>).
1044  
1045 This target implicitely includes the B<pot> target (see above).
1046  
1047 =item B<update-mo>
1048  
1049 Compiles the F<.po> files containing the translations. Additionally a
1050 syntactic check is performed on the translation files and statistics about
1051 translated, untranslated and fuzzy entries is printed out.
1052  
1053 This target implicitely includes the B<update-po> and all preceding targets.
1054  
1055 =item B<install>
1056  
1057 Installs translations. Running C<qgoda build> or C<qgoda watch> will now
1058 use these translations when rendering your site.
1059  
1060 This target implicitely includes the B<update-mo> and all preceding targets.
1061  
1062 =item B<all>
1063  
1064 Does all of the above. Use this target if you just want to ensure that
1065 everything is up-to-date and documents are translated to the extent that
1066 translations are available.
1067  
1068 =item B<reset>
1069  
1070 Overwrites the files F<po-make.pl> and F<Makefile>
1071 L<https://github.com/gflohr/Template-Plugin-Gettext-Seed/> and resets
1072 F<PACKAGE>, F<qgoda.yaml>, F<qgoda.inc>, and F<.gitignore> to a vanilla
1073 state.
1074  
1075 =back
1076  
1077 =head1 OPTIONS
1078  
1079 =over 4
1080  
1081 =item -h, --help
1082  
1083 Show this help page and exit.
1084  
1085 =back
1086  
1087 =head1 CONFIGURATION
1088  
1089 If you set the configuration variable C<po.make> to the name of a make(1)
1090 executable on your system, the Makefile in F<_po> is executed instead of
1091 emulating the Makefile behavior with Perl.
1092  
1093 The Makefile includes all files in the C<_po> directory that match C<*.inc>
1094 so that you can easily extend it.
1095  
1096 =head1 SEE ALSO
1097  
1098 L<http://www.qgoda.net/en/docs/i18n/>, L<xgettext-tt2>,
1099 L<Template::Plugin::Gettext>, xgettext(1), msgmerge(1), msgfmt(1), git(1)
1100  
1101 =head1 QGODA
1102  
1103 Part of L<Qgoda|http://www.qgoda.net/>.