/usr/share/perl5/Mojolicious/Plugin/I18N.pm is in libmojolicious-perl 2.98+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | package Mojolicious::Plugin::I18N;
use Mojo::Base 'Mojolicious::Plugin';
use I18N::LangTags;
use I18N::LangTags::Detect;
use Mojo::Loader;
# "Can we have Bender burgers again?
# No, the cat shelter's onto me."
sub register {
my ($self, $app, $conf) = @_;
# Initialize
my $namespace = $conf->{namespace} || ((ref $app) . "::I18N");
my $default = $conf->{default} || 'en';
die qq{Couldn't initialize I18N class "$namespace": $@}
unless eval "package $namespace; use base 'Locale::Maketext'; 1";
my $dc = "${namespace}::$default";
if (my $e = Mojo::Loader->load($dc)) {
die qq{Couldn't load default lexicon class "$dc": $e} if ref $e;
die qq{Couldn't initialize default lexicon class "$dc": $@}
unless eval
"package $dc; use base '$namespace'; our \%Lexicon = (_AUTO => 1);";
}
# Add hook
$app->hook(
before_dispatch => sub {
my $self = shift;
# Header detection
my @languages = I18N::LangTags::implicate_supers(
I18N::LangTags::Detect->http_accept_langs(
$self->req->headers->accept_language
)
);
# Handler
$self->stash->{i18n}
= Mojolicious::Plugin::I18N::_Handler->new(namespace => $namespace);
# Languages
$self->stash->{i18n}->languages(@languages, $default);
}
);
# Add "languages" helper
$app->helper(languages => sub { shift->stash->{i18n}->languages(@_) });
# Add "l" helper
$app->helper(l => sub { shift->stash->{i18n}->localize(@_) });
}
package Mojolicious::Plugin::I18N::_Handler;
use Mojo::Base -base;
# "Robot 1-X, save my friends! And Zoidberg!"
sub languages {
my ($self, @languages) = @_;
return $self->{language} unless @languages;
# Handle
my $namespace = $self->{namespace};
if (my $handle = $namespace->get_handle(@languages)) {
$handle->fail_with(sub { $_[1] });
$self->{handle} = $handle;
$self->{language} = $handle->language_tag;
}
return $self;
}
sub localize {
my ($self, $key) = (shift, shift);
return $key unless my $handle = $self->{handle};
return $handle->maketext($key, @_);
}
1;
=head1 NAME
Mojolicious::Plugin::I18N - Internationalization 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 Mojo::Base 'MyApp::I18N';
our %Lexicon = (hello => 'hallo');
1;
=head1 DESCRIPTION
L<Mojolicious::Plugin::I18N> adds L<Locale::Maketext> support to
L<Mojolicious>. All you have to do besides using this plugin is to add as many
lexicon classes as you need. Languages can usually be detected automatically
from the C<Accept-Languages> request header.
This plugin can save a lot of typing, since it will generate the following
code by default.
# $self->plugin('I18N');
package MyApp::I18N;
use base 'Locale::Maketext';
package MyApp::I18N::en;
use base 'MyApp::I18N';
our %Lexicon = (_AUTO => 1);
1;
Namespace and default language of generated code are affected by their
respective options. The default lexicon class will only be generated if it
doesn't already exist.
The code of this plugin is a good example for learning to build new plugins,
you're welcome to fork it.
=head1 OPTIONS
L<Mojolicious::Plugin::I18N> supports the following options.
=head2 C<default>
# Mojolicious::Lite
plugin I18N => {default => 'en'};
Default language, defaults to C<en>.
=head2 C<namespace>
# Mojolicious::Lite
plugin I18N => {namespace => 'MyApp::I18N'};
Lexicon namespace, defaults to the application class followed by C<::I18N>.
=head1 HELPERS
L<Mojolicious::Plugin::I18N> implements the following helpers.
=head2 C<l>
%=l 'hello'
$self->l('hello');
Translate sentence.
=head2 C<languages>
% languages 'de';
$self->languages('de');
Change languages.
=head1 METHODS
L<Mojolicious::Plugin::I18N> inherits all methods from L<Mojolicious::Plugin>
and implements the following new ones.
=head2 C<register>
$plugin->register($app, $conf);
Register plugin hooks and helpers in L<Mojolicious> application.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=cut
|