annotate Lib/IMPL/Resources/StringLocaleMap.pm @ 380:1eca08048ba9

TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
author cin
date Fri, 17 Jan 2014 15:58:57 +0400
parents a471e8b77544
children 99ac2e19c0cc
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
380
1eca08048ba9 TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents: 379
diff changeset
40 warn "id: $id,\t\tlocale: $locale";
1eca08048ba9 TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents: 379
diff changeset
41
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
42 if(not $map = $this->_maps->{$locale}) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
43 $map = $this->LoadMap($locale,$this->_maps->{default});
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
44 if (is($map,StringMap)) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
45 #nop
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
46 } elsif (ref($map) eq 'HASH') {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
47 $map = StringMap->new($map,$this->_maps->{default});
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
48 } elsif( not $map ) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
49 $map = $this->_maps->{default};
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
50 } else {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
51 die Exception->new("ResolveLocale returned unexpected data", $map);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
52 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
53
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
54 $this->_maps->{$locale} = $map;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
55 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
56
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
57 return $map->GetString($id,$args);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
58 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
59
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
60 sub LoadMap {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
61 my ($this,$locale,$default) = @_;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
62
379
cin
parents: 378
diff changeset
63 my @spec = split /_/, $locale;
cin
parents: 378
diff changeset
64
cin
parents: 378
diff changeset
65 my @locales;
cin
parents: 378
diff changeset
66
cin
parents: 378
diff changeset
67 do {
cin
parents: 378
diff changeset
68 push @locales, join('_', @spec);
cin
parents: 378
diff changeset
69 } while(pop @spec);
cin
parents: 378
diff changeset
70
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
71 my $file = first { -f } map {
379
cin
parents: 378
diff changeset
72 my $path = $_;
cin
parents: 378
diff changeset
73
cin
parents: 378
diff changeset
74 map {
cin
parents: 378
diff changeset
75 my $name = FS->catfile($path,$_,$this->name);
cin
parents: 378
diff changeset
76 ("$name.s", "$name.p");
cin
parents: 378
diff changeset
77 } @locales;
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
78 } $this->paths;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
79
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
80 if($file) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
81 if ($file =~ /\.s$/) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
82 return $this->LoadStringMap($file);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
83 } else {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
84 return $this->LoadPerlMap($file,$default);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
85 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
86 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
87
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
88 return;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
89 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
90
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
91 sub LoadPerlMap {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
92 my ($self,$file,$parent) = @_;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
93
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
94 my $data = do $file;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
95 my $e = $@;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
96 die Exception->new("Failed to load file '$file'", $e) if $e;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
97 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
98 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
99
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
100 return StringMap->new($data,$parent);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
101 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
102
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
103 sub LoadStringMap {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
104 my ($this,$fname) = @_;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
105
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
106 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
107 local $_;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
108 my %map;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
109 my $line = 1;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
110 while (<$hRes>) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
111 chomp;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
112 $line ++ and next if /^\s*$/;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
113
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
114 if (/^(\w+)\s*=\s*(.*)$/) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
115 $map{$1} = $2;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
116 } else {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
117 die "Invalid resource format in $fname at $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 $line ++;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
120 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
121
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
122 return \%map;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 377
diff changeset
123 }
377
cin
parents:
diff changeset
124
cin
parents:
diff changeset
125 1;