summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis.hoffmann@rwth-aachen.de>2022-05-03 20:10:48 +0200
committerJannis M. Hoffmann <jannis.hoffmann@rwth-aachen.de>2022-05-03 20:10:48 +0200
commit9288d79ea1cfe2e9b2c9318f6f8cefe922bf1652 (patch)
tree48d3f2e389d2d14d6b3234c99bfd9ecfa11a1746
parent0afc24fdc102776e5e1ac12c25f0e145932e1166 (diff)
extend matcher by adding role dynamically
-rw-r--r--CHANGES.md2
-rw-r--r--lib/JWebmail/Plugin/I18N2.pm51
2 files changed, 29 insertions, 24 deletions
diff --git a/CHANGES.md b/CHANGES.md
index 23383f7..54f53d0 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -137,7 +137,7 @@ Current v1.1.0
- [ ] improve i18n
- [ ] add localization of dates and time
- [x] refactor I18N plugin to allow independent translate provider
- - [ ] extend matcher dynamic with a role
+ - [x] extend matcher dynamic with a role
- [ ] fix tests
- [ ] moving mails to other folders
- [ ] creating new folders
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});