Mercurial > pub > Impl
comparison Lib/IMPL/Resources/StringLocaleMap.pm @ 378:2eed076cb944
rewritten IMPL::Resources::Strings + tests
author | cin |
---|---|
date | Wed, 15 Jan 2014 17:20:54 +0400 |
parents | a0d342ac9a36 |
children | a471e8b77544 |
comparison
equal
deleted
inserted
replaced
377:a0d342ac9a36 | 378:2eed076cb944 |
---|---|
1 package IMPL::Resources::StringLocaleMap; | 1 package IMPL::Resources::StringLocaleMap; |
2 use strict; | 2 use strict; |
3 | 3 |
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 }; | |
4 | 23 |
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 | |
61 my $file = first { -f } map { | |
62 my $name = FS->catfile($_,$locale,$this->name); | |
63 ("$name.s", "$name.p"); | |
64 } $this->paths; | |
65 | |
66 if($file) { | |
67 if ($file =~ /\.s$/) { | |
68 return $this->LoadStringMap($file); | |
69 } else { | |
70 return $this->LoadPerlMap($file,$default); | |
71 } | |
72 } | |
73 | |
74 return; | |
75 } | |
76 | |
77 sub LoadPerlMap { | |
78 my ($self,$file,$parent) = @_; | |
79 | |
80 my $data = do $file; | |
81 my $e = $@; | |
82 die Exception->new("Failed to load file '$file'", $e) if $e; | |
83 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!; | |
84 die Exception->new("Failed to load file '$file'", "A hash data is expected") unless ref($data) eq 'HASH'; | |
85 | |
86 return StringMap->new($data,$parent); | |
87 } | |
88 | |
89 sub LoadStringMap { | |
90 my ($this,$fname) = @_; | |
91 | |
92 open my $hRes, "<:encoding(utf-8)", $fname or die "Failed to open file $fname: $!"; | |
93 local $_; | |
94 my %map; | |
95 my $line = 1; | |
96 while (<$hRes>) { | |
97 chomp; | |
98 $line ++ and next if /^\s*$/; | |
99 | |
100 if (/^(\w+)\s*=\s*(.*)$/) { | |
101 $map{$1} = $2; | |
102 } else { | |
103 die "Invalid resource format in $fname at $line"; | |
104 } | |
105 $line ++; | |
106 } | |
107 | |
108 return \%map; | |
109 } | |
5 | 110 |
6 1; | 111 1; |