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.pm50
1 files changed, 35 insertions, 15 deletions
diff --git a/lib/JWebmail/Plugin/I18N2.pm b/lib/JWebmail/Plugin/I18N2.pm
index 8c57ec5..31d6234 100644
--- a/lib/JWebmail/Plugin/I18N2.pm
+++ b/lib/JWebmail/Plugin/I18N2.pm
@@ -25,15 +25,17 @@ package JWebmail::Plugin::I18N2::Maketext {
my @res = grep { /\.pm$/ && -f "$lexica/$_" } readdir $dh;
closedir($dh);
@languages = map { scalar fileparse $_, '.pm' } @res;
+ @languages = map { my ($l, $c) = split '_', $_, 2; $c ? "$l-\U$c" : $l } @languages;
}
+ if (my $dl = $conf->{default_language}) { push @languages, $dl; };
my $self = {};
for (@languages) {
$self->{$_} = JWebmail::I18N->get_handle($_) || die "unable to load language $_";
}
- if (my $dl = $conf->{default_language}) {
- $self->{$dl} = JWebmail::I18N->get_handle($dl) || die "unable to load language $dl";
+ if (my $l = $conf->{log}) {
+ $_->logger($l) for values %$self;
}
return bless $self, $class;
@@ -42,7 +44,7 @@ package JWebmail::Plugin::I18N2::Maketext {
sub languages {
my $self = shift;
if (@_) {
- return exists $self->{$_[0]};
+ return exists $self->{$_[0] || ''};
}
return wantarray ? sort keys $self->%* : scalar keys $self->%*;
}
@@ -53,7 +55,6 @@ package JWebmail::Plugin::I18N2::Maketext {
my $phrase = shift;
return $self->{$lang}->maketext($phrase, @_);
}
-
}
package JWebmail::Plugin::I18N2::Translator {
@@ -66,6 +67,10 @@ package JWebmail::Plugin::I18N2::Translator {
my $cls = shift;
my $conf = @_ == 1 ? shift : {@_};
+ my $self = bless {}, $cls;
+
+ $self->_log($conf->{log} || sub { @_ = $_[0]->() if ref $_[0] eq 'CODE'; local $" = ' '; warn "@_" });
+
my @languages = keys %{$conf->{languages} // {}};
unless (@languages) {
@@ -73,20 +78,19 @@ package JWebmail::Plugin::I18N2::Translator {
}
# load languages
- my $self = {};
for my $l (@languages) {
if (my $dict = __loadi18n($conf->{directory}, $l)) {
$self->{$l} = $dict;
}
}
- return bless $self, $cls;
+ return $self;
}
sub languages {
my $self = shift;
if (@_) {
- return exists $self->{$_[0]};
+ return exists $self->{$_[0] || ''};
}
return wantarray ? sort keys $self->%* : scalar keys $self->%*;
}
@@ -95,7 +99,27 @@ package JWebmail::Plugin::I18N2::Translator {
my $self = shift;
my $lang = shift;
my $word = shift;
- return $self->{$lang}{$word};
+
+ my $res = $self->{$lang}{$word};
+ unless ($res) {
+ local $" = ' ';
+ $self->_log->("missing translation for $lang:'$word' @{[ caller(1) ]}[0..2]");
+ }
+ if (@_) {
+ $res = sprintf($res, @_);
+ }
+ return $res;
+ }
+
+ sub _log {
+ my $self = shift;
+ if (@_) {
+ $self->{_log} = $_[0];
+ return $self;
+ }
+ else {
+ return $self->{_log};
+ }
}
sub __loadi18n {
@@ -172,6 +196,7 @@ sub register {
my $t = $translator->(
default_language => $defaultLang,
directory => $fileLocation,
+ log => sub { $i18n_log->warn(@_) },
%{$conf->{rest} // {}}
);
@@ -199,12 +224,7 @@ sub register {
my $word = shift;
my $lang = $c->stash('lang');
- my $res = $t->translate($lang, $word, @_);
- unless ($res) {
- local $" = ' ';
- $app->log->warn('[' . __PACKAGE__ . "] missing translation for $lang:'$word' @{[ caller(1) ]}[0..2]");
- }
- return $res;
+ return $t->translate($lang, $word, @_);
});
# modify incoming and generated urls
@@ -212,7 +232,7 @@ sub register {
my $c = shift;
unshift @{ $c->req->url->path->parts }, ''
- unless $t->languages($c->req->url->path->parts->[0] || '');
+ unless $t->languages($c->req->url->path->parts->[0]);
my $ext_match = $c->match->with_roles('JWebmail::Plugin::I18N2::Match::Role');
$ext_match->_i18n2_stash($c->stash);