PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/website/update_translation.pl

#
Perl | 280 lines | 165 code | 76 blank | 39 comment | 32 complexity | d94c638ea68f55901be7f688f42fcd45 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. #!/usr/bin/perl
  2. use lib "Perl";
  3. use Digest::MD5;
  4. use File::Path;
  5. use File::Copy;
  6. use File::Compare;
  7. use Locale::PO;
  8. use strict;
  9. # This script updates the translation in the folder given as parameter
  10. my $TARGET="website-generated";
  11. sub filesAreDifferent {
  12. my $file1 = shift;
  13. my $file2 = shift;
  14. return compare($file1, $file2);
  15. }
  16. sub get_langs {
  17. my $file = shift || "config/language-online.conf";
  18. open(IN, "< $file");
  19. my $line = <IN>;
  20. my @langs = split(" ", $line);
  21. close(IN);
  22. return @langs;
  23. }
  24. # 1st parameter is the dir with the english XML sources
  25. # 2nd parameter is the dir where the POT files should be put
  26. sub update_pot_files() {
  27. my $source = shift;
  28. my $dest = shift;
  29. print "Making POT files\n";
  30. mkpath($dest);
  31. unlink("$dest/full.pot");
  32. opendir(DIR, $source);
  33. while (my $file = readdir(DIR)) {
  34. next unless ($file =~ /\.xml$/);
  35. #next unless ($file =~ /(\.xml|\.docbook)$/);
  36. next if ($file eq "catalog.xml");
  37. my $potfile = $dest . $file;
  38. $potfile =~ s/(\.xml|\.docbook)$/\.pot/;
  39. my $command = "xml2pot $source/$file > $potfile";
  40. `$command`;
  41. #print "\tCreating temporary POT $potfile\n";
  42. }
  43. print "Merging POT files.\n";
  44. `msgcat --force-po -o $dest/full.pot.cat $dest/*.pot 2>&1 > /dev/null`;
  45. `rm $dest/*.pot 2>&1 > /dev/null`;
  46. move("$dest/full.pot.cat","$dest/full.pot");
  47. closedir(DIR);
  48. }
  49. # 1st parameter is the dir where the POT files are
  50. # 2nd parameter is the dir where the PO files should be put
  51. sub update_po_files() {
  52. my $potdir = shift;
  53. my $podir = shift;
  54. print "$podir: Creating PO files\n";
  55. mkpath($podir);
  56. my $potfile = "$potdir/full.pot";
  57. my $pofile = "$podir/full.po";
  58. if (!-f $pofile) {
  59. `touch $pofile`;
  60. }
  61. #print "msgmerge --force-po --no-wrap -o $pofile.new $pofile $potfile\n";
  62. `msgmerge --force-po --no-wrap -o $pofile.new $pofile $potfile `;
  63. if (compare($pofile, "$pofile.new") != 0) { #different
  64. move("$pofile.new", $pofile);
  65. #print "\t\tMerged in changes\n";
  66. }
  67. else {
  68. #print "\t\tSame entries!\n";
  69. }
  70. unlink("$pofile.new");
  71. #print "\n\n";
  72. }
  73. # 1st parameter is the dir with the english sources
  74. # 2nd parameter is the dir where the PO files are
  75. # 3rd parameter is the dir where the XML files should be put
  76. sub make_xml_files() {
  77. my $source = shift;
  78. my $podir = shift;
  79. my $dest = shift;
  80. my $pofile = "$podir/full.po";
  81. print "Creating XML files\n";
  82. # Create a PO file which contains the original english strings as msgstr of empty/untranslated entries
  83. # This way the files won"t be empty if the translation is misisng
  84. copy("$pofile","$pofile-complete");
  85. my $aref = Locale::PO->load_file_asarray("$pofile");
  86. my @entries = @$aref if ($aref);
  87. if ($#entries+1) {
  88. foreach my $entry (@entries) {
  89. my $msgid = $entry->dequote( $entry->msgid() );
  90. my $msgstr = $entry->dequote( $entry->msgstr() );
  91. my $no_msgstr = !$msgstr || ($msgstr eq "") || (length($msgstr) == 0);
  92. $entry->msgstr($msgid) if ($msgid && ($no_msgstr || $entry->fuzzy()));
  93. }
  94. }
  95. die unless Locale::PO->save_file_fromarray("$pofile-complete", \@entries);
  96. opendir(DIR, $source);
  97. while (my $file = readdir(DIR)) {
  98. next unless ($file =~ /(\.xml|\.docbook)$/);
  99. next if ($file eq "catalog.xml");
  100. my $age_en = -M "$source/$file";
  101. my $age_translated = -M "$dest/$file";
  102. my $age_po = -M "$pofile-complete";
  103. #print "no recreateion of $file\n" if (!$alwaysRecreate{"$file"});
  104. next if (-f "$source/$file" && -f "$dest/$file" && ($age_en > $age_translated) && ($age_po > $age_translated)); #english file is older than the translated file so no update needed
  105. #print "\tChecking if we need to create $dest/$file\n";
  106. print "\tCreating file $dest/$file\n";
  107. `po2xml $source/$file $pofile-complete > $dest/$file.test`;
  108. #if the created XML file is different from the original XML file the PO file had some changes
  109. #if (($file eq "biblehowto.docbook") || ( ($age_po <= $age_translated) && (filesAreDifferent("$dest/$file", "$dest/$file.test"))) ) {
  110. # print "\t\tRecreating $file\n";
  111. move("$dest/$file.test", "$dest/$file");
  112. #}
  113. #else { #the files are the same, remove the test file
  114. # unlink("$dest/$file.test");
  115. #}
  116. }
  117. # Delete temporary PO file used for XML file creation
  118. unlink("$pofile-complete");
  119. print "\n\n";
  120. closedir(DIR);
  121. }
  122. sub create_apache_files() {
  123. my $source = shift;
  124. my $dest = shift;
  125. my @langs = &get_langs("config/language-online.conf");
  126. print "Creating apache files ...\n";
  127. #Create the robots.txt file
  128. open(OUT, "> $dest/robots.txt");
  129. print OUT "# Robots file for www.bibletime.info. Created by update_translation.pl\n";
  130. print OUT "User-agent *\n";
  131. foreach my $lang (sort @langs) {
  132. print OUT "Allow: /$lang/\n";
  133. }
  134. close(OUT);
  135. #Create the .var files
  136. opendir(DIR, "$source");
  137. while (my $file = readdir(DIR)) {
  138. next unless ($file =~ /\.html|\.shtml|\.phtml|\.php4|\.php$/);
  139. #print "$file\n";
  140. my $htmlfile = $file;
  141. $file =~ s/\.html|\.shtml|\.phtml|.php4|.php$/.var/;
  142. open(OUT, "> $dest/$file");
  143. print OUT "URI: en/$htmlfile\n";
  144. print OUT "Content-type: text/html\n";
  145. print OUT "Content-language: en\n";
  146. foreach my $lang (@langs) {
  147. #print "$lang\n";
  148. print OUT "\nURI: $lang/$htmlfile\n";
  149. print OUT "Content-type: text/html\n";
  150. print OUT "Content-language: $lang\n";
  151. }
  152. print OUT "\nURI: en/$htmlfile\n";
  153. print OUT "Content-type: text/html\n";
  154. close(OUT);
  155. }
  156. closedir(DIR);
  157. }
  158. #Creates the HTML files for the files in the dir given by the first parameter
  159. sub create_html {
  160. my $dir = shift || die;
  161. my $dirOut = shift || die;
  162. my $lang = shift || die;
  163. print "Creating html files for $dir in $dirOut\n";
  164. unlink "$dir/autolayout.xml";
  165. my $command_layout = "XML_CATALOG_FILES='docbook-xsl/catalog.xml docbook-xsl/website/catalog.xml' xsltproc --output $dir/autolayout.xml docbook-xsl/website/xsl/autolayout.xsl --stringparam \"page-language\" \"$lang\" $dir/layout.xml";
  166. my $command_create = "XML_CATALOG_FILES='docbook-xsl/catalog.xml docbook-xsl/website/catalog.xml' xsltproc --nonet --path $dir --stringparam output-root $dirOut --stringparam page-language $lang bibletime-xsl/bibletime.xsl $dir/autolayout.xml";
  167. print "$command_layout\n";
  168. `$command_layout`;
  169. print "$command_create\n";
  170. `$command_create`;
  171. }
  172. # Either the parameters or the languages we know
  173. my @langs;
  174. while (my $lang = pop(@ARGV)) {
  175. push(@langs, $lang);
  176. }
  177. if (!@langs) {
  178. @langs = sort &get_langs();
  179. #("bg", "cs", "de", "fr", "nl", "ko", "pt-br", "ro", "ru", "ua");
  180. }
  181. #required for all languages
  182. &update_pot_files($ENV{"PWD"} . "/content/en", $ENV{"PWD"} . "/content/en/pot/");
  183. foreach my $lang (&get_langs("config/language.conf")) {
  184. print "Creating PO files for $lang...\n";
  185. &update_po_files($ENV{"PWD"} . "/content/en/pot/", $ENV{"PWD"} . "/content/$lang/po/");
  186. };
  187. while (my $lang = shift(@langs)) {
  188. if ($lang eq "en") {
  189. next;
  190. }
  191. print "Working on $lang ...\n";
  192. &make_xml_files($ENV{"PWD"}. "/content/en", $ENV{"PWD"} . "/content/$lang/po/", $ENV{"PWD"} . "/content/$lang/");
  193. &create_html("./content/$lang", "./$TARGET/$lang", "$lang");
  194. }
  195. #copy all common files
  196. `cp -raf website-common/* website-common/.htaccess $TARGET`;
  197. #Copy all language po file
  198. `test -d website-generated/po || mkdir website-generated/po;`;
  199. `for d in \$(cat config/language.conf); do echo "\$d"; cp -Rf content/\$d/po/full.po website-generated/po/bibletime_website_\$d.po; done;`;
  200. `cp content/en/pot/full.pot website-generated/po/bibletime_website_template.pot`;
  201. &create_apache_files($ENV{"PWD"} . "/$TARGET/en", "$TARGET");
  202. #Now update the statistics. At the end so we get all changed to the PO files.
  203. #`(cd $TARGET/postats && perl make_postats.pl > /dev/null 2>&1; cd ..)`;
  204. `(./update_postats.sh > /dev/null 2>&1; cd ..)`;