#!/usr/bin/perl # # This plugin allows translations of wiki pages. # # Translatable pages and translations must have the following format: # "pagename.$LANG", where $LANG is a ISO639-1 (two-letter) language code. # # Usage: # # [[linguas ]] # # Note that linguas is only required in one of the pages (the original, # for instance), translations will be automatically updated. Additionally, # it is also possible to specify the title: # # [[linguas title="Localized title"]] # # Jordà Polo , 2006. # package IkiWiki::Plugin::linguas; use warnings; use strict; use IkiWiki; my %titles; my %translations; my %linguas; $linguas{'ca'}{'name'} = 'Català'; $linguas{'de'}{'name'} = 'Deutsch'; $linguas{'en'}{'name'} = 'English'; $linguas{'es'}{'name'} = 'Castellano'; $linguas{'ca'}{'other'} = 'Aquesta pàgina també està disponible en els següents idiomes:'; $linguas{'de'}{'other'} = 'Diese Seite gibt es auch in den folgenden Sprachen:'; $linguas{'en'}{'other'} = 'This page is also available in the following languages:'; $linguas{'es'}{'other'} = 'Esta página también está disponible en los siguientes idiomas:'; sub import { #{{{ IkiWiki::hook(type => "preprocess", id => "linguas", call => \&preprocess); IkiWiki::hook(type => "pagetemplate", id => "linguas", call => \&pagetemplate); } # }}} sub preprocess (@) { #{{{ my %params = @_; my $page = $params{page}; my $title = ($params{title} or undef); my ($basename, $lang) = ($page =~ /(.*)[.]([a-z]{2}$)/); return "[[linguas : untranslatable page]]" if !defined $basename || !defined $lang; if (defined $title) { $titles{$page} = $title; } # depend on the rest of translations my $spec = $basename.".*"; IkiWiki::add_depends($page, $spec); # find translations only once return "" if exists $translations{$basename}; $translations{$basename} = []; foreach my $link (keys %IkiWiki::links) { if (IkiWiki::pagespec_match($link, $spec)) { push @{$translations{$basename}}, $link; } } @{$translations{$basename}} = sort @{$translations{$basename}}; return ""; } # }}} sub pagetemplate (@) { #{{{ my %params = @_; my $page = $params{page}; my $destpage = $params{destpage}; my $template = $params{template}; my ($basename, $lang) = ($page =~ /(.*)[.]([a-z]{2}$)/); return if !defined $basename || !defined $lang || !exists $translations{$basename}; my @links; foreach my $trans (@{$translations{$basename}}) { my $link = $trans; $trans =~ /(.*)[.]([a-z]{2}$)/; if (exists $linguas{$2} && defined $linguas{$2}) { $link = $linguas{$2}{'name'}; } push @links, IkiWiki::htmllink($page, $destpage, $trans, 0, 0, $link); } my $otherlinguas = 'Translations:'; if (exists $linguas{$lang} && exists $linguas{$lang}{'other'}) { $otherlinguas = $linguas{$lang}{'other'}; } $template->param(title => $titles{$page}) if defined $titles{$page} && $template->query(name => "title"); $template->param(otherlinguas => $otherlinguas) if $template->query(name => "otherlinguas"); $template->param(linguas => [ map { link => $_ }, @links ]) if $template->query(name => "linguas"); } # }}} 1