/usr/share/perl5/Mojolicious/Plugin/I18N.pm is in libmojolicious-plugin-i18n-perl 1.60-1.
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | package Mojolicious::Plugin::I18N;
use Mojo::Base 'Mojolicious::Plugin';
use Mojo::URL;
use I18N::LangTags;
use I18N::LangTags::Detect;
our $VERSION = '1.6';
# "Can we have Bender burgers again?
# No, the cat shelter’s onto me."
sub register {
my ($plugin, $app, $conf) = @_;
# Initialize
my $namespace = $conf->{namespace} || ( (ref $app) . '::I18N' );
my $default = $conf->{default } || 'en';
$default =~ tr/-A-Z/_a-z/;
$default =~ tr/_a-z0-9//cd;
my $langs = $conf->{support_url_langs};
my $hosts = $conf->{support_hosts };
# Default Handler
my $handler = sub {
shift->stash->{i18n} =
Mojolicious::Plugin::I18N::_Handler->new(namespace => $namespace, default => $default)
;
};
# Add hook
$app->hook(
before_dispatch => sub {
my $self = shift;
# Handler
$handler->( $self );
# Header detection
my @languages = $conf->{no_header_detect}
? ()
: I18N::LangTags::implicate_supers(
I18N::LangTags::Detect->http_accept_langs(
$self->req->headers->accept_language
)
)
;
# Host detection
my $host = $self->req->headers->header('X-Host') || $self->req->headers->host;
if ($conf->{support_hosts} && $host) {
warn $host;
$host =~ s/^www\.//; # hack
if (my $lang = $conf->{support_hosts}->{ $host }) {
$self->app->log->debug("Found language $lang, Host header is $host");
unshift @languages, $lang;
}
}
# Set default language
$self->stash(lang_default => $languages[0]) if $languages[0];
# URL detection
if (my $path = $self->req->url->path) {
my $part = $path->parts->[0];
if ($part && $langs && grep { $part eq $_ } @$langs) {
# Ignore static files
return if $self->res->code;
$self->app->log->debug("Found language $part in URL $path");
unshift @languages, $part;
# Save lang in stash
$self->stash(lang => $part);
# Clean path
shift @{$path->parts};
$path->trailing_slash(0);
}
}
# Languages
$self->languages(@languages, $default);
}
);
# Add "languages" helper
$app->helper(languages => sub {
my $self = shift;
$handler->( $self ) unless $self->stash('i18n');
$self->stash->{i18n}->languages(@_);
});
# Add "l" helper
$app->helper(l => sub {
my $self = shift;
$handler->( $self ) unless $self->stash('i18n');
$self->stash->{i18n}->localize(@_);
});
# Reimplement "url_for" helper
my $mojo_url_for = *Mojolicious::Controller::url_for{CODE};
my $i18n_url_for = sub {
my $self = shift;
my $url = $self->$mojo_url_for(@_);
# Absolute URL
return $url if $url->is_abs;
# Discard target if present
shift if (@_ % 2 && !ref $_[0]) || (@_ > 1 && ref $_[-1]);
# Unveil params
my %params = @_ == 1 ? %{$_[0]} : @_;
# Detect lang
if (my $lang = $params{lang} || $self->stash('lang')) {
my $path = $url->path || [];
# Root
if (!$path->[0]) {
$path->parts([ $lang ]);
}
# No language detected
elsif ( ref $langs ne 'ARRAY' or not scalar grep { $path->contains("/$_") } @$langs ) {
unshift @{ $path->parts }, $lang;
}
}
$url;
};
{
no strict 'refs';
no warnings 'redefine';
*Mojolicious::Controller::url_for = $i18n_url_for;
}
}
package Mojolicious::Plugin::I18N::_Handler;
use Mojo::Base -base;
use constant DEBUG => $ENV{MOJO_I18N_DEBUG} || 0;
# "Robot 1-X, save my friends! And Zoidberg!"
sub languages {
my ($self, @languages) = @_;
unless (@languages) {
my $lang = $self->{language};
# lang such as en-us
$lang =~ s/_/-/g;
return $lang;
}
# Handle
my $namespace = $self->{namespace};
# Load Lang Module
$self->_load_module($namespace => $_) for @languages;
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 = shift;
my $key = shift;
return $key unless my $handle = $self->{handle};
return $handle->maketext($key, @_);
}
sub _load_module {
my $self = shift;
my($namespace, $lang) = @_;
return unless $namespace && $lang;
# lang such as en-us
$lang =~ s/-/_/g;
unless ($namespace->can('new')) {
DEBUG && warn("Load default namespace $namespace");
(my $file = $namespace) =~ s{::|'}{/}g;
eval qq(require "$file.pm");
if ($@) {
DEBUG && warn("Create default namespace $namespace");
eval "package $namespace; use base 'Locale::Maketext'; 1;";
die qq/Couldn't initialize I18N default class "$namespace": $@/ if $@;
}
}
for ($self->{default}, $lang) {
my $module = "${namespace}::$_";
unless ($module->can('new')) {
DEBUG && warn("Load the I18N class $module");
(my $file = $module) =~ s{::|'}{/}g;
eval qq(require "$file.pm");
my $default = $self->{default};
if ($@ || not eval "\%${module}::Lexicon") {
if ($_ eq $default) {
DEBUG && warn("Create the I18N class $module");
eval "package ${module}; use base '$namespace';" . 'our %Lexicon = (_AUTO => 1); 1;';
die qq/Couldn't initialize I18N class "$namespace": $@/ if $@;
}
}
}
}
}
1;
__END__
=head1 NAME
Mojolicious::Plugin::I18N - Internationalization Plugin for Mojolicious
=head1 SYNOPSIS
# Mojolicious
$self->plugin('I18N');
% languages 'de';
%=l 'hello'
# Mojolicious::Lite (detect language from URL, i.e. /en/ or /de/)
plugin I18N => {namespace => 'MyApp::I18N', support_url_langs => [qw(en de)]};
%=l 'hello'
# Lexicon
package MyApp::I18N::de;
use Mojo::Base 'MyApp::I18N';
our %Lexicon = (hello => 'hallo');
1;
=head1 DESCRIPTION
L<Mojolicious::Plugin::I18N> is internationalization plugin for Mojolicious
It works with Mojolicious 4.0+.
Old namespace is L<Mojolicious::Plugin::I18N2>.
=head1 OPTIONS
L<Mojolicious::Plugin::I18N> supports the following options.
=head2 C<support_url_langs>
plugin I18N => {support_url_langs => [qw(en de)]};
Detect language from URL.
=head2 C<support_hosts>
plugin I18N => {support_hosts => { 'mojolicious.ru' => 'ru', 'mojolicio.us' => 'en' }};
Detect Host header and use language for that host.
=head2 C<no_header_detect>
plugin I18N => {no_header_detect => 1};
Off header detect.
=head2 C<default>
plugin I18N => {default => 'en'};
Default language for i18n, defaults to C<en>.
=head2 C<namespace>
plugin I18N => {namespace => 'MyApp::I18N'};
Lexicon namespace, defaults to the application class followed by C<::I18N>.
=head1 HELPERS
L<Mojolicious::Plugin::I18N> implements helpers same as L<Mojolicious::Plugin::I18N>.
=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::I18N>
and reimplements the following new ones.
=head2 C<register>
$plugin->register;
Register plugin hooks and helpers in L<Mojolicious> application.
=head1 DEBUG MODE
L<Mojolicious::Plugin::I18N> has debug mode.
# debug mode on
BEGIN { $ENV{MOJO_I18N_DEBUG} = 1 };
# or
MOJO_I18N_DEBUG=1 perl script.pl
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
=head1 AUTHORS
2011-2014 Anatoly Sharifulin <sharifulin@gmail.com>
2010-2012 Sebastian Riedel <kraihx@googlemail.com>
=head1 BUGS
Please report any bugs or feature requests to C<bug-mojolicious-plugin-i18n at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.htMail?Queue=Mojolicious-Plugin-I18N>. We will be notified, and then you'll
automatically be notified of progress on your bug as we make changes.
=over 5
=item * Github
L<http://github.com/sharifulin/mojolicious-plugin-i18n/tree/master>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.htMail?Dist=Mojolicious-Plugin-I18N>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Mojolicious-Plugin-I18N>
=item * CPANTS: CPAN Testing Service
L<http://cpants.perl.org/dist/overview/Mojolicious-Plugin-I18N>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Mojolicious-Plugin-I18N>
=item * Search CPAN
L<http://search.cpan.org/dist/Mojolicious-Plugin-I18N>
=back
=head1 COPYRIGHT & LICENSE
Copyright (C) 2011-2014 by Anatoly Sharifulin.
Copyright (C) 2008-2012, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
=cut
|