]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/po.pm
po plugin: created OTHERLANGUAGES template loop
[git.ikiwiki.info.git] / IkiWiki / Plugin / po.pm
index 0698b248838451e8788afd2a21a83ddbe0846fd5..88985cd1208fb77a75657f91f35b3ae31345a9a1 100644 (file)
@@ -10,6 +10,11 @@ use IkiWiki 2.00;
 use Encode;
 use Locale::Po4a::Chooser;
 use File::Temp;
+use Memoize;
+
+my %translations;
+memoize("istranslatable");
+memoize("_istranslation");
 
 sub import {
        hook(type => "getsetup", id => "po", call => \&getsetup);
@@ -20,6 +25,7 @@ sub import {
        hook(type => "tweakbestlink", id => "po", call => \&tweakbestlink);
        hook(type => "filter", id => "po", call => \&filter);
        hook(type => "htmlize", id => "po", call => \&htmlize);
+       hook(type => "pagetemplate", id => "po", call => \&pagetemplate);
 }
 
 sub getsetup () { #{{{
@@ -40,9 +46,10 @@ sub getsetup () { #{{{
                },
                po_slave_languages => {
                        type => "string",
-                       example => {'fr' => { 'name' => 'Français' },
-                                   'es' => { 'name' => 'Castellano' },
-                                   'de' => { 'name' => 'Deutsch' },
+                       example => {
+                               'fr' => 'Français',
+                               'es' => 'Castellano',
+                               'de' => 'Deutsch'
                        },
                        description => "slave languages (PO files)",
                        safe => 1,
@@ -88,9 +95,10 @@ sub checkconfig () { #{{{
 sub scan (@) { #{{{
        my %params=@_;
        my $page=$params{page};
-
-       # FIXME: cache (or memoize) the list of translatable/translation pages,
-       # and/or istranslation/istranslated results
+       # let's build %translations, using istranslation's
+       # side-effect, so that we can consider it is complete at
+       # preprocess time
+       istranslation($page);
 } #}}}
 
 sub targetpage (@) { #{{{
@@ -191,6 +199,49 @@ sub htmlize (@) { #{{{
        return IkiWiki::htmlize($page, $page, pagetype($masterfile), $content);
 } #}}}
 
+sub otherlanguages ($) { #{{{
+       my $page=shift;
+       my @ret;
+       if (istranslatable($page)) {
+               foreach my $lang (sort keys %{$translations{$page}}) {
+                       push @ret, {
+                               url => urlto($translations{$page}{$lang}, $page),
+                               code => $lang,
+                               language => $config{po_slave_languages}{$lang},
+                               master => 0,
+                       };
+               }
+       }
+       elsif (istranslation($page)) {
+               my ($masterpage, $curlang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+               push @ret, {
+                       url => urlto($masterpage, $page),
+                       code => $config{po_master_language}{code},
+                       language => $config{po_master_language}{name},
+                       master => 1,
+               };
+               foreach my $lang (sort keys %{$translations{$masterpage}}) {
+                       push @ret, {
+                               url => urlto($translations{$masterpage}{$lang}, $page),
+                               code => $lang,
+                               language => $config{po_slave_languages}{$lang},
+                               master => 0,
+                       } unless ($lang eq $curlang);
+               }
+       }
+       return @ret;
+} #}}}
+
+sub pagetemplate (@) { #{{{
+       my %params=@_;
+        my $page=$params{page};
+        my $template=$params{template};
+
+       if ($template->query(name => "otherlanguages")) {
+               $template->param(otherlanguages => [otherlanguages($page)]);
+       }
+} # }}}
+
 sub istranslatable ($) { #{{{
        my $page=shift;
        my $file=$pagesources{$page};
@@ -203,7 +254,7 @@ sub istranslatable ($) { #{{{
        return pagespec_match($page, $config{po_translatable_pages});
 } #}}}
 
-sub istranslation ($) { #{{{
+sub _istranslation ($) { #{{{
        my $page=shift;
        my $file=$pagesources{$page};
        if (! defined $file) {
@@ -228,6 +279,15 @@ sub istranslation ($) { #{{{
        return istranslatable($masterpage);
 } #}}}
 
+sub istranslation ($) { #{{{
+       my $page=shift;
+       if (_istranslation($page)) {
+               my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+               $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
+               return 1;
+       }
+       return 0;
+} #}}}
 
 package IkiWiki::PageSpec;
 use warnings;