Mercurial > pub > Impl
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 |
| rev | line source |
|---|---|
| 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 | |
|
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 | 42 if(not $map = $this->_maps->{$locale}) { |
| 43 $map = $this->LoadMap($locale,$this->_maps->{default}); | |
| 44 if (is($map,StringMap)) { | |
| 45 #nop | |
| 46 } elsif (ref($map) eq 'HASH') { | |
| 47 $map = StringMap->new($map,$this->_maps->{default}); | |
| 48 } elsif( not $map ) { | |
| 49 $map = $this->_maps->{default}; | |
| 50 } else { | |
| 51 die Exception->new("ResolveLocale returned unexpected data", $map); | |
| 52 } | |
| 53 | |
| 54 $this->_maps->{$locale} = $map; | |
| 55 } | |
| 56 | |
| 57 return $map->GetString($id,$args); | |
| 58 } | |
| 59 | |
| 60 sub LoadMap { | |
| 61 my ($this,$locale,$default) = @_; | |
| 62 | |
| 379 | 63 my @spec = split /_/, $locale; |
| 64 | |
| 65 my @locales; | |
| 66 | |
| 67 do { | |
| 68 push @locales, join('_', @spec); | |
| 69 } while(pop @spec); | |
| 70 | |
| 378 | 71 my $file = first { -f } map { |
| 379 | 72 my $path = $_; |
| 73 | |
| 74 map { | |
| 75 my $name = FS->catfile($path,$_,$this->name); | |
| 76 ("$name.s", "$name.p"); | |
| 77 } @locales; | |
| 378 | 78 } $this->paths; |
| 79 | |
| 80 if($file) { | |
| 81 if ($file =~ /\.s$/) { | |
| 82 return $this->LoadStringMap($file); | |
| 83 } else { | |
| 84 return $this->LoadPerlMap($file,$default); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 sub LoadPerlMap { | |
| 92 my ($self,$file,$parent) = @_; | |
| 93 | |
| 94 my $data = do $file; | |
| 95 my $e = $@; | |
| 96 die Exception->new("Failed to load file '$file'", $e) if $e; | |
| 97 die IOException->new("Failed to load file '$file'", $!) if not defined $data and $!; | |
| 98 die Exception->new("Failed to load file '$file'", "A hash data is expected") unless ref($data) eq 'HASH'; | |
| 99 | |
| 100 return StringMap->new($data,$parent); | |
| 101 } | |
| 102 | |
| 103 sub LoadStringMap { | |
| 104 my ($this,$fname) = @_; | |
| 105 | |
| 106 open my $hRes, "<:encoding(utf-8)", $fname or die "Failed to open file $fname: $!"; | |
| 107 local $_; | |
| 108 my %map; | |
| 109 my $line = 1; | |
| 110 while (<$hRes>) { | |
| 111 chomp; | |
| 112 $line ++ and next if /^\s*$/; | |
| 113 | |
| 114 if (/^(\w+)\s*=\s*(.*)$/) { | |
| 115 $map{$1} = $2; | |
| 116 } else { | |
| 117 die "Invalid resource format in $fname at $line"; | |
| 118 } | |
| 119 $line ++; | |
| 120 } | |
| 121 | |
| 122 return \%map; | |
| 123 } | |
| 377 | 124 |
| 125 1; |
