1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
# Copyright (C) 2008-2010, Sebastian Riedel.
package Mojolicious::Plugin::I18n;
use strict;
use warnings;
use base 'Mojolicious::Plugin';
use I18N::LangTags;
# Core module since Perl 5.8.5
use constant DETECT => eval { require I18N::LangTags::Detect; 1 };
# Can we have Bender burgers again?
# No, the cat shelter’s onto me.
sub register {
my ($self, $app, $conf) = @_;
# Config
$conf ||= {};
# Namespace
my $namespace = $conf->{namespace} || ((ref $app) . "::I18N");
# Initialize
eval "package $namespace; use base 'Locale::Maketext'; 1;";
die qq/Couldn't initialize I18N class "$namespace": $@/ if $@;
# Start timer
$app->plugins->add_hook(
before_dispatch => sub {
my ($self, $c) = @_;
# Languages
my @languages = ('en');
# Header detection
@languages = I18N::LangTags::implicate_supers(
I18N::LangTags::Detect->http_accept_langs(
scalar $c->req->headers->accept_language
)
) if DETECT;
# Handler
$c->stash->{i18n} =
Mojolicious::Plugin::I18n::_Handler->new(
_namespace => $namespace);
# Languages
$c->stash->{i18n}->languages(@languages);
}
);
# Add "languages" helper
$app->renderer->add_helper(
languages => sub { shift->stash->{i18n}->languages(@_) });
# Add "l" helper
$app->renderer->add_helper(l => sub { shift->stash->{i18n}->localize(@_) }
);
}
# Container
package Mojolicious::Plugin::I18n::_Handler;
use base 'Mojo::Base';
__PACKAGE__->attr([qw/_handle _language _namespace/]);
sub languages {
my ($self, @languages) = @_;
# Shortcut
return $self->_language unless @languages;
# Namespace
my $namespace = $self->_namespace;
# Handle
if (my $handle = $namespace->get_handle(@languages)) {
$self->_handle($handle);
$self->_language($handle->language_tag);
}
else { $self->_language('en') }
return $self;
}
sub localize {
my $self = shift;
# Localize
my $handle = $self->_handle;
return $handle->maketext(@_) if $handle;
# Pass through
return join '', @_;
}
1;
__END__
=head1 NAME
Mojolicious::Plugin::I18n - Intenationalization Plugin
=head1 SYNOPSIS
# Mojolicious
$self->plugin('i18n');
% languages 'de';
<%=l 'hello' %>
# Mojolicious::Lite
plugin 'i18n' => {namespace => 'MyApp::I18N'};
<%=l 'hello' %>
# Lexicon
package MyApp::I18N::de;
use base 'MyApp::I18N';
our %Lexicon = (hello => 'hallo');
1;
=head1 DESCRIPTION
L<Mojolicous::Plugin::I18n> adds L<Locale::Maketext> support to
L<Mojolicious>.
=head1 METHODS
L<Mojolicious::Plugin::I18n> inherits all methods from L<Mojolicious::Plugin>
and implements the following new ones.
=head2 C<register>
$plugin->register;
Register plugin hooks and helpers in L<Mojolicious> application.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.
=cut
|