NAME

Plack::Middleware::I18N - language detection

SYNOPSIS

use Plack::Builder;

builder {
    enable 'I18N', i18n => $i18n;

    ...
};

DESCRIPTION

Plack::Middleware::I18N detects client's languages and set $env variables.

Language detection

All detected languages are validated against available languages.

From path

When PATH_INFO contains something like /en/path/to, then en is detected as a language and PATH_INFO is changed to /path/to.

From session

When psgix.session contains plack.i18n.language then it is used as a language. Session option is set after every detection.

From HTTP_ACCEPT_LANGUAGE

Detects language from HTTP_ACCEPT_LANGUAGE header using I18N::AcceptLanguage.

From custom callback

Sometimes a more sophisticated language detection is needed. Thus a custom callback can be provided. For example:

enable 'I18N', custom_cb => sub {
    my ($env, $lang) = @_;

    return 'de';
};

$env parameters

Plack::Middleware::I18N registers the following $env parameters:

plack.i18n

Holds Plack::I18N instance.

plack.i18n.language

Current detected language. A shortcut for $env-{'plack.i18n'}->language>.

plack.i18n.handle

A shortcut for $env-{'plack.i18n'}->handle($env->{'plack.i18n.language'})>.

ISA

Plack::Middleware

METHODS

prepare_app

call($env)

INHERITED METHODS

wrap($app, @args)

OPTIONS

i18n

Plack::I18N instance.

use_path

Whether detect language from URL.

use_session

Whether detect language from session.

use_header

Whether detect language from HTTP_ACCEPT_LANGUAGE.

custom_cb

Whether detect language from a custom callback.

AUTHOR

Viacheslav Tykhanovskyi, <viacheslav.t@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2015, Viacheslav Tykhanovskyi

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.