summaryrefslogtreecommitdiff
path: root/t/po.t
blob: 827e7c7a5ef39aa4a619676205c44791620e6e02 (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 => 65;
  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{discussion} = 0;
  29. $config{po_master_language} = { code => 'en',
  30. name => 'English'
  31. };
  32. $config{po_slave_languages} = {
  33. es => 'Castellano',
  34. fr => "Français"
  35. };
  36. $config{po_translatable_pages}='index or test1 or test2 or translatable';
  37. $config{po_link_to}='negotiated';
  38. IkiWiki::loadplugins();
  39. IkiWiki::checkconfig();
  40. ok(IkiWiki::loadplugin('po'), "po plugin loaded");
  41. ### seed %pagesources and %pagecase
  42. $pagesources{'index'}='index.mdwn';
  43. $pagesources{'index.fr'}='index.fr.po';
  44. $pagesources{'index.es'}='index.es.po';
  45. $pagesources{'test1'}='test1.mdwn';
  46. $pagesources{'test1.fr'}='test1.fr.po';
  47. $pagesources{'test2'}='test2.mdwn';
  48. $pagesources{'test2.es'}='test2.es.po';
  49. $pagesources{'test2.fr'}='test2.fr.po';
  50. $pagesources{'test3'}='test3.mdwn';
  51. $pagesources{'test3.es'}='test3.es.mdwn';
  52. $pagesources{'translatable'}='translatable.mdwn';
  53. $pagesources{'translatable.fr'}='translatable.fr.po';
  54. $pagesources{'translatable.es'}='translatable.es.po';
  55. $pagesources{'nontranslatable'}='nontranslatable.mdwn';
  56. foreach my $page (keys %pagesources) {
  57. $IkiWiki::pagecase{lc $page}=$page;
  58. }
  59. ### istranslatable/istranslation
  60. # we run these tests twice because memoization attempts made them
  61. # succeed once every two tries...
  62. foreach (1, 2) {
  63. ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
  64. ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
  65. ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
  66. ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
  67. ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
  68. ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
  69. ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
  70. ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
  71. ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
  72. ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
  73. ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
  74. ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
  75. ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
  76. }
  77. ### links
  78. require IkiWiki::Render;
  79. sub refresh_n_scan(@) {
  80. my @masterfiles_rel=@_;
  81. foreach my $masterfile_rel (@masterfiles_rel) {
  82. my $masterfile=srcfile($masterfile_rel);
  83. IkiWiki::scan($masterfile_rel);
  84. next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
  85. my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
  86. IkiWiki::Plugin::po::refreshpot($masterfile);
  87. IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
  88. map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
  89. }
  90. }
  91. writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
  92. writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
  93. writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
  94. $config{po_link_to}='negotiated';
  95. $msgprefix="links (po_link_to=negotiated)";
  96. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  97. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  98. is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
  99. is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
  100. is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
  101. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  102. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  103. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  104. $config{po_link_to}='current';
  105. $msgprefix="links (po_link_to=current)";
  106. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  107. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  108. is_deeply(\@{$links{'index.es'}}, [ map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')], "$msgprefix index.es");
  109. is_deeply(\@{$links{'index.fr'}}, [ map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')], "$msgprefix index.fr");
  110. is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
  111. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  112. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  113. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  114. ### targetpage
  115. $config{usedirs}=0;
  116. $msgprefix="targetpage (usedirs=0)";
  117. is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
  118. is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
  119. $config{usedirs}=1;
  120. $msgprefix="targetpage (usedirs=1)";
  121. is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
  122. is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
  123. is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
  124. is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
  125. is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
  126. is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
  127. ### urlto -> index
  128. $config{po_link_to}='current';
  129. $msgprefix="urlto (po_link_to=current)";
  130. is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
  131. is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
  132. is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
  133. $config{po_link_to}='negotiated';
  134. $msgprefix="urlto (po_link_to=negotiated)";
  135. is(urlto('', 'index'), './', "$msgprefix index -> ''");
  136. is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
  137. is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
  138. ### bestlink
  139. $config{po_link_to}='current';
  140. $msgprefix="bestlink (po_link_to=current)";
  141. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  142. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  143. $config{po_link_to}='negotiated';
  144. $msgprefix="bestlink (po_link_to=negotiated)";
  145. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  146. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  147. ### beautify_urlpath
  148. $config{po_link_to}='default';
  149. $msgprefix="beautify_urlpath (po_link_to=default)";
  150. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
  151. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
  152. $config{po_link_to}='negotiated';
  153. $msgprefix="beautify_urlpath (po_link_to=negotiated)";
  154. is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
  155. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
  156. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");