annotate Lib/IMPL/Resources/Strings.pm @ 250:129e48bb5afb

DOM refactoring ObjectToDOM methods are virtual QueryToDOM uses inflators Fixed transform for the complex values in the ObjectToDOM QueryToDOM doesn't allow to use complex values (HASHes) as values for nodes (overpost problem)
author sergey
date Wed, 07 Nov 2012 04:17:53 +0400
parents 7c517134c42f
children bbc0da7ef90e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
1 use strict;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
2 use warnings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
3
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
4 package IMPL::Resources::Strings;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
5
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
6 use File::Spec;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
7 use List::Util qw(first);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
8 use IMPL::Resources::Format qw(FormatMessage);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
9
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
10 our $Locale ||= 'default';
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
11 our $Encoding ||= 'utf-8';
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
12 our @Locations;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
13
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
14 sub import {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
15 my ($self,$refStrings,%options) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
16
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
17 no strict 'refs';
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
18
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
19 my $class = caller;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
20
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
21 if (ref $refStrings eq 'HASH') {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
22 my %map;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
23 while(my ($name,$format) = each %$refStrings) {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
24 $map{default}{$name} = $format;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
25
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
26 *{"${class}::$name"} = sub {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
27 my $args = @_ == 1 ? shift : { @_ };
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
28
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
29 return _FormatMapMessage($class,$name,\%map,$Locale,$args);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
30 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
31 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
32 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
33 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
34
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
35 sub _FormatMapMessage {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
36 my ($class,$msg,$map,$locale,$args) = @_;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
37
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
38 if (not exists $map->{$locale} ) {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
39 $map->{$locale} = LoadStrings($class,$locale);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
40 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
41
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
42 return FormatMessage( ($map->{$locale} || $map->{default})->{$msg}, $args );
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
43 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
44
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
45 sub LoadStrings {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
46 my ($class,$locale) = @_;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
47
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
48 # Foo::Bar -> ('Foo','Bar')
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
49 my @classNamespace = split /::/,$class;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
50
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
51 my $classShortName = pop @classNamespace;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
52
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
53 # Foo::Bar -> 'Foo/Bar.pm'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
54 my $classModuleName = File::Spec->catfile(@classNamespace,"${classShortName}.pm");
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
55
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
56 # 'Foo/Bar.pm' -> '/full/path/to/Foo/Bar.pm'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
57 my $fullModulePath = first { -f } map( File::Spec->catfile($_,$classModuleName), @INC );
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
58
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
59 my @ways = map {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
60 my @path = ($_);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
61 push @path,$Locale;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
62
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
63 File::Spec->catfile($_,$Locale,@classNamespace,$classShortName);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
64 } @Locations;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
65
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
66
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
67 if ($fullModulePath) {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
68
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
69 # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
70 my ($vol,$dir,$file) = File::Spec->splitpath($fullModulePath);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
71 my $baseDir = File::Spec->catpath($vol,$dir,'');
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
72
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
73 # '/full/path/to/Foo' -> '/full/path/to/Foo/locale/En_US/Bar'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
74 push @ways, File::Spec->catfile($baseDir,'locale',$Locale,$classShortName);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
75 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
76
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
77 my $mapFile = first { -f } @ways;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
78
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
79 return unless $mapFile;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
80
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
81 return ParseStringsMap($mapFile);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
82 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
83
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
84 sub ParseStringsMap {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
85 my ($fname) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
86
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
87 open my $hRes, "<:encoding($Encoding)", findFile($fname) or die "Failed to open file $fname: $!";
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
88
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
89 my %Map;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
90 my $line = 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
91 while (<$hRes>) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
92 chomp;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
93 $line ++ and next if /^\s*$/;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
94
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
95 if (/^(\w+)\s*=\s*(.*)$/) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
96 $Map{$1} = $2;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
97 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
98 die "Invalid resource format in $fname at $line";
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
99 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
100 $line ++;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
101 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
102
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
103 return \%Map;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
104 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
105
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
106 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
107
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
108 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
109
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
110 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
111
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
112 =head1 NAME
f47f93534005 Documentation
wizard
parents: 50
diff changeset
113
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
114 C<IMPL::Resources::Strings> - Строковые ресурсы
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
115
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
116 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
117
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
118 =begin code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
119
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
120 package Foo;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
121
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
122 use IMPL::Resources::Strings {
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
123 msg_say_hello => "Hello, %name%!",
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
124 msg_module_name => "Simple Foo class"
211
2b9b55cfb79b Completed IMPL::Web::AutoLocator, added tests
cin
parents: 180
diff changeset
125 }, auto => 1, locale => 'en-US';
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
126
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
127 sub InviteUser {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
128 my ($this,$uname) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
129
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
130 print msg_say_hello(name => $uname);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
131
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
132 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
133
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
134 =end code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
135
f47f93534005 Documentation
wizard
parents: 50
diff changeset
136 =head1 DESCRIPTION
f47f93534005 Documentation
wizard
parents: 50
diff changeset
137
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
138 Импортирует в целевой модуль функции, которые возвращают локализованные
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
139 параметризованные сообщения.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
140
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
141 При импорте ищутся модули по следующему алгоритму:
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
142
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
143 В каталогах из массива C<@Locations> ищется файл с относительным путем
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
144 C<$Locale/$ModName>, где C<$Locale> - глобальная переменная
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
145 модуля C<IMPL::Resourses::Strings>, а переменная C<$ModName> получена
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
146 путем замены 'C<::>' в имени целевого модуля на 'C</>'.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
147
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
148 Если файл не был найден, то производится поиск в каталоге, где
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
149 расположен сам модуль, файла с относительным путем C<locale/$Locale/$ShortModName>,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
150 где C<$ShortModeName> - последняя часть после 'C<::>' из имени целевого модуля.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
151
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
152 Если файл не найден, то используются строки, указанные при объявлении
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
153 сообщений в целевом модуле.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
154
f47f93534005 Documentation
wizard
parents: 50
diff changeset
155 =head1 FORMAT
f47f93534005 Documentation
wizard
parents: 50
diff changeset
156
f47f93534005 Documentation
wizard
parents: 50
diff changeset
157 =begin code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
158
f47f93534005 Documentation
wizard
parents: 50
diff changeset
159 msg_name = any text with named %params%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
160 msg_hello = hello, %name%!!!
f47f93534005 Documentation
wizard
parents: 50
diff changeset
161 msg_resolve = this is a value of the property: %user.age%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
162
f47f93534005 Documentation
wizard
parents: 50
diff changeset
163 msg_short_err = %error.Message%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
164 msg_full_err = %error%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
165
f47f93534005 Documentation
wizard
parents: 50
diff changeset
166 =end code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
167
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
168 =cut