377
|
1 package IMPL::Resources::StringLocaleMap;
|
|
2 use strict;
|
|
3
|
378
|
4 use List::Util qw(first);
|
|
5 use IMPL::lang qw(:base);
|
|
6 use IMPL::Const qw(:prop);
|
|
7 use IMPL::declare {
|
|
8 require => {
|
|
9 Resources => 'IMPL::Resources',
|
|
10 StringMap => 'IMPL::Resources::StringMap',
|
|
11 Exception => 'IMPL::Exception',
|
|
12 FS => 'File::Spec'
|
|
13 },
|
|
14 base => {
|
|
15 'IMPL::Object' => '@_'
|
|
16 },
|
|
17 props => [
|
|
18 _maps => PROP_RW,
|
|
19 name => PROP_RW,
|
|
20 paths => PROP_RW | PROP_LIST
|
|
21 ]
|
|
22 };
|
377
|
23
|
378
|
24 sub CTOR {
|
|
25 my ($this,$data,$parent) = @_;
|
|
26
|
|
27 if (is($data, StringMap)) {
|
|
28 $this->_maps({ default => $data });
|
|
29 } elsif ( ref($data) eq 'HASH' ) {
|
|
30 $this->_maps({ default => StringMap->new($data,$parent)});
|
|
31 }
|
|
32 }
|
|
33
|
|
34 sub GetString {
|
|
35 my ($this,$id,$args) = @_;
|
|
36
|
|
37 my $locale = Resources->currentLocale || 'default';
|
|
38 my $map;
|
|
39
|
|
40 if(not $map = $this->_maps->{$locale}) {
|
|
41 $map = $this->LoadMap($locale,$this->_maps->{default});
|
|
42 if (is($map,StringMap)) {
|
|
43 #nop
|
|
44 } elsif (ref($map) eq 'HASH') {
|
|
45 $map = StringMap->new($map,$this->_maps->{default});
|
|
46 } elsif( not $map ) {
|
|
47 $map = $this->_maps->{default};
|
|
48 } else {
|
|
49 die Exception->new("ResolveLocale returned unexpected data", $map);
|
|
50 }
|
|
51
|
|
52 $this->_maps->{$locale} = $map;
|
|
53 }
|
|
54
|
|
55 return $map->GetString($id,$args);
|
|
56 }
|
|
57
|
|
58 sub LoadMap {
|
|
59 my ($this,$locale,$default) = @_;
|
|
60
|
379
|
61 my @spec = split /_/, $locale;
|
|
62
|
|
63 my @locales;
|
|
64
|
|
65 do {
|
|
66 push @locales, join('_', @spec);
|
|
67 } while(pop @spec);
|
|
68
|
378
|
69 my $file = first { -f } map {
|
379
|
70 my $path = $_;
|
|
71
|
|
72 map {
|
|
73 my $name = FS->catfile($path,$_,$this->name);
|
|
74 ("$name.s", "$name.p");
|
|
75 } @locales;
|
378
|
76 } $this->paths;
|
|
77
|
|
78 if($file) {
|
|
79 if ($file =~ /\.s$/) {
|
|
80 return $this->LoadStringMap($file);
|
|
81 } else {
|
|
82 return $this->LoadPerlMap($file,$default);
|
|
83 }
|
|
84 }
|
|
85
|
|
86 return;
|
|
87 }
|
|
88
|
|
89 sub LoadPerlMap {
|
|
90 my ($self,$file,$parent) = @_;
|
|
91
|
|
92 my $data = do $file;
|
|
93 my $e = $@;
|
|
94 die Exception->new("Failed to load file '$file'", $e) if $e;
|
|
95 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!;
|
|
96 die Exception->new("Failed to load file '$file'", "A hash data is expected") unless ref($data) eq 'HASH';
|
|
97
|
|
98 return StringMap->new($data,$parent);
|
|
99 }
|
|
100
|
|
101 sub LoadStringMap {
|
|
102 my ($this,$fname) = @_;
|
|
103
|
|
104 open my $hRes, "<:encoding(utf-8)", $fname or die "Failed to open file $fname: $!";
|
|
105 local $_;
|
|
106 my %map;
|
|
107 my $line = 1;
|
|
108 while (<$hRes>) {
|
|
109 chomp;
|
|
110 $line ++ and next if /^\s*$/;
|
|
111
|
|
112 if (/^(\w+)\s*=\s*(.*)$/) {
|
|
113 $map{$1} = $2;
|
|
114 } else {
|
|
115 die "Invalid resource format in $fname at $line";
|
|
116 }
|
|
117 $line ++;
|
|
118 }
|
|
119
|
|
120 return \%map;
|
|
121 }
|
377
|
122
|
|
123 1; |