# Blosxom Plugin: AcceptLang # vim:ft=perl:fdm=marker: # Author: Gerfried Fuchs # Version: 0.6.0 # Documentation: See the bottom of this file or type: perldoc AcceptLang package AcceptLang; use CGI qw(param); # --- Configurable variables ----- # available languages our @Langs = ('de', 'en'); # default lang if no requested language is available our $DefaultLang = 'en;q=1.0,de;q=0.8'; # -------------------------------- sub start { # doesn't do anything special {{{ 1; } # }}} sub filter { # filters out unwanted languages {{{ my ($self, $files, $others) = @_; my $acceptlang = (defined param('lang')) ? param('lang') : ($ENV{'HTTP_ACCEPT_LANGUAGE'}) ? $ENV{'HTTP_ACCEPT_LANGUAGE'} : $DefaultLang; my @order = langparse($acceptlang); # filter out everything that isn't wanted or default $blosxom::blog_language = $order[0]; foreach (keys %$files) { delete $files->{$_} unless ( /^[^.]+\.$order[0]\.$blosxom::file_extension$/ or /^[^.]+\.$blosxom::file_extension$/ ); } } # }}} sub langparse { # parse the Accept-Language header {{{ my ($acceptlang) = @_; my %langs; $acceptlang =~ s/\s+//; my @langs = split /,/, $acceptlang; foreach (@langs) { my ($lang, $quality) = m/([a-z]{2})(?:-[a-z]{2})?(?:;q=([0-9.]+))?/i; $quality = -1 if $lang eq '*'; $quality = 1 unless defined $quality; $langs{$lang} = $quality; } my $langs = join('|', @Langs); return grep { /^(?:$langs)$/ } sort { $langs{$b} <=> $langs{$a} } keys %langs; } # }}} 1; __END__ {{{ use Data::Dumper; open FOO, ">/tmp/blaaah" or die "$0: can't write to /tmp/blaaah: $!"; print FOO Dumper(\$acceptlang, \@order); close FOO; =head1 NAME Blosxom Plug-in: AcceptLang =head1 SYNOPSIS Allows to use translated blog entries based on the Accept-Language header or a lang parameter. =head1 DESCRIPTION This plugin will change the naming requirements of your blog entries to ones containing just one dot infront of C<$blosxom::file_extension> and ones that have two dots in between which the language the file is for is specified. This way you can have entries that will be displayed in all languages, and entries that will only be displayed for a specific language. The language code will be taken either from the C> parameter to the request URL or from the C HTTP header. If neither of those are set it will default to C<$AcceptLang::DefaultLang>. You should configure "C<@AcceptLang::Langs>" to an array of the languages you plan to support, and set "C<$AcceptLang::DefaultLang>" to the default language which should be shown when none is given. =head1 OPTIONS =over =item C<@AcceptLang::Langs> defaults to C<('de', 'en')>. The languages you want to support. Please notice that localized languages are not supported yet and not planed. If you feel that this is serious to you please enquire. If you don't know what I am talking about stick with this simple rule: language codes are two characters long, all lower cased. =item C<$AcceptLang::DefaultLang> defaults to C<'en'>. The language which should be chosen if the client hasn't sent a preference. =back =head1 VERSION 0.6.0 =head1 AUTHOR Gerfried Fuchs L, L =head1 SEE ALSO Blosxom Home/Docs/Licensing: L Blosxom Plugin Docs: L =head1 BUGS Address bug reports and comments to the Author. =head1 LICENSE Copyright (c) 2004 Gerfried Fuchs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.