+sub islanguagecode ($) { #{{{
+ my $code=shift;
+ return ($code =~ /^[a-z]{2}$/);
+} #}}}
+
+sub checkconfig () { #{{{
+ foreach my $field (qw{po_master_language po_slave_languages}) {
+ if (! exists $config{$field} || ! defined $config{$field}) {
+ error(sprintf(gettext("Must specify %s"), $field));
+ }
+ }
+ if (! (keys %{$config{po_slave_languages}})) {
+ error(gettext("At least one slave language must be defined in po_slave_languages"));
+ }
+ map {
+ islanguagecode($_)
+ or error(sprintf(gettext("%s is not a valid language code"), $_));
+ } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
+ if (! exists $config{po_translatable_pages} ||
+ ! defined $config{po_translatable_pages}) {
+ $config{po_translatable_pages}="";
+ }
+ if (! exists $config{po_link_to} ||
+ ! defined $config{po_link_to}) {
+ $config{po_link_to}='default';
+ }
+ elsif (! grep {
+ $config{po_link_to} eq $_
+ } ('default', 'current', 'negotiated')) {
+ warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
+ $config{po_link_to}));
+ $config{po_link_to}='default';
+ }
+ elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
+ warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
+ $config{po_link_to}='default';
+ }
+ push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
+} #}}}
+
+sub potfile ($) { #{{{
+ my $masterfile=shift;
+
+ (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
+ $dir='' if $dir eq './';
+ return File::Spec->catpath('', $dir, $name . ".pot");
+} #}}}
+
+sub pofile ($$) { #{{{
+ my $masterfile=shift;
+ my $lang=shift;
+
+ (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
+ $dir='' if $dir eq './';
+ return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
+} #}}}
+
+sub refreshpot ($) { #{{{
+ my $masterfile=shift;
+
+ my $potfile=potfile($masterfile);
+ my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
+ my $doc=Locale::Po4a::Chooser::new('text',%options);
+ $doc->read($masterfile);
+ $doc->{TT}{utf_mode} = 1;
+ $doc->{TT}{file_in_charset} = 'utf-8';
+ $doc->{TT}{file_out_charset} = 'utf-8';
+ # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
+ # this is undocument use of internal Locale::Po4a::TransTractor's data,
+ # compulsory since this module prevents us from using the porefs option.
+ my %po_options = ('porefs' => 'none');
+ $doc->{TT}{po_out}=Locale::Po4a::Po->new(\%po_options);
+ $doc->{TT}{po_out}->set_charset('utf-8');
+ # do the actual work
+ $doc->parse;
+ IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
+ $doc->writepo($potfile);
+} #}}}
+
+sub refreshpofiles ($@) { #{{{
+ my $masterfile=shift;
+ my @pofiles=@_;
+
+ my $potfile=potfile($masterfile);
+ error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
+
+ foreach my $pofile (@pofiles) {
+ IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
+ if (-e $pofile) {
+ system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
+ or error("[po/refreshpofiles:$pofile] failed to update");
+ }
+ else {
+ File::Copy::syscopy($potfile,$pofile)
+ or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
+ }
+ }
+} #}}}
+
+sub needsbuild () { #{{{
+ my $needsbuild=shift;
+
+ # backup @needsbuild content so that change() can know whether
+ # a given master page was rendered because its source file was changed
+ @origneedsbuild=(@$needsbuild);
+
+ # build %translations, using istranslation's side-effect
+ map istranslation($_), (keys %pagesources);
+
+ # make existing translations depend on the corresponding master page
+ foreach my $master (keys %translations) {
+ foreach my $slave (values %{$translations{$master}}) {
+ add_depends($slave, $master);
+ }
+ }
+} #}}}