]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/po.pm
po: support language codes in the form of 'es_AR', and 'arn'.
[ikiwiki.git] / IkiWiki / Plugin / po.pm
index 79142ed1fe62396f17cff88cbfe914acb40896f7..d3a996a2189fd24c007ea4127ed0a56faf9d7c8b 100644 (file)
@@ -31,6 +31,7 @@ my @origneedsbuild;
 my %origsubs;
 my @slavelanguages; # language codes ordered as in config po_slave_languages
 my %slavelanguages; # language code to name lookup
+my $language_code_pattern = '[a-zA-Z]+(?:_[a-zA-Z]+)?';
 
 memoize("istranslatable");
 memoize("_istranslation");
@@ -38,7 +39,8 @@ memoize("percenttranslated");
 
 sub import {
        hook(type => "getsetup", id => "po", call => \&getsetup);
-       hook(type => "checkconfig", id => "po", call => \&checkconfig);
+       hook(type => "checkconfig", id => "po", call => \&checkconfig,
+               last => 1);
        hook(type => "needsbuild", id => "po", call => \&needsbuild);
        hook(type => "scan", id => "po", call => \&scan, last => 1);
        hook(type => "filter", id => "po", call => \&filter);
@@ -65,8 +67,11 @@ sub import {
                inject(name => "IkiWiki::urlto", call => \&myurlto);
                $origsubs{'cgiurl'}=\&IkiWiki::cgiurl;
                inject(name => "IkiWiki::cgiurl", call => \&mycgiurl);
-               $origsubs{'rootpage'}=\&IkiWiki::rootpage;
-               inject(name => "IkiWiki::rootpage", call => \&myrootpage);
+               if (IkiWiki->can('rootpage')) {
+                       $origsubs{'rootpage'}=\&IkiWiki::rootpage;
+                       inject(name => "IkiWiki::rootpage", call => \&myrootpage)
+                               if defined $origsubs{'rootpage'};
+               }
                $origsubs{'isselflink'}=\&IkiWiki::isselflink;
                inject(name => "IkiWiki::isselflink", call => \&myisselflink);
        }
@@ -367,7 +372,8 @@ sub pagetemplate (@) {
            && $masterpage eq "index") {
                $template->param('parentlinks' => []);
        }
-       if (ishomepage($page) && $template->query(name => "title")) {
+       if (ishomepage($page) && $template->query(name => "title")
+           && !$template->param("title_overridden")) {
                $template->param(title => $config{wikiname});
        }
 }
@@ -619,23 +625,27 @@ sub mybeautify_urlpath ($) {
        return $res;
 }
 
-sub mytargetpage ($$) {
+sub mytargetpage ($$;$) {
        my $page=shift;
        my $ext=shift;
+       my $filename=shift;
 
        if (istranslation($page) || istranslatable($page)) {
                my ($masterpage, $lang) = (masterpage($page), lang($page));
-               if (! $config{usedirs} || $masterpage eq 'index') {
+               if (defined $filename) {
+                       return $masterpage . "/" . $filename . "." . $lang . "." . $ext;
+               }
+               elsif (! $config{usedirs} || $masterpage eq 'index') {
                        return $masterpage . "." . $lang . "." . $ext;
                }
                else {
                        return $masterpage . "/index." . $lang . "." . $ext;
                }
        }
-       return $origsubs{'targetpage'}->($page, $ext);
+       return $origsubs{'targetpage'}->($page, $ext, $filename);
 }
 
-sub myurlto ($$;$) {
+sub myurlto ($;$$) {
        my $to=shift;
        my $from=shift;
        my $absolute=shift;
@@ -644,7 +654,12 @@ sub myurlto ($$;$) {
        if (! length $to
            && $config{po_link_to} eq "current"
            && istranslatable('index')) {
-               return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
+               if (defined $from) {
+                       return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
+               }
+               else {
+                       return $origsubs{'urlto'}->($to,$from,$absolute);
+               }
        }
        # avoid using our injected beautify_urlpath if run by cgi_editpage,
        # so that one is redirected to the just-edited page rather than to the
@@ -797,7 +812,7 @@ sub _istranslation ($) {
                         && pagetype($file) eq 'po';
        return 0 if $file =~ /\.pot$/;
 
-       my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
+       my ($masterpage, $lang) = ($page =~ /(.*)[.]($language_code_pattern)$/);
        return 0 unless defined $masterpage && defined $lang
                         && length $masterpage && length $lang
                         && defined $pagesources{$masterpage}
@@ -839,7 +854,7 @@ sub lang ($) {
 sub islanguagecode ($) {
        my $code=shift;
 
-       return $code =~ /^[a-z]{2}$/;
+       return $code =~ /^$language_code_pattern$/;
 }
 
 sub otherlanguage_page ($$) {
@@ -1245,7 +1260,7 @@ sub po4a_options($) {
 sub splitlangpair ($) {
        my $pair=shift;
 
-       my ($code, $name) = ( $pair =~ /^([a-z]{2})\|(.+)$/ );
+       my ($code, $name) = ( $pair =~ /^($language_code_pattern)\|(.+)$/ );
        if (! defined $code || ! defined $name ||
            ! length $code || ! length $name) {
                # not a fatal error to avoid breaking if used with web setup