NAME
Mojolicious::Plugin::Localize::Locale - Localize Based on Requested Locales
SYNOPSIS
# Register plugin with a dictionary in Mojolicious::Lite
plugin Localize => {
dict => {
welcome => {
'_' => sub { $_->locale },
-en => 'Welcome!',
de => 'Willkommen!',
fr => 'Bonjour!'
}
}
};
# Optionally create language depending routes
under '/:lang' => { lang => '' } => sub {
my $c = shift;
# Prefer the chosen language
$c->localize->locale($c->stash('lang')) if $c->stash('lang');
return 1;
};
# Set language depending routes
get '/' => sub {
shift->render('<%= loc "welcome" %>');
};
DESCRIPTION
Mojolicious::Plugin::Localize::Locale detects preferred languages of a user agent's request to be used as preferred keys in dictionaries for Mojolicious::Plugin::Localize.
METHODS
Mojolicious::Plugin::Localize::Locale inherits all methods from Mojolicious::Plugin and implements the following new ones.
register
# Mojolicious
$mojo->plugin('Localize::Locale');
# Mojolicious::Lite
plugin 'Localize::Locale';
Called when registering the plugin. The plugin is registered by Mojolicious::Plugin::Localize by default.
HELPERS
localize->locale
# Return the requested languages
my $lang = $c->localize->locale;
# $lang = ['en-us', 'en']
# Set a preferred language
$lang = $c->localize->locale('de-DE');
# $lang = ['de-de', 'de', 'en-us', 'en']
Returns an array reference of locales the user preferred based on the request headers. If language notations following RFC 3066 are passed, these will be preferred over detected languages (e.g. based on the URL path, TLD, GeoIP, or user preferences coming from a database).
All short names will be lower cased and specific languages will be followed by the short name of their super languages.
AVAILABILITY
https://github.com/Akron/Mojolicious-Plugin-Localize
COPYRIGHT AND LICENSE
Copyright (C) 2014-2017, Nils Diewald.
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.