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;
68 writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
69 writefile('test1.mdwn', $config{srcdir}, 'test1 content');
70 writefile('test2.mdwn', $config{srcdir}, 'test2 content');
71 writefile('test3.mdwn', $config{srcdir}, 'test3 content');
72 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
73 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...
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('test2'), "test2 is translatable");
89 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
90 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
91 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
95 require IkiWiki::Render;
97 sub refresh_n_scan(@) {
98 my @masterfiles_rel=@_;
99 foreach my $masterfile_rel (@masterfiles_rel) {
100 my $masterfile=srcfile($masterfile_rel);
101 IkiWiki::scan($masterfile_rel);
102 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
103 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
104 IkiWiki::Plugin::po::refreshpot($masterfile);
105 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
106 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
110 $config{po_link_to}='negotiated';
111 $msgprefix="links (po_link_to=negotiated)";
112 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
113 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
114 is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
115 is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
116 is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
117 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
118 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
119 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
121 $config{po_link_to}='current';
122 $msgprefix="links (po_link_to=current)";
123 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
124 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
125 is_deeply(\@{$links{'index.es'}}, [ map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')], "$msgprefix index.es");
126 is_deeply(\@{$links{'index.fr'}}, [ map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')], "$msgprefix index.fr");
127 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
128 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
129 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
130 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
134 $msgprefix="targetpage (usedirs=0)";
135 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
136 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
138 $msgprefix="targetpage (usedirs=1)";
139 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
140 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
141 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
142 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
143 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
144 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
147 $config{po_link_to}='current';
148 $msgprefix="urlto (po_link_to=current)";
149 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
150 is(urlto('', 'nontranslatable'), '../index.en.html', "$msgprefix nontranslatable -> ''");
151 is(urlto('', 'translatable.fr'), '../index.fr.html', "$msgprefix translatable.fr -> ''");
152 $config{po_link_to}='negotiated';
153 $msgprefix="urlto (po_link_to=negotiated)";
154 is(urlto('', 'index'), './', "$msgprefix index -> ''");
155 is(urlto('', 'nontranslatable'), '../', "$msgprefix nontranslatable -> ''");
156 is(urlto('', 'translatable.fr'), '../', "$msgprefix translatable.fr -> ''");
159 $config{po_link_to}='current';
160 $msgprefix="bestlink (po_link_to=current)";
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");
163 $config{po_link_to}='negotiated';
164 $msgprefix="bestlink (po_link_to=negotiated)";
165 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
166 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
169 $config{po_link_to}='default';
170 $msgprefix="beautify_urlpath (po_link_to=default)";
171 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
172 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
173 $config{po_link_to}='negotiated';
174 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
175 is(IkiWiki::beautify_urlpath('test1/index.html'), './test1/', "$msgprefix test1/index.html");
176 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/', "$msgprefix test1/index.en.html");
177 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/', "$msgprefix test1/index.fr.html");