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...
70 ok(IkiWiki::Plugin::po::istranslatable('index'), "index is translatable");
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'), "/index is translatable");
74 ok(! IkiWiki::Plugin::po::istranslatable('index.fr'), "index.fr is not 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.es'), "index.es is not translatable");
78 ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
79 ok(! IkiWiki::Plugin::po::istranslatable('/index.fr'), "/index.fr is not translatable");
80 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
81 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
82 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
83 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
84 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
85 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
86 ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
87 ok(IkiWiki::Plugin::po::istranslation('/index.fr'), "/index.fr is a translation");
88 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
89 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
90 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
91 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
92 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
93 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
94 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
95 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
98 require IkiWiki::Render;
100 sub refresh_n_scan(@) {
101 my @masterfiles_rel=@_;
102 foreach my $masterfile_rel (@masterfiles_rel) {
103 my $masterfile=srcfile($masterfile_rel);
104 IkiWiki::scan($masterfile_rel);
105 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
106 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
107 IkiWiki::Plugin::po::refreshpot($masterfile);
108 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
109 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
113 writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
114 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
115 writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
117 $config{po_link_to}='negotiated';
118 $msgprefix="links (po_link_to=negotiated)";
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'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
122 is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
123 is_deeply(\@{$links{'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");
128 $config{po_link_to}='current';
129 $msgprefix="links (po_link_to=current)";
130 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
131 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
132 is_deeply(\@{$links{'index.es'}}, [ map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')], "$msgprefix index.es");
133 is_deeply(\@{$links{'index.fr'}}, [ map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')], "$msgprefix index.fr");
134 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
135 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
136 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
137 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
141 $msgprefix="targetpage (usedirs=0)";
142 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
143 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
145 $msgprefix="targetpage (usedirs=1)";
146 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
147 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
148 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
149 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
150 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
151 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
154 $config{po_link_to}='current';
155 $msgprefix="urlto (po_link_to=current)";
156 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
157 is(urlto('', 'nontranslatable'), './../index.en.html', "$msgprefix nontranslatable -> ''");
158 is(urlto('', 'translatable.fr'), './../index.fr.html', "$msgprefix translatable.fr -> ''");
159 $config{po_link_to}='negotiated';
160 $msgprefix="urlto (po_link_to=negotiated)";
161 is(urlto('', 'index'), './', "$msgprefix index -> ''");
162 is(urlto('', 'nontranslatable'), './../', "$msgprefix nontranslatable -> ''");
163 is(urlto('', 'translatable.fr'), './../', "$msgprefix translatable.fr -> ''");
166 $config{po_link_to}='current';
167 $msgprefix="bestlink (po_link_to=current)";
168 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
169 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
170 $config{po_link_to}='negotiated';
171 $msgprefix="bestlink (po_link_to=negotiated)";
172 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
173 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
176 $config{po_link_to}='default';
177 $msgprefix="beautify_urlpath (po_link_to=default)";
178 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
179 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
180 $config{po_link_to}='negotiated';
181 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
182 is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
183 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
184 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");