2 # -*- cperl-indent-level: 8; -*-
5 use File::Temp qw{tempdir};
9 unless (eval { require Locale::Po4a::Chooser }) {
11 use Test::More skip_all => "Locale::Po4a::Chooser::new is not available"
14 unless (eval { require Locale::Po4a::Po }) {
16 use Test::More skip_all => "Locale::Po4a::Po::new is not available"
23 BEGIN { use_ok("IkiWiki"); }
27 my $dir = tempdir("ikiwiki-test-po.XXXXXXXXXX",
28 DIR => File::Spec->tmpdir,
32 %config=IkiWiki::defaultconfig();
33 $config{srcdir} = "$dir/src";
34 $config{destdir} = "$dir/dst";
35 $config{destdir} = "$dir/dst";
36 $config{underlaydirbase} = "/dev/null";
37 $config{underlaydir} = "/dev/null";
38 $config{url} = "http://example.com";
39 $config{cgiurl} = "http://example.com/ikiwiki.cgi";
40 $config{discussion} = 0;
41 $config{po_master_language} = { code => 'en',
44 $config{po_slave_languages} = {
48 $config{po_translatable_pages}='index or test1 or test2 or translatable or debian*';
49 $config{po_link_to}='negotiated';
50 IkiWiki::loadplugins();
51 ok(IkiWiki::loadplugin('meta'), "meta plugin loaded");
52 ok(IkiWiki::loadplugin('po'), "po plugin loaded");
53 IkiWiki::checkconfig();
55 ### seed %pagesources and %pagecase
56 $pagesources{'index'}='index.mdwn';
57 $pagesources{'index.fr'}='index.fr.po';
58 $pagesources{'index.es'}='index.es.po';
59 $pagesources{'test1'}='test1.mdwn';
60 $pagesources{'test1.es'}='test1.es.po';
61 $pagesources{'test1.fr'}='test1.fr.po';
62 $pagesources{'test2'}='test2.mdwn';
63 $pagesources{'test2.es'}='test2.es.po';
64 $pagesources{'test2.fr'}='test2.fr.po';
65 $pagesources{'test3'}='test3.mdwn';
66 $pagesources{'test3.es'}='test3.es.mdwn';
67 $pagesources{'translatable'}='translatable.mdwn';
68 $pagesources{'translatable.fr'}='translatable.fr.po';
69 $pagesources{'translatable.es'}='translatable.es.po';
70 $pagesources{'nontranslatable'}='nontranslatable.mdwn';
71 $pagesources{'debian911356'}='debian911356.mdwn';
72 $pagesources{'debian911356ish'}='debian911356ish.mdwn';
73 $pagesources{'debian911356.fr'}='debian911356.fr.po';
74 $pagesources{'debian911356ish.fr'}='debian911356ish.fr.po';
75 $pagesources{'debian911356-inlined'}='debian911356-inlined.mdwn';
76 $pagesources{'debian911356-inlined.fr'}='debian911356-inlined.fr.po';
77 $pagesources{'templates/feedlink.tmpl'}='templates/feedlink.tmpl';
78 $pagesources{'templates/inlinepage.tmpl'}='templates/inlinepage.tmpl';
80 foreach my $page (keys %pagesources) {
81 $IkiWiki::pagecase{lc $page}=$page;
82 $IkiWiki::pagectime{$page}=$now;
83 $IkiWiki::pagemtime{$page}=$now;
87 writefile('index.mdwn', $config{srcdir},
88 "[[!meta title=\"index title\"]]\n[[translatable]] [[nontranslatable]]");
89 writefile('test1.mdwn', $config{srcdir},
90 "[[!meta title=\"test1 title\"]]\ntest1 content");
91 writefile('test2.mdwn', $config{srcdir}, 'test2 content');
92 writefile('test3.mdwn', $config{srcdir}, 'test3 content');
93 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
94 writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
95 writefile('debian911356.mdwn', $config{srcdir}, <<EOF);
98 [[!inline pages="debian911356-inlined" raw="yes"]]
102 [[!inline pages="debian911356-inlined" raw="yes"]]
106 writefile('debian911356-inlined.mdwn', $config{srcdir}, <<EOF);
109 writefile('debian911356.fr.po', $config{srcdir}, <<EOF);
111 "MIME-Version: 1.0\\n"
112 "Content-Type: text/plain; charset=UTF-8\\n"
113 "Content-Transfer-Encoding: 8bit\\n"
115 msgid "Before first inline"
116 msgstr "Avant la première inline"
118 msgid "[[!inline pages=\\"debian911356-inlined\\" raw=\\"yes\\"]]\\n"
119 msgstr "[[!inline pages=\\"debian911356-inlined.fr\\" raw=\\"yes\\"]]\\n"
121 msgid "Between inlines"
122 msgstr "Entre les inlines"
124 msgid "After inlines"
125 msgstr "Après les inlines"
127 writefile('debian911356-inlined.fr.po', $config{srcdir}, <<EOF);
128 msgid "English content"
129 msgstr "Contenu français"
131 writefile('debian911356ish.mdwn', $config{srcdir}, <<EOF);
134 [[!inline pages="debian911356-inlined"]]
138 [[!inline pages="debian911356-inlined"]]
142 writefile('debian911356ish.fr.po', $config{srcdir}, <<EOF);
144 "MIME-Version: 1.0\\n"
145 "Content-Type: text/plain; charset=UTF-8\\n"
146 "Content-Transfer-Encoding: 8bit\\n"
148 msgid "Before first inline"
149 msgstr "Avant la première inline"
151 msgid "[[!inline pages=\\"debian911356-inlined\\"]]\\n"
152 msgstr "[[!inline pages=\\"debian911356-inlined.fr\\"]]\\n"
154 msgid "Between inlines"
155 msgstr "Entre les inlines"
157 msgid "After inlines"
158 msgstr "Après les inlines"
160 # We don't actually care what the feed links look like, so skip them
161 writefile('templates/feedlink.tmpl', $config{srcdir}, <<EOF);
164 # Make inlines' appearance predictable so we can screen-scrape them
165 writefile('templates/inlinepage.tmpl', $config{srcdir}, <<EOF);
166 <div class="inlinecontent">
167 <h6><TMPL_VAR TITLE></h6>
169 </div><!--inlinecontent-->
172 ### istranslatable/istranslation
173 # we run these tests twice because memoization attempts made them
174 # succeed once every two tries...
176 ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
177 ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
178 ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
179 ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
180 ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
181 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
182 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
183 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
184 ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
185 ok(IkiWiki::Plugin::po::istranslatable('test1'), "test1 is translatable");
186 ok(IkiWiki::Plugin::po::istranslation('test1.es'), "test1.es is a translation");
187 ok(IkiWiki::Plugin::po::istranslation('test1.fr'), "test1.fr is a translation");
188 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
189 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
190 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
191 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
196 my @pofiles = IkiWiki::Plugin::po::pofiles(srcfile("index.mdwn"));
197 ok( @pofiles, "pofiles is defined");
198 ok( @pofiles == 2, "pofiles has correct size");
199 is_deeply(\@pofiles, ["$config{srcdir}/index.es.po", "$config{srcdir}/index.fr.po"], "pofiles content is correct");
202 require IkiWiki::Render;
204 sub refresh_n_scan(@) {
205 my @masterfiles_rel=@_;
206 foreach my $masterfile_rel (@masterfiles_rel) {
207 my $masterfile=srcfile($masterfile_rel);
208 IkiWiki::scan($masterfile_rel);
209 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
210 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
211 IkiWiki::Plugin::po::refreshpot($masterfile);
212 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
213 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
217 $config{po_link_to}='negotiated';
218 $msgprefix="links (po_link_to=negotiated)";
219 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
220 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
221 is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
222 is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
223 is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
224 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
225 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
226 is_deeply([sort @{$links{'nontranslatable'}}], [sort('/', 'translatable', 'translatable.fr', 'translatable.es')], "$msgprefix nontranslatable");
228 $config{po_link_to}='current';
229 $msgprefix="links (po_link_to=current)";
230 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
231 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
232 is_deeply(\@{$links{'index.es'}}, [ (map bestlink('index.es', $_), ('translatable.es', 'nontranslatable'))], "$msgprefix index.es");
233 is_deeply(\@{$links{'index.fr'}}, [ (map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable'))], "$msgprefix index.fr");
234 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
235 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
236 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
237 is_deeply([sort @{$links{'nontranslatable'}}], [sort('/', 'translatable', 'translatable.fr', 'translatable.es')], "$msgprefix nontranslatable");
241 $msgprefix="targetpage (usedirs=0)";
242 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
243 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
245 $msgprefix="targetpage (usedirs=1)";
246 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
247 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
248 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
249 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
250 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
251 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
254 $config{po_link_to}='current';
255 $msgprefix="urlto (po_link_to=current)";
256 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
257 is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
258 is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
259 # when asking for a semi-absolute or absolute URL, we can't know what the
260 # current language is, so for translatable pages we use the master language
261 is(urlto('nontranslatable'), '/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
262 is(urlto('translatable'), '/translatable/index.en.html', "$msgprefix 1-arg -> translatable");
263 is(urlto('nontranslatable', undef, 1), 'http://example.com/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
264 is(urlto('index', undef, 1), 'http://example.com/index.en.html', "$msgprefix 1-arg -> index");
265 is(urlto('', undef, 1), 'http://example.com/index.en.html', "$msgprefix 1-arg -> ''");
266 # FIXME: should these three produce the negotiatable URL instead of the master
268 is(urlto(''), '/index.en.html', "$msgprefix 1-arg -> ''");
269 is(urlto('index'), '/index.en.html', "$msgprefix 1-arg -> index");
270 is(urlto('translatable', undef, 1), 'http://example.com/translatable/index.en.html', "$msgprefix 1-arg -> translatable");
272 $config{po_link_to}='negotiated';
273 $msgprefix="urlto (po_link_to=negotiated)";
274 is(urlto('', 'index'), './', "$msgprefix index -> ''");
275 is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
276 is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
277 is(urlto('nontranslatable'), '/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
278 is(urlto('translatable'), '/translatable/', "$msgprefix 1-arg -> translatable");
279 is(urlto(''), '/', "$msgprefix 1-arg -> ''");
280 is(urlto('index'), '/', "$msgprefix 1-arg -> index");
281 is(urlto('nontranslatable', undef, 1), 'http://example.com/nontranslatable/', "$msgprefix 1-arg -> nontranslatable");
282 is(urlto('translatable', undef, 1), 'http://example.com/translatable/', "$msgprefix 1-arg -> translatable");
283 is(urlto('index', undef, 1), 'http://example.com/', "$msgprefix 1-arg -> index");
284 is(urlto('', undef, 1), 'http://example.com/', "$msgprefix 1-arg -> ''");
287 $config{po_link_to}='current';
288 $msgprefix="bestlink (po_link_to=current)";
289 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
290 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
291 $config{po_link_to}='negotiated';
292 $msgprefix="bestlink (po_link_to=negotiated)";
293 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
294 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
297 $config{po_link_to}='default';
298 $msgprefix="beautify_urlpath (po_link_to=default)";
299 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
300 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
301 $config{po_link_to}='negotiated';
302 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
303 is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
304 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
305 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");
306 $config{po_link_to}='current';
307 $msgprefix="beautify_urlpath (po_link_to=current)";
308 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
309 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
312 refresh_n_scan('index.mdwn');
313 is($pagestate{'index'}{meta}{title}, 'index title');
314 is($pagestate{'index.es'}{meta}{title}, 'index title');
315 is($pagestate{'index.fr'}{meta}{title}, 'index title');
316 refresh_n_scan('test1.mdwn');
317 is($pagestate{'test1'}{meta}{title}, 'test1 title');
318 is($pagestate{'test1.es'}{meta}{title}, 'test1 title');
319 is($pagestate{'test1.fr'}{meta}{title}, 'test1 title');
322 ok(IkiWiki::Plugin::po::istranslatedto('index', 'es'));
323 ok(IkiWiki::Plugin::po::istranslatedto('index', 'fr'));
324 ok(! IkiWiki::Plugin::po::istranslatedto('index', 'cz'));
325 ok(IkiWiki::Plugin::po::istranslatedto('test1', 'es'));
326 ok(IkiWiki::Plugin::po::istranslatedto('test1', 'fr'));
327 ok(! IkiWiki::Plugin::po::istranslatedto('test1', 'cz'));
328 ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'es'));
329 ok(! IkiWiki::Plugin::po::istranslatedto('nontranslatable', 'cz'));
330 ok(! IkiWiki::Plugin::po::istranslatedto('test1.es', 'fr'));
331 ok(! IkiWiki::Plugin::po::istranslatedto('test1.fr', 'es'));
334 ok(IkiWiki::Plugin::po::islanguagecode('en'));
335 ok(IkiWiki::Plugin::po::islanguagecode('es'));
336 ok(IkiWiki::Plugin::po::islanguagecode('arn'));
337 ok(! IkiWiki::Plugin::po::islanguagecode('es_'));
338 ok(! IkiWiki::Plugin::po::islanguagecode('_en'));
340 # Actually render translated pages
344 foreach my $page (sort keys %pagesources) {
345 my $source = "$config{srcdir}/$pagesources{$page}";
347 IkiWiki::scan($pagesources{$page});
351 # This is the most complicated case, so use this while we test rendering
352 $config{po_link_to}='current';
354 foreach my $page (sort keys %pagesources) {
355 my $source = "$config{srcdir}/$pagesources{$page}";
356 if (-e $source && defined IkiWiki::pagetype($pagesources{$page})) {
357 IkiWiki::scan($pagesources{$page});
358 my $content = readfile($source);
359 #print STDERR "-------------------------------------\n";
360 #print STDERR "SOURCE: $page: $content\n";
361 $content = IkiWiki::filter($page, $page, $content);
362 #print STDERR "FILTERED: $page: $content\n";
363 $content = IkiWiki::preprocess($page, $page, $content);
364 #print STDERR "PREPROCESSED: $page: $content\n";
365 $content = IkiWiki::linkify($page, $page, $content);
366 #print STDERR "LINKIFIED: $page: $content\n";
367 $content = IkiWiki::htmlize($page, $page, IkiWiki::pagetype($pagesources{$page}), $content);
368 #print STDERR "HTMLIZED: $page: $content\n";
369 IkiWiki::run_hooks(format => sub {
375 #print STDERR "FORMATTED: $page: $content\n";
376 $output{$page} = $content;
380 like($output{index}, qr{
382 <a\s+href="\./translatable/index\.en\.html">
385 <a\s+href="\./nontranslatable/">
391 like($output{'index.es'}, qr{
393 <a\s+href="\./translatable/index\.es\.html">
396 <a\s+href="\./nontranslatable/">
402 like($output{'index.fr'}, qr{
404 <a\s+href="\./translatable/index\.fr\.html">
407 <a\s+href="\./nontranslatable/">
413 like($output{'translatable'}, qr{
414 <a\s+href="\.\./nontranslatable/">
420 local $TODO = 'was [[/]] meant to be a link to the index?';
421 unlike($output{'nontranslatable'}, qr{
425 like($output{'nontranslatable'}, qr{
426 <a\s+href="\.\./translatable/index\.en\.html">
431 like($output{debian911356}, qr{
432 <p>Before\sfirst\sinline</p>
434 <p>English\scontent</p>
436 <p>Between\sinlines</p>
438 <p>English\scontent</p>
440 <p>After\sinlines</p>
443 like($output{'debian911356.fr'}, qr{
444 <p>Avant\sla\spremière\sinline</p>
446 <p>Contenu\sfrançais</p>
448 <p>Entre\sles\sinlines</p>
450 .* # TODO: This paragraph gets mangled (Debian #911356)
452 <p>Après\sles\sinlines</p>
456 local $TODO = "Debian bug #911356";
457 like($output{'debian911356.fr'}, qr{
458 <p>Avant\sla\spremière\sinline</p>
460 <p>Contenu\sfrançais</p>
462 <p>Entre\sles\sinlines</p>
464 <p>Contenu\sfrançais</p>
466 <p>Après\sles\sinlines</p>
470 # Variation of Debian #911356 without using raw inlines.
471 like($output{debian911356ish}, qr{
472 <p>Before\sfirst\sinline</p>
476 <div\sclass="inlinecontent">
478 <h6>debian911356-inlined</h6>
480 <p>English\scontent</p>
482 </div><!--inlinecontent-->
484 <p>Between\sinlines</p>
488 <div\sclass="inlinecontent">
490 <h6>debian911356-inlined</h6>
492 <p>English\scontent</p>
494 </div><!--inlinecontent-->
496 <p>After\sinlines</p>
499 like($output{'debian911356ish.fr'}, qr{
500 <p>Avant\sla\spremière\sinline</p>
504 <div\sclass="inlinecontent">
506 <h6>debian911356-inlined\.fr</h6>
508 <p>Contenu\sfrançais</p>
510 </div><!--inlinecontent-->
512 <p>Entre\sles\sinlines</p>
514 .* # TODO: This paragraph gets mangled (Debian #911356)
516 <p>Après\sles\sinlines</p>
520 local $TODO = "Debian bug #911356";
521 like($output{'debian911356ish.fr'}, qr{
522 <p>Avant\sla\spremière\sinline</p>
526 <div\sclass="inlinecontent">
528 <h6>debian911356-inlined\.fr</h6>
530 <p>Contenu\sfrançais</p>
532 </div><!--inlinecontent-->
534 <p>Entre\sles\sinlines</p>
538 <div\sclass="inlinecontent">
540 <h6>debian911356-inlined\.fr</h6>
542 <p>Contenu\sfrançais</p>
544 </div><!--inlinecontent-->
546 <p>Après\sles\sinlines</p>