2 # -*- cperl-indent-level: 8; -*-
5 use File::Temp qw{tempdir};
8 unless (eval { require Locale::Po4a::Chooser }) {
10 use Test::More skip_all => "Locale::Po4a::Chooser::new is not available"
13 unless (eval { require Locale::Po4a::Po }) {
15 use Test::More skip_all => "Locale::Po4a::Po::new is not available"
20 use Test::More tests => 65;
22 BEGIN { use_ok("IkiWiki"); }
26 my $dir = tempdir("ikiwiki-test-po.XXXXXXXXXX",
27 DIR => File::Spec->tmpdir,
31 %config=IkiWiki::defaultconfig();
32 $config{srcdir} = "$dir/src";
33 $config{destdir} = "$dir/dst";
34 $config{discussion} = 0;
35 $config{po_master_language} = { code => 'en',
38 $config{po_slave_languages} = {
42 $config{po_translatable_pages}='index or test1 or test2 or translatable';
43 $config{po_link_to}='negotiated';
44 IkiWiki::loadplugins();
45 IkiWiki::checkconfig();
46 ok(IkiWiki::loadplugin('po'), "po plugin loaded");
48 ### seed %pagesources and %pagecase
49 $pagesources{'index'}='index.mdwn';
50 $pagesources{'index.fr'}='index.fr.po';
51 $pagesources{'index.es'}='index.es.po';
52 $pagesources{'test1'}='test1.mdwn';
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;
67 ### istranslatable/istranslation
68 # we run these tests twice because memoization attempts made them
69 # succeed once every two tries...
71 ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
72 ok(IkiWiki::Plugin::po::istranslatable('/index'), "/index is translatable");
73 ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not translatable");
74 ok(! IkiWiki::Plugin::po::istranslatable('index.es'), "index.es is not translatable");
75 ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
76 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
77 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
78 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
79 ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
80 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
81 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
82 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
83 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
87 require IkiWiki::Render;
89 sub refresh_n_scan(@) {
90 my @masterfiles_rel=@_;
91 foreach my $masterfile_rel (@masterfiles_rel) {
92 my $masterfile=srcfile($masterfile_rel);
93 IkiWiki::scan($masterfile_rel);
94 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
95 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
96 IkiWiki::Plugin::po::refreshpot($masterfile);
97 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
98 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
102 writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
103 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
104 writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
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");
117 $config{po_link_to}='current';
118 $msgprefix="links (po_link_to=current)";
119 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
120 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
121 is_deeply(\@{$links{'index.es'}}, [ map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')], "$msgprefix index.es");
122 is_deeply(\@{$links{'index.fr'}}, [ map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')], "$msgprefix index.fr");
123 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
124 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
125 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
126 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
130 $msgprefix="targetpage (usedirs=0)";
131 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
132 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
134 $msgprefix="targetpage (usedirs=1)";
135 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
136 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
137 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
138 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
139 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
140 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
143 $config{po_link_to}='current';
144 $msgprefix="urlto (po_link_to=current)";
145 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
146 is(urlto('', 'nontranslatable'), './../index.en.html', "$msgprefix nontranslatable -> ''");
147 is(urlto('', 'translatable.fr'), './../index.fr.html', "$msgprefix translatable.fr -> ''");
148 $config{po_link_to}='negotiated';
149 $msgprefix="urlto (po_link_to=negotiated)";
150 is(urlto('', 'index'), './', "$msgprefix index -> ''");
151 is(urlto('', 'nontranslatable'), './../', "$msgprefix nontranslatable -> ''");
152 is(urlto('', 'translatable.fr'), './../', "$msgprefix translatable.fr -> ''");
155 $config{po_link_to}='current';
156 $msgprefix="bestlink (po_link_to=current)";
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 $config{po_link_to}='negotiated';
160 $msgprefix="bestlink (po_link_to=negotiated)";
161 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
162 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
165 $config{po_link_to}='default';
166 $msgprefix="beautify_urlpath (po_link_to=default)";
167 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
168 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
169 $config{po_link_to}='negotiated';
170 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
171 is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
172 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
173 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");