summaryrefslogtreecommitdiff
path: root/lib/JWebmail/Plugin/I18N2/Maketext.pm
blob: a38bba732d0b8540ccb02058030d2810a2f33329 (plain)
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
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