blob: 29364bbdc2135d8b38ab5e26846d3fe3dba21f8f (
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
package JWebmail::Plugin::I18N2::INI;
use v5.22;
use warnings;
use utf8;
use Mojo::File;
use Config::Tiny;
use Role::Tiny::With;
with 'JWebmail::Plugin::I18N2::Role';
sub new {
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) {
@languages = map { s|^.*/(..)\.lang$|$1|r } glob("'$conf->{directory}/*.lang'");
}
# load languages
for my $l (@languages) {
if (my $dict = __loadi18n($conf->{directory}, $l)) {
$self->{$l} = $dict;
}
}
return $self;
}
sub loaded_languages { grep { $_ !~ /^_/ } keys $_[0]->%* }
sub translate {
my $self = shift;
my $lang = shift;
my $word = shift;
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 {
my $langsubdir = shift;
my $lang = shift;
my $langFile = "$langsubdir/$lang.lang";
my $TXT;
if ( -f $langFile ) {
$TXT = Config::Tiny->read($langFile, 'utf8')->{'_'};
if ($@) {
die "error reading file $langFile: $@";
}
}
return $TXT;
}
1
|