# Bloxsom Plugin: MultiViews # Author: Victor Hsieh # Version: 3.0 # It's for Bloxsom 3.0 package Blosxom::Plugin::MultiViews; use strict; use CGI; use constant render_html_switch => 1; # $original_language is the original language you used in your blog # all entries EXCEPT which writen in $original_language should be # named like "XXXX.txt.fr". $original_language version is named normally # such as "XXXX.txt". my $original_language = "zh"; # $default_language is the default language you want to be read for others # if no Accept-Language is matched my $default_language = "en"; # If there's any KEY match the Accept-Language (in order), set the preference # language to VALUE. Use $default_language if non. my %acccepted_language = ( "zh-tw" => "zh", "zh-cn" => "zh", "zh" => "zh", "en" => "en", "perl" => "perl", ); my %html_selection = ( "zh" => "中文", "en" => "English", "perl" => "Perl", ); sub html_switch { my $self = shift; my $base_url = $self->{settings}->{url}; if ($self->{request}->{path_info} ne '/') { $base_url .= $self->{request}->{path_info}; } my $h = '
'; map { $h .= qq($html_selection{$_} ); } keys %html_selection; $h .= '
'; $self->{state}->{current_entry}->{Plugin}->{MultiViews}->{html_switch} = $h; } sub set_accept_language { my $self = shift; my @langs = (CGI::param('lang')); my $lang; $langs[0] or @langs = map { s/;.*//; $_ } split /,/, CGI::http('Accept-language'); for my $l (@langs) { next unless exists $acccepted_language{$l}; $lang = $acccepted_language{$l}; last; } if (!$lang or $lang eq $original_language) { undef $self->{settings}->{find_entries_ext}; } else { $self->{settings}->{find_entries_ext} = "txt.$lang"; } # for my BlosxomCache $self->{request}->{accept_language} = $lang; } 1; __END__ =head1 NAME Blosxom Plug-in: MultiViews =head1 SYNOPSIS Multi-language entries for Blosxom 3.0 =head1 DESCRIPTION You can add multiple-language version of entries to your blog. Let's see an example: I'm a Taiwanese. I'm trying to keep both Chinese and English version in my blog (http://victor.csie.org/blog/). I wrote XXXX.txt in chinese and XXXX.txt.en in English. I'm going to set my default language to "en" for the most people use English, and people who use Chinese will read the Chinese version if their prefer language (Accept-Language in HTTP heder) is "zh", "zh-tw", etc. There are several variable you might want to change, $original_language, $default_language and %acccepted_language. They are all well documented in the upper. Please refer to there for further information. =head1 INSTALLATION =head2 INSTALL ENTRIES Just add this line: Blosxom::Plugin::MultiViews::set_accept_language to your data/.settings/handlers.flow before Blosxom::find_entries . =head2 INSTALL HTML RENDER Add Blosxom::Plugin::MultiViews::html_switch to data/.settings/handlers.flow after Blosxom::run_entries. Edit the .templates/head.html or .templates/foot.html file and add the variable $Plugin::MultiViews::html_switch where you want the language switch to appear. =head1 AUTHOR Victor Hsieh - http://victor.csie.org/ =head1 COPYRIGHT Copyright (c) 2005. Victor Hsieh. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://www.perl.com/perl/misc/Artistic.html =cut