X-Git-Url: http://git.vanrenterghem.biz/git.ikiwiki.info.git/blobdiff_plain/230b7d4d6e1a87bc5e18eb6c6155cc45957cff70..346bac30f090a0d24c887ebc57dd4251b889dc27:/IkiWiki/Plugin/po.pm

diff --git a/IkiWiki/Plugin/po.pm b/IkiWiki/Plugin/po.pm
index 4f8d5036e..6b55ee351 100644
--- a/IkiWiki/Plugin/po.pm
+++ b/IkiWiki/Plugin/po.pm
@@ -23,7 +23,6 @@ use File::Copy;
 use File::Spec;
 use File::Temp;
 use Memoize;
-use UNIVERSAL;
 
 my ($master_language_code, $master_language_name);
 my %translations;
@@ -31,6 +30,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");
@@ -47,7 +47,7 @@ sub import {
 	hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
 	hook(type => "rename", id => "po", call => \&renamepages, first => 1);
 	hook(type => "delete", id => "po", call => \&mydelete);
-	hook(type => "change", id => "po", call => \&change);
+	hook(type => "rendered", id => "po", call => \&rendered);
 	hook(type => "checkcontent", id => "po", call => \&checkcontent);
 	hook(type => "canremove", id => "po", call => \&canremove);
 	hook(type => "canrename", id => "po", call => \&canrename);
@@ -66,8 +66,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);
 	}
@@ -339,6 +342,19 @@ sub pagetemplate (@) {
 	if ($template->query(name => "istranslatable")) {
 		$template->param(istranslatable => istranslatable($page));
 	}
+	my $lang_code = istranslation($page) ? lang($page) : $master_language_code;
+	if ($template->query(name => "lang_code")) {
+		$template->param(lang_code => $lang_code);
+	}
+	if ($template->query(name => "html_lang_code")) {
+		$template->param(html_lang_code => htmllangcode($lang_code));
+	}
+	if ($template->query(name => "html_lang_dir")) {
+		$template->param(html_lang_dir => htmllangdir($lang_code));
+	}
+	if ($template->query(name => "lang_name")) {
+		$template->param(lang_name => languagename($lang_code));
+	}
 	if ($template->query(name => "HOMEPAGEURL")) {
 		$template->param(homepageurl => homepageurl($page));
 	}
@@ -417,7 +433,7 @@ sub mydelete (@) {
 	map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
 }
 
-sub change (@) {
+sub rendered (@) {
 	my @rendered=@_;
 
 	my $updated_po_files=0;
@@ -621,20 +637,24 @@ 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 ($;$$) {
@@ -804,7 +824,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}
@@ -843,10 +863,23 @@ sub lang ($) {
 	return $master_language_code;
 }
 
+sub htmllangcode ($) {
+	(my $lang = shift) =~ tr/_/-/;
+	return $lang;
+}
+
+sub htmllangdir ($) {
+	my $lang = shift;
+	if ($lang =~ /^(ar|fa|he)/) {
+		return 'rtl';
+	}
+	return 'ltr';
+}
+
 sub islanguagecode ($) {
 	my $code=shift;
 
-	return $code =~ /^[a-z]{2}$/;
+	return $code =~ /^$language_code_pattern$/;
 }
 
 sub otherlanguage_page ($$) {
@@ -960,10 +993,9 @@ sub refreshpofiles ($@) {
 		}
 
 		if (-e $pofile) {
-			system("msgmerge", "--previous", "-q", "-U", "--backup=none", $pofile, $potfile) == 0
-				or error("po(refreshpofiles) ".
-					 sprintf(gettext("failed to update %s"),
-						 $pofile));
+			if (! (system("msgmerge", "--previous", "-q", "-U", "--backup=none", $pofile, $potfile) == 0)) {
+				print STDERR ("po(refreshpofiles) ". sprintf(gettext("failed to update %s"), $pofile));
+			}
 		}
 		else {
 			File::Copy::syscopy($potfile,$pofile)
@@ -1039,6 +1071,8 @@ sub otherlanguagesloop ($) {
 		push @ret, {
 			url => urlto_with_orig_beautiful_urlpath(masterpage($page), $page),
 			code => $master_language_code,
+			html_code => htmllangcode($master_language_code),
+			html_dir => htmllangdir($master_language_code),
 			language => $master_language_name,
 			master => 1,
 		};
@@ -1049,6 +1083,8 @@ sub otherlanguagesloop ($) {
 		push @ret, {
 			url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
 			code => $lang,
+			html_code => htmllangcode($lang),
+			html_dir => htmllangdir($lang),
 			language => languagename($lang),
 			percent => percenttranslated($otherpage),
 		}
@@ -1088,7 +1124,7 @@ sub deletetranslations ($) {
 			IkiWiki::rcs_remove($_);
 		}
 		else {
-			IkiWiki::prune("$config{srcdir}/$_");
+			IkiWiki::prune("$config{srcdir}/$_", $config{srcdir});
 		}
 	} @todelete;
 
@@ -1238,6 +1274,7 @@ sub po4a_options($) {
 		# how to disable options is not consistent across po4a modules
 		$options{includessi} = '';
 		$options{includeexternal} = 0;
+		$options{ontagerror} = 'warn';
 	}
 	elsif ($pagetype eq 'mdwn') {
 		$options{markdown} = 1;
@@ -1252,7 +1289,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