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 => 58;
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.fr'), "index.fr is not 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.es'), "index.es is not translatable");
76 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
77 ok(! IkiWiki::Plugin::po::istranslation('index'), "index is not a translation");
78 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
79 ok(IkiWiki::Plugin::po::istranslation('index.fr'), "index.fr is a translation");
80 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
81 ok(IkiWiki::Plugin::po::istranslation('index.es'), "index.es is a translation");
82 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
83 ok(IkiWiki::Plugin::po::istranslatable('test2'), "test2 is translatable");
84 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
85 ok(! IkiWiki::Plugin::po::istranslation('test2'), "test2 is not a translation");
86 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
87 ok(! IkiWiki::Plugin::po::istranslatable('test3'), "test3 is not translatable");
88 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
89 ok(! IkiWiki::Plugin::po::istranslation('test3'), "test3 is not a translation");
92 require IkiWiki::Render;
94 sub refresh_n_scan(@) {
95 my @masterfiles_rel=@_;
96 foreach my $masterfile_rel (@masterfiles_rel) {
97 my $masterfile=srcfile($masterfile_rel);
98 IkiWiki::scan($masterfile_rel);
99 next unless IkiWiki::Plugin::po::istranslatable(pagename($masterfile_rel));
100 my @pofiles=IkiWiki::Plugin::po::pofiles($masterfile);
101 IkiWiki::Plugin::po::refreshpot($masterfile);
102 IkiWiki::Plugin::po::refreshpofiles($masterfile, @pofiles);
103 map IkiWiki::scan(IkiWiki::abs2rel($_, $config{srcdir})), @pofiles;
107 writefile('index.mdwn', $config{srcdir}, '[[translatable]] [[nontranslatable]]');
108 writefile('translatable.mdwn', $config{srcdir}, '[[nontranslatable]]');
109 writefile('nontranslatable.mdwn', $config{srcdir}, '[[/]] [[translatable]]');
111 $config{po_link_to}='negotiated';
112 $msgprefix="links (po_link_to=negotiated)";
113 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
114 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
115 is_deeply(\@{$links{'index.es'}}, ['translatable.es', 'nontranslatable'], "$msgprefix index.es");
116 is_deeply(\@{$links{'index.fr'}}, ['translatable.fr', 'nontranslatable'], "$msgprefix index.fr");
117 is_deeply(\@{$links{'translatable'}}, ['nontranslatable'], "$msgprefix translatable");
118 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
119 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
120 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
122 $config{po_link_to}='current';
123 $msgprefix="links (po_link_to=current)";
124 refresh_n_scan('index.mdwn', 'translatable.mdwn', 'nontranslatable.mdwn');
125 is_deeply(\@{$links{'index'}}, ['translatable', 'nontranslatable'], "$msgprefix index");
126 is_deeply(\@{$links{'index.es'}}, [ map bestlink('index.es', $_), ('translatable.es', 'nontranslatable')], "$msgprefix index.es");
127 is_deeply(\@{$links{'index.fr'}}, [ map bestlink('index.fr', $_), ('translatable.fr', 'nontranslatable')], "$msgprefix index.fr");
128 is_deeply(\@{$links{'translatable'}}, [bestlink('translatable', 'nontranslatable')], "$msgprefix translatable");
129 is_deeply(\@{$links{'translatable.es'}}, ['nontranslatable'], "$msgprefix translatable.es");
130 is_deeply(\@{$links{'translatable.fr'}}, ['nontranslatable'], "$msgprefix translatable.fr");
131 is_deeply(\@{$links{'nontranslatable'}}, ['/', 'translatable', 'translatable.fr', 'translatable.es'], "$msgprefix nontranslatable");
135 $msgprefix="targetpage (usedirs=0)";
136 is(targetpage('test1', 'html'), 'test1.en.html', "$msgprefix test1");
137 is(targetpage('test1.fr', 'html'), 'test1.fr.html', "$msgprefix test1.fr");
139 $msgprefix="targetpage (usedirs=1)";
140 is(targetpage('index', 'html'), 'index.en.html', "$msgprefix index");
141 is(targetpage('index.fr', 'html'), 'index.fr.html', "$msgprefix index.fr");
142 is(targetpage('test1', 'html'), 'test1/index.en.html', "$msgprefix test1");
143 is(targetpage('test1.fr', 'html'), 'test1/index.fr.html', "$msgprefix test1.fr");
144 is(targetpage('test3', 'html'), 'test3/index.html', "$msgprefix test3 (non-translatable page)");
145 is(targetpage('test3.es', 'html'), 'test3.es/index.html', "$msgprefix test3.es (non-translatable page)");
148 $config{po_link_to}='current';
149 $msgprefix="urlto (po_link_to=current)";
150 is(urlto('', 'index'), './index.en.html', "$msgprefix index -> ''");
151 is(urlto('', 'nontranslatable'), './../index.en.html', "$msgprefix nontranslatable -> ''");
152 is(urlto('', 'translatable.fr'), './../index.fr.html', "$msgprefix translatable.fr -> ''");
153 $config{po_link_to}='negotiated';
154 $msgprefix="urlto (po_link_to=negotiated)";
155 is(urlto('', 'index'), './', "$msgprefix index -> ''");
156 is(urlto('', 'nontranslatable'), './../', "$msgprefix nontranslatable -> ''");
157 is(urlto('', 'translatable.fr'), './../', "$msgprefix translatable.fr -> ''");
160 $config{po_link_to}='current';
161 $msgprefix="bestlink (po_link_to=current)";
162 is(bestlink('test1.fr', 'test2'), 'test2.fr', "$msgprefix test1.fr -> test2");
163 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
164 $config{po_link_to}='negotiated';
165 $msgprefix="bestlink (po_link_to=negotiated)";
166 is(bestlink('test1.fr', 'test2'), 'test2', "$msgprefix test1.fr -> test2");
167 is(bestlink('test1.fr', 'test2.es'), 'test2.es', "$msgprefix test1.fr -> test2.es");
170 $config{po_link_to}='default';
171 $msgprefix="beautify_urlpath (po_link_to=default)";
172 is(IkiWiki::beautify_urlpath('test1/index.en.html'), './test1/index.en.html', "$msgprefix test1/index.en.html");
173 is(IkiWiki::beautify_urlpath('test1/index.fr.html'), './test1/index.fr.html', "$msgprefix test1/index.fr.html");
174 $config{po_link_to}='negotiated';
175 $msgprefix="beautify_urlpath (po_link_to=negotiated)";
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/index.fr.html', "$msgprefix test1/index.fr.html");