summaryrefslogtreecommitdiff
path: root/t/po.t
blob: da0bd68a7d89e6177ddc811e503444d75c59c264 (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 => 109;
  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{url} = "http://example.com";
  32. $config{cgiurl} = "http://example.com/ikiwiki.cgi";
  33. $config{discussion} = 0;
  34. $config{po_master_language} = { code => 'en',
  35. name => 'English'
  36. };
  37. $config{po_slave_languages} = {
  38. es => 'Castellano',
  39. fr => "Français"
  40. };
  41. $config{po_translatable_pages}='index or test1 or test2 or translatable';
  42. $config{po_link_to}='negotiated';
  43. IkiWiki::loadplugins();
  44. ok(IkiWiki::loadplugin('meta'), "meta plugin loaded");
  45. ok(IkiWiki::loadplugin('po'), "po plugin loaded");
  46. IkiWiki::checkconfig();
  47. ### seed %pagesources and %pagecase
  48. $pagesources{'index'}='index.mdwn';
  49. $pagesources{'index.fr'}='index.fr.po';
  50. $pagesources{'index.es'}='index.es.po';
  51. $pagesources{'test1'}='test1.mdwn';
  52. $pagesources{'test1.es'}='test1.es.po';
  53. $pagesources{'test1.fr'}='test1.fr.po';
  54. $pagesources{'test2'}='test2.mdwn';
  55. $pagesources{'test2.es'}='test2.es.po';
  56. $pagesources{'test2.fr'}='test2.fr.po';
  57. $pagesources{'test3'}='test3.mdwn';
  58. $pagesources{'test3.es'}='test3.es.mdwn';
  59. $pagesources{'translatable'}='translatable.mdwn';
  60. $pagesources{'translatable.fr'}='translatable.fr.po';
  61. $pagesources{'translatable.es'}='translatable.es.po';
  62. $pagesources{'nontranslatable'}='nontranslatable.mdwn';
  63. foreach my $page (keys %pagesources) {
  64. $IkiWiki::pagecase{lc $page}=$page;
  65. }
  66. ### populate srcdir
  67. writefile('index.mdwn', $config{srcdir},
  68. "[[!meta title=\"index title\"]]\n[[translatable]] [[nontranslatable]]");
  69. writefile('test1.mdwn', $config{srcdir},
  70. "[[!meta title=\"test1 title\"]]\ntest1 content");
  71. writefile('test2.mdwn', $config{srcdir}, 'test2 content');
  72. writefile('test3.mdwn', $config{srcdir}, 'test3 content');
  73. writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
  74. writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
  75. ### istranslatable/istranslation
  76. # we run these tests twice because memoization attempts made them
  77. # succeed once every two tries...
  78. foreach (1, 2) {
  79. ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
  80. ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
  81. ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
  82. ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
  83. ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
  84. ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
  85. ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
  86. ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
  87. ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
  88. ok(IkiWiki::Plugin::po::istranslatable('test1'), "test1 is translatable");
  89. ok(IkiWiki::Plugin::po::istranslation('test1.es'), "test1.es is a translation");
  90. ok(IkiWiki::Plugin::po::istranslation('test1.fr'), "test1.fr is a translation");
  91. ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
  92. ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
  93. ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
  94. ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
  95. }
  96. ### pofiles
  97. my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
  98. ok( @pofiles, "pofiles is defined");
  99. ok( @pofiles == 2, "pofiles has correct size");
  100. is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
  101. ### links
  102. require IkiWiki::Render;
  103. sub refresh_n_scan(@) {
  104. my @masterfiles_rel=@_;
  105. foreach my $masterfile_rel (@masterfiles_rel) {
  106. my $masterfile=srcfile($masterfile_rel);
  107. IkiWiki::scan($masterfile_rel);
  108. next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
  109. my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
  110. IkiWiki::Plugin::po::refreshpot($masterfile);
  111. IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
  112. map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
  113. }
  114. }
  115. $config{po_link_to}='negotiated';
  116. $msgprefix="links (po_link_to=negotiated)";
  117. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  118. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  119. is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
  120. is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
  121. is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
  122. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  123. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  124. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  125. $config{po_link_to}='current';
  126. $msgprefix="links (po_link_to=current)";
  127. refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
  128. is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
  129. is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable'))], "$msgprefix index.es");
  130. is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable'))], "$msgprefix index.fr");
  131. is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
  132. is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
  133. is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
  134. is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
  135. ### targetpage
  136. $config{usedirs}=0;
  137. $msgprefix="targetpage (usedirs=0)";
  138. is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
  139. is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
  140. $config{usedirs}=1;
  141. $msgprefix="targetpage (usedirs=1)";
  142. is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
  143. is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
  144. is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
  145. is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
  146. is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
  147. is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
  148. ### urlto -> index
  149. $config{po_link_to}='current';
  150. $msgprefix="urlto (po_link_to=current)";
  151. is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
  152. is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
  153. is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
  154. # when asking for a semi-absolute or absolute URL, we can't know what the
  155. # current language is, so for translatable pages we use the master language
  156. is(urlto('nontranslatable'), '/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
  157. is(urlto('translatable'), '/translatable/index.en.html', "$msgprefix 1-arg -> translatable");
  158. is(urlto('nontranslatable', undef, 1), 'http://example.com/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
  159. is(urlto('index', undef, 1), 'http://example.com/index.en.html', "$msgprefix 1-arg -> index");
  160. is(urlto('', undef, 1), 'http://example.com/index.en.html', "$msgprefix 1-arg -> ''");
  161. # FIXME: should these three produce the negotiatable URL instead of the master
  162. # language?
  163. is(urlto(''), '/index.en.html', "$msgprefix 1-arg -> ''");
  164. is(urlto('index'), '/index.en.html', "$msgprefix 1-arg -> index");
  165. is(urlto('translatable', undef, 1), 'http://example.com/translatable/index.en.html', "$msgprefix 1-arg -> translatable");
  166. $config{po_link_to}='negotiated';
  167. $msgprefix="urlto (po_link_to=negotiated)";
  168. is(urlto('', 'index'), './', "$msgprefix index -> ''");
  169. is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
  170. is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
  171. is(urlto('nontranslatable'), '/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
  172. is(urlto('translatable'), '/translatable/', "$msgprefix 1-arg -> translatable");
  173. is(urlto(''), '/', "$msgprefix 1-arg -> ''");
  174. is(urlto('index'), '/', "$msgprefix 1-arg -> index");
  175. is(urlto('nontranslatable', undef, 1), 'http://example.com/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
  176. is(urlto('translatable', undef, 1), 'http://example.com/translatable/', "$msgprefix 1-arg -> translatable");
  177. is(urlto('index', undef, 1), 'http://example.com/', "$msgprefix 1-arg -> index");
  178. is(urlto('', undef, 1), 'http://example.com/', "$msgprefix 1-arg -> ''");
  179. ### bestlink
  180. $config{po_link_to}='current';
  181. $msgprefix="bestlink (po_link_to=current)";
  182. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  183. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  184. $config{po_link_to}='negotiated';
  185. $msgprefix="bestlink (po_link_to=negotiated)";
  186. is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
  187. is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
  188. ### beautify_urlpath
  189. $config{po_link_to}='default';
  190. $msgprefix="beautify_urlpath (po_link_to=default)";
  191. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
  192. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
  193. $config{po_link_to}='negotiated';
  194. $msgprefix="beautify_urlpath (po_link_to=negotiated)";
  195. is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
  196. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
  197. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");
  198. $config{po_link_to}='current';
  199. $msgprefix="beautify_urlpath (po_link_to=current)";
  200. is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
  201. is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
  202. ### re-scan
  203. refresh_n_scan('index.mdwn');
  204. is($pagestate{'index'}{meta}{title}, 'index title');
  205. is($pagestate{'index.es'}{meta}{title}, 'index title');
  206. is($pagestate{'index.fr'}{meta}{title}, 'index title');
  207. refresh_n_scan('test1.mdwn');
  208. is($pagestate{'test1'}{meta}{title}, 'test1 title');
  209. is($pagestate{'test1.es'}{meta}{title}, 'test1 title');
  210. is($pagestate{'test1.fr'}{meta}{title}, 'test1 title');
  211. ### istranslatedto
  212. ok(IkiWiki::Plugin::po::istranslatedto('index', 'es'));
  213. ok(IkiWiki::Plugin::po::istranslatedto('index', 'fr'));
  214. ok(! IkiWiki::Plugin::po::istranslatedto('index', 'cz'));
  215. ok(IkiWiki::Plugin::po::istranslatedto('test1', 'es'));
  216. ok(IkiWiki::Plugin::po::istranslatedto('test1', 'fr'));
  217. ok(! IkiWiki::Plugin::po::istranslatedto('test1', 'cz'));
  218. ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'es'));
  219. ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'cz'));
  220. ok(! IkiWiki::Plugin::po::istranslatedto('test1.es', 'fr'));
  221. ok(! IkiWiki::Plugin::po::istranslatedto('test1.fr', 'es'));