From a52a7d7c27440a7c2716af033a6113abcaa2bd46 Mon Sep 17 00:00:00 2001 From: "Jannis M. Hoffmann" Date: Wed, 16 Nov 2022 23:11:42 +0100 Subject: split I18N2 plugin into multiple files and added translate role --- lib/JWebmail/Plugin/I18N2/Maketext.pm | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/JWebmail/Plugin/I18N2/Maketext.pm (limited to 'lib/JWebmail/Plugin/I18N2/Maketext.pm') diff --git a/lib/JWebmail/Plugin/I18N2/Maketext.pm b/lib/JWebmail/Plugin/I18N2/Maketext.pm new file mode 100644 index 0000000..a38bba7 --- /dev/null +++ b/lib/JWebmail/Plugin/I18N2/Maketext.pm @@ -0,0 +1,55 @@ +package JWebmail::Plugin::I18N2::Maketext; + +use v5.22; +use warnings; +use utf8; + +use JWebmail::I18N; + +use File::Basename 'fileparse'; + +use Role::Tiny::With; +with 'JWebmail::Plugin::I18N2::Role'; + + +sub new { + my $class = shift; + my $conf = @_ == 1 ? shift : {@_}; + + my $lexica = $conf->{directory} || [fileparse(__FILE__)]->[1] . '../I18N'; + + my @languages = keys %{$conf->{languages} // {}}; + + unless (@languages) { + use autodie; + + opendir(my $dh, $lexica); + 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 $l = $conf->{log}) { + $_->logger($l) for values %$self; + } + + return bless $self, $class; +} + +sub loaded_languages { grep { $_ !~ /^_/ } keys $_[0]->%* } + +sub translate { + my $self = shift; + my $lang = shift; + my $phrase = shift; + return $self->{$lang}->maketext($phrase, @_); +} + +1 -- cgit v1.2.3