annotate Lib/IMPL/Resources/StringLocaleMap.pm @ 379:a471e8b77544

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