Apache-LangPrefCookie

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

-*-text-*-

Revision history for Perl extension Apache::LangPrefCookie.

0.01  Wed Dec 14 12:52:34 2005
	- original version; created by h2xs 1.23 with options
		-X -n Apache::LangPrefCookie

0.02 - LangPrefCookieName directive to alter the default cookie-name
     - comments
     - write debug-message into error log 

0.03 - use "our" to declare $VERSION
     - avoid parsing an empty Accept-Language Header
     - made debug-message clearer
     - fixed language-code regexp
     - better variablenames
     - more comments
     - POD updates 

MANIFEST  view on Meta::CPAN

t/conf/startup.pl.in
t/htdocs/langprefcookie/index.html.de
t/htdocs/langprefcookie/index.html.en
t/htdocs/langprefcookie/index.html.html
t/htdocs/langprefcookie/index.html.it
t/htdocs/langprefcookie/switch.html
t/htdocs/langprefcookie/foo/index.html.de
t/htdocs/langprefcookie/foo/index.html.en
t/htdocs/langprefcookie/foo/index.html.html
t/htdocs/langprefcookie/foo/index.html.it
lib/Apache/LangPrefCookie.pm
META.yml                                 Module meta-data (added by MakeMaker)

META.yml  view on Meta::CPAN

# http://module-build.sourceforge.net/META-spec.html
#XXXXXXX This is a prototype!!!  It will change in the future!!! XXXXX#
name:         Apache-LangPrefCookie
version:      1.03
version_from: lib/Apache/LangPrefCookie.pm
installdirs:  site
requires:
    Apache::Cookie:                
    Apache::Request:               
    Apache::Test:                  

distribution_type: module
generated_by: ExtUtils::MakeMaker version 6.30_01

Makefile.PL  view on Meta::CPAN


use 5.006;
use ExtUtils::MakeMaker;
use Apache::TestMM qw(test clean);

# prerequisites
my %require = (
    "Apache::Test"    => "",    # any version will do
    'Apache::Request' => "",
    'Apache::Cookie'  => "",
);

my @scripts = qw(t/TEST);

# accept the configs from command line
Apache::TestMM::filter_args();
Apache::TestMM::generate_script('t/TEST');

WriteMakefile(
    NAME         => 'Apache::LangPrefCookie',
    VERSION_FROM => 'lib/Apache/LangPrefCookie.pm',    # finds $VERSION
    PREREQ_PM    => \%require,
    ABSTRACT_FROM =>
      'lib/Apache/LangPrefCookie.pm',    # retrieve abstract from module
    AUTHOR => 'Hansjoerg Pehofer <hansjoerg.pehofer@uibk.ac.at>',
    clean  => { FILES => "@{ clean_files() }", },
);

sub clean_files {
    return [@scripts];
}

README  view on Meta::CPAN

Apache-LangPrefCookie version 1.03
==================================
DESCRIPTION

This module looks for a cookie providing a language-code as its value.
This preference is then implanted into httpd's representation of the
Accept-Language header, just as if the client had asked for it as #1
choice. The original preferences are still present, albeit with lowered
q-values. The cookie's name is configurable, as described in the
examples. Setting/modifying/deleting such a cookie is to be handled
separately; Apache::LangPrefCookie just consumes it.

After that, it's up to httpd's mod_negotiation to choose the best
deliverable representation.

CHANGES

0.05 - first released Version.

1.00 - fix typo in regexp
     - cleanup Makefile.PL, Perl >= 5.6.0 required.

lib/Apache/LangPrefCookie.pm  view on Meta::CPAN

package Apache::LangPrefCookie;

use strict;
use warnings;

use Apache::Constants qw(OK DECLINED);
use Apache::Request;
use Apache::Cookie;
use Apache::Log ();

our $VERSION = '1.03';

sub handler {
    my $r           = Apache::Request->new(shift);
    my %cookies     = Apache::Cookie->new($r)->parse;
    my $cookie_name = $r->dir_config('LangPrefCookieName') || 'prefer-language';
    my @ua_lang_prefs;

    # $r->log->debug("Looking for cookie: \"$cookie_name\"");

    $r->header_out( 'Vary',
                    $r->header_out('Vary') ? $r->header_out('Vary') . 'cookie'
                    :                        'cookie'
        );

    # if we have no cookie, this is none of our business

lib/Apache/LangPrefCookie.pm  view on Meta::CPAN

            if (m/^(?:\w{1,8}(?:-\w{1,8})*|\*)$/)
            {
                $language_ranges .= "$_;q=$qvalue, ";
                $qvalue *= .9;
            }
        } ( $cookie_pref_lang, @ua_lang_prefs );
        $language_ranges =~ s/,\s*$//;
        return DECLINED unless length $language_ranges;
        $r->header_in( "Accept-Language", $language_ranges );
        $r->log->debug(
"Cookie \"$cookie_name\" requested \"$cookie_pref_lang\", set \"Accept-Language: $language_ranges\""
        );
    }
    return OK;
}

# taken and modified from Philippe M. Chiasson's Apache::Language;
# later, Aldo Calpini (dada) showed how to get rid of $`
# returns a sorted (from most to least acceptable) list of languages.
sub parse_accept_language_header {
    my $language_ranges = shift;

lib/Apache/LangPrefCookie.pm  view on Meta::CPAN

        }
    }
    return sort { $pairs{$b} <=> $pairs{$a} } keys %pairs;
}

1;
__END__

=head1 NAME

Apache::LangPrefCookie - implant a language-preference given by
cookie into httpd's representation of the Accept-Language HTTP-header.

=head1 SYNOPSIS

  <Location />
     PerlInitHandler  Apache::LangPrefCookie
  </Location>

  <Location /foo>
     # optionally set a custom cookie-name, default is "prefer-language"
     PerlSetVar LangPrefCookieName "foo-pref"
  </Location>


=head1 DESCRIPTION

This module looks for a cookie providing a language-code as its value.
This preference is then implanted into httpd's representation of the
C<Accept-Language> header, just as if the client had asked for it as #1
choice. The original preferences are still present, albeit with lowered
q-values. The cookie's name is configurable, as described in the
examples. Setting/modifying/deleting such a cookie is to be handled
separately; F<Apache::LangPrefCookie> just consumes it.

After that, it's up to httpd's mod_negotiation to choose the best
deliverable representation.

=head2 WHY?

I had demands to let users switch language I<once> for a given site.
Additionally, the availability and languages of translations offered
vary over places within this site.

lib/Apache/LangPrefCookie.pm  view on Meta::CPAN

different preference just for certain realms within one site.

I dislike solutions involving virtual paths, because they normally
lengthen and multiply URIs for a given resource.

=head1 EXAMPLE COOKIE

C<prefer-language=x-klingon;expires=Saturday 31-Dec-05 24:00:00 GMT;path=/>

Optionally, the default cookie name C<prefer-language> can be overridden
by setting the C<LangPrefCookieName> variable:

C<PerlSetVar LangPrefCookieName "mypref">

C<mypref=x-klingon;expires=Saturday 31-Dec-05 24:00:00 GMT;path=/>

=head1 SEE ALSO

L<mod_perl(3)>

L<http://httpd.apache.org/docs/1.3/content-negotiation.html>

L<http://httpd.apache.org/docs/1.3/mod/mod_negotiation.html>

t/compiles.t  view on Meta::CPAN

# -*- perl -*-

use Test::More tests => 1;
BEGIN { use_ok('Apache::LangPrefCookie') };

t/conf/extra.conf.in  view on Meta::CPAN

AddLanguage de    .de
AddLanguage it    .it
AddLanguage es    .es

LanguagePriority en de it fr es

<Location />
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    DirectoryIndex index.html index.htm index.xml
    PerlInitHandler Apache::LangPrefCookie
</Location>

<Location /langprefcookie/foo>
    PerlSetVar LangPrefCookieName "foo-pref"
</Location>

t/conf/startup.pl.in  view on Meta::CPAN

# $Id: startup.pl,v 1.5 2005/08/10 08:20:57 c10232 Exp $ -*-perl-*- 
use strict;

use lib("@ServerRoot@/../lib");
use Apache::Request;
use Apache::LangPrefCookie ();

1;

t/htdocs/langprefcookie/switch.html  view on Meta::CPAN

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
<title>Switch Lang</title>
<script type="text/javascript">
function Set_Cookie( value ) {
  document.cookie = "prefer-language=" + escape( value );
}
</script>
</head>

<body>
<h1>Switch Langprefs</h1>

<a href="." onclick="Set_Cookie('it');">Italiano</a>
<a href="." onclick="Set_Cookie('de');">Deutsch</a>
<a href="." onclick="Set_Cookie('en');">English</a>
<a href="." onclick="Set_Cookie('x-klingon');">Klingon</a>
<a href="." onclick="Set_Cookie('*bogus*');">Bogus</a>
<a href="." onclick="Set_Cookie('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX...
<hr>
<address></address>
<!-- hhmts start -->Last modified: Thu Dec 15 16:02:51 CET 2005 <!-- hhmts end -->
</body> </html>

t/requests.t  view on Meta::CPAN

}

sub do_test {
    my ($url, $accept_language,  $cookie, $expect_resp, $resp, $resp_content) = @_;

    if (length $cookie) {
        Apache::TestRequest::user_agent(cookie_jar => {});
    }

    if ($accept_language) {
        $resp = GET $url, 'Accept-Language' => $accept_language, 'Cookie' => $cookie;
    } else {
        $resp = GET $url;
    }
    $resp_content = $resp->content;
    $resp_content =~ s!^.*<h1>(.+)</h1>.*$!$1!si;
    return $resp_content;
}

__DATA__
/langprefcookie/!!!English



( run in 0.491 second using v1.01-cache-2.11-cpan-4e96b696675 )