summaryrefslogtreecommitdiff
path: root/t/po.t
blob: 1cd18c317c7b5a6b1220af3f53966933d70e119e (plain)
  1. #!/usr/bin/perl
  2. # -*- cperl-indent-level: 8; -*-
  3. use warnings;
  4. use strict;
  5. use File::Temp qw{tempdir};
  6. BEGIN {
  7. unless (eval { require Locale::Po4a::Chooser }) {
  8. eval q{
  9. use Test::More skip_all => "Locale::Po4a::Chooser::new is not available"
  10. }
  11. }
  12. unless (eval { require Locale::Po4a::Po }) {
  13. eval q{
  14. use Test::More skip_all => "Locale::Po4a::Po::new is not available"
  15. }
  16. }
  17. }
  18. use Test::More tests => 68;
  19. BEGIN { use_ok("IkiWiki"); }
  20. my $msgprefix;
  21. my $dir = tempdir("ikiwiki-test-po.XXXXXXXXXX",
  22. DIR => File::Spec->tmpdir,
  23. CLEANUP => 1);
  24. ### Init
  25. %config=IkiWiki::defaultconfig();
  26. $config{srcdir} = "$dir/src";
  27. $config{destdir} = "$dir/dst";
  28. $config{destdir} = "$dir/dst";
  29. $config{underlaydirbase} = "/dev/null";
  30. $config{underlaydir} = "/dev/null";
  31. $config{discussion} = 0;
  32. $config{po_master_language} = { code => 'en',
  33. name => 'English'
  34. };
  35. $config{po_slave_languages} = {
  36. es => 'Castellano',
  37. fr => "Français"
  38. };
  39. $config{po_translatable_pages}='index or test1 or test2 or translatable';
  40. $config{po_link_to}='negotiated';
  41. IkiWiki::loadplugins();
  42. ok(IkiWiki::loadplugin('po'), "po plugin loaded");
  43. IkiWiki::checkconfig();
  44. ### seed %pagesources and %pagecase
  45. $pagesources{'index'}='index.mdwn';
  46. $pagesources{'index.fr'}='index.fr.po';
  47. $pagesources{'index.es'}='index.es.po';
  48. $pagesources{'test1'}='test1.mdwn';
  49. $pagesources{'test1.fr'}='test1.fr.po';
  50. $pagesources{'test2'}='test2.mdwn';
  51. $pagesources{'test2.es'}='test2.es.po';
  52. $pagesources{'test2.fr'}='test2.fr.po';
  53. $pagesources{'test3'}='test3.mdwn';
  54. $pagesources{'test3.es'}='test3.es.mdwn';
  55. $pagesources{'translatable'}='translatable.mdwn';
  56. $pagesources{'translatable.fr'}='translatable.fr.po';
  57. $pagesources{'translatable.es'}='translatable.es.po';
  58. $pagesources{'nontranslatable'}='nontranslatable.mdwn';
  59. foreach my $page (keys %pagesources) {
  60. $IkiWiki::pagecase{lc $page}=$page;
  61. }
  62. ### populate srcdir
  63. writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
  64. writefile('test1.mdwn', $config{srcdir}, 'test1 content');
  65. writefile('test2.mdwn', $config{srcdir}, 'test2 content');
  66. writefile('test3.mdwn', $config{srcdir}, 'test3 content');
  67. writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
  68. writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
  69. ### istranslatable/istranslation
  70. # we run these tests twice because memoization attempts made them
  71. # succeed once every two tries...
  72. foreach (1, 2) {
  73. ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
  74. ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
  75. ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
  76. ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
  77. ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
  78. ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
  79. ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
  80. ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
  81. ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
  82. ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
  83. ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
  84. ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
  85. ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
  86. }
  87. ### pofiles
  88. my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
  89. ok( @pofiles, "pofiles is defined");
  90. ok( @pofiles == 2, "pofiles has correct size");
  91. is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
  92. ### links
  93. require IkiWiki::Render;
  94. sub refresh_n_scan(@) {
  95. my @masterfiles_rel=@_;
  96. foreach my $masterfile_rel (@masterfiles_rel) {
  97. my $masterfile=srcfile($masterfile_rel);
  98. IkiWiki::scan($masterfile_rel);
  99. next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
  100. my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
  101. IkiWiki::Plugin::po::refreshpot($masterfile);
  102. IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
  103. map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
  104. }
  105. }
  106. $config{po_link_to}='negotiated';
  107. $msgprefix="links (po_link_to=negotiated)";
  108. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  109. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  110. is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
  111. is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
  112. is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
  113. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  114. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  115. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  116. $config{po_link_to}='current';
  117. $msgprefix="links (po_link_to=current)";
  118. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  119. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  120. is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable'))], "$msgprefix index.es");
  121. is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable'))], "$msgprefix index.fr");
  122. is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
  123. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  124. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  125. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  126. ### targetpage
  127. $config{usedirs}=0;
  128. $msgprefix="targetpage (usedirs=0)";
  129. is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
  130. is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
  131. $config{usedirs}=1;
  132. $msgprefix="targetpage (usedirs=1)";
  133. is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
  134. is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
  135. is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
  136. is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
  137. is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
  138. is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
  139. ### urlto -> index
  140. $config{po_link_to}='current';
  141. $msgprefix="urlto (po_link_to=current)";
  142. is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
  143. is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
  144. is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
  145. $config{po_link_to}='negotiated';
  146. $msgprefix="urlto (po_link_to=negotiated)";
  147. is(urlto('', 'index'), './', "$msgprefix index -> ''");
  148. is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
  149. is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
  150. ### bestlink
  151. $config{po_link_to}='current';
  152. $msgprefix="bestlink (po_link_to=current)";
  153. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  154. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  155. $config{po_link_to}='negotiated';
  156. $msgprefix="bestlink (po_link_to=negotiated)";
  157. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  158. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  159. ### beautify_urlpath
  160. $config{po_link_to}='default';
  161. $msgprefix="beautify_urlpath (po_link_to=default)";
  162. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
  163. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
  164. $config{po_link_to}='negotiated';
  165. $msgprefix="beautify_urlpath (po_link_to=negotiated)";
  166. is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
  167. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
  168. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");