summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Plugin/I18N2.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/JWebmail/Plugin/I18N2.pm')
-rw-r--r--lib/JWebmail/Plugin/I18N2.pm51
1 files changed, 28 insertions, 23 deletions
diff --git a/lib/JWebmail/Plugin/I18N2.pm b/lib/JWebmail/Plugin/I18N2.pm
index 61f87d6..9c10c8d 100644
--- a/lib/JWebmail/Plugin/I18N2.pm
+++ b/lib/JWebmail/Plugin/I18N2.pm
@@ -63,38 +63,44 @@ package JWebmail::Plugin::I18N2::Translator {
}
-package JWebmail::Plugin::I18N2::Match {
- use Mojo::Base 'Mojolicious::Routes::Match';
+package JWebmail::Plugin::I18N2::Match::Role {
+
+ use Mojo::Base -role;
has '_i18n2_stash';
- sub __add_option_no_override {
+ # cases: ($) (\%) ($, \%) (%) ($, %)
+ sub __i18n2_add_option_no_override {
my $key = shift;
my $value = shift;
- if (ref $_[0] eq 'HASH' && @_ == 1) {
- $_[0]->{$key} ||= $value;
+ if (@_ == 1) { # handles ($) and (\%)
+ $_[0]->{$key} ||= $value if ref $_[0] eq 'HASH';
}
- elsif (ref $_[1] eq 'HASH' && @_ == 2) {
+ elsif (@_ == 2 && ref $_[1] eq 'HASH') { # handles ($, \%)
$_[1]->{$key} ||= $value;
}
- else {
- my ($dom, %args) = @_%2 == 0 ? (undef, @_) : @_;
- $args{$key} ||= $value;
- @_ = ($dom, %args);
- shift @_ unless defined $_[0];
+ elsif (@_ % 2 == 0) { # handles (%)
+ my %opts = @_;
+ $opts{$key} ||= $value;
+ @_ = %opts;
+ }
+ else { # handles ($, %)
+ my ($primary, %opts) = @_;
+ $opts{$key} ||= $value;
+ @_ = ($primary, %opts);
}
return @_;
}
- sub path_for {
+ around 'path_for' => sub {
+ my $orig = shift;
my $self = shift;
- my @args = @_;
if (my $lang = $self->_i18n2_stash->{lang}) {
- @args = __add_option_no_override(lang => $lang, @args);
+ @_ = __i18n2_add_option_no_override(lang => $lang, @_);
}
- return $self->next::method(@args);
- }
+ $orig->($self, @_)
+ };
}
@@ -133,10 +139,10 @@ sub register {
# add translator as helper
$app->helper(l => sub {
my $c = shift;
- my $lang = @_ == 2 ? $_[0] : $c->stash->{lang};
- my $word = @_ == 2 ? $_[1] : $_[0];
+ my $word = shift;
+ my $lang = $c->stash('lang');
- my $res = $t->translate($lang, $word);
+ my $res = $t->translate($lang, $word, @_);
unless ($res) {
local $" = ' ';
$app->log->warn('[' . __PACKAGE__ . "] missing translation for $lang:'$word' @{[ caller(1) ]}[0..2]");
@@ -151,10 +157,9 @@ sub register {
unshift @{ $c->req->url->path->parts }, ''
unless $t->languages($c->req->url->path->parts->[0] || '');
- $c->match(JWebmail::Plugin::I18N2::Match->new(
- root => $c->app->routes,
- _i18n2_stash => $c->stash,
- ));
+ my $ext_match = $c->match->with_roles('JWebmail::Plugin::I18N2::Match::Role');
+ $ext_match->_i18n2_stash($c->stash);
+ $c->match($ext_match);
});
return $app->routes->any('/:lang' => {lang => $defaultLang});