annotate Lib/IMPL/Resources/Strings.pm @ 393:69a1f1508696

minor security refactoring
author cin
date Fri, 14 Feb 2014 16:41:12 +0400
parents 2eed076cb944
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
1 package IMPL::Resources::Strings;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
2 use strict;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
3
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
4 use File::Spec;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
5 use List::Util qw(first);
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
6 use IMPL::require {
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
7 StringMap => 'IMPL::Resources::StringLocaleMap'
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
8 };
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 @Locations;
292
cin
parents: 267
diff changeset
11 my %maps;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
12
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
13 sub import {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
14 my ($self,$refStrings,%options) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
15
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
16 no strict 'refs';
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
17
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
18 my $class = caller;
292
cin
parents: 267
diff changeset
19 my $methods = $options{methods};
245
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') {
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
22 my $map = $self->_GetMapForClass($class,$refStrings);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
23
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
24 while(my ($str,$format) = each %$refStrings) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
25 *{"${class}::$str"} = sub {
292
cin
parents: 267
diff changeset
26 shift if $methods;
245
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
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
29 return $map->GetString($str,$args);
245
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
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
35 sub _GetResourceLocations {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
36 my ($self,$class) = @_;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
37
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
38 my @classNamespace = split /::/,$class;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
39
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
40 my $classShortName = pop @classNamespace;
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
41
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
42 my @paths = map File::Spec->catdir($_,@classNamespace), @Locations;
245
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 # Foo::Bar -> 'Foo/Bar.pm'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
45 my $classModuleName = File::Spec->catfile(@classNamespace,"${classShortName}.pm");
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
46
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
47 # 'Foo/Bar.pm' -> '/full/path/to/Foo/Bar.pm'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
48 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
49
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
50 if ($fullModulePath) {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
51
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
52 # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo/locale/'
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
53 my ($vol,$dir,$file) = File::Spec->splitpath($fullModulePath);
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
54 push @paths, File::Spec->catpath($vol,File::Spec->catdir($dir,'locale'),'');
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
55 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
56
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
57 return \@paths, $classShortName;
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
378
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
60 sub _GetMapForClass {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
61 my ($self,$class,$data) = @_;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
62
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
63 my $map;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
64
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
65 unless ($map) {
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
66
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
67 my ($paths,$name) = $self->_GetResourceLocations($class);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
68
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
69 $map = StringMap->new($data);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
70 $map->name($name);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
71 $map->paths($paths);
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
72
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
73 $maps{$class} = $map;
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
74
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
75 }
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
76
2eed076cb944 rewritten IMPL::Resources::Strings + tests
cin
parents: 337
diff changeset
77 return $map;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
78 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
79
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
80 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
81
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
82 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
83
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
84 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
85
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
86 =head1 NAME
f47f93534005 Documentation
wizard
parents: 50
diff changeset
87
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
88 C<IMPL::Resources::Strings> - Строковые ресурсы
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
89
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
90 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
91
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
92 =begin code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
93
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
94 package Foo;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
95
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
96 use IMPL::Resources::Strings {
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
97 msg_say_hello => "Hello, %name%!",
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
98 msg_module_name => "Simple Foo class"
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 245
diff changeset
99 };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
100
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
101 sub InviteUser {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
102 my ($this,$uname) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
103
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
104 print msg_say_hello(name => $uname);
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 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
107
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
108 =end code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
109
f47f93534005 Documentation
wizard
parents: 50
diff changeset
110 =head1 DESCRIPTION
f47f93534005 Documentation
wizard
parents: 50
diff changeset
111
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
112 Импортирует в целевой модуль функции, которые возвращают локализованные
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
113 параметризованные сообщения.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
114
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
115 При импорте ищутся модули по следующему алгоритму:
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
116
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
117 В каталогах из массива C<@Locations> ищется файл с относительным путем
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
118 C<$Locale/$ModName>, где C<$Locale> - глобальная переменная
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
119 модуля C<IMPL::Resourses::Strings>, а переменная C<$ModName> получена
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
120 путем замены 'C<::>' в имени целевого модуля на 'C</>'.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
121
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
122 Если файл не был найден, то производится поиск в каталоге, где
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
123 расположен сам модуль, файла с относительным путем C<locale/$Locale/$ShortModName>,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
124 где C<$ShortModeName> - последняя часть после 'C<::>' из имени целевого модуля.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
125
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
126 Если файл не найден, то используются строки, указанные при объявлении
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
127 сообщений в целевом модуле.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
128
f47f93534005 Documentation
wizard
parents: 50
diff changeset
129 =head1 FORMAT
f47f93534005 Documentation
wizard
parents: 50
diff changeset
130
f47f93534005 Documentation
wizard
parents: 50
diff changeset
131 =begin code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
132
f47f93534005 Documentation
wizard
parents: 50
diff changeset
133 msg_name = any text with named %params%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
134 msg_hello = hello, %name%!!!
f47f93534005 Documentation
wizard
parents: 50
diff changeset
135 msg_resolve = this is a value of the property: %user.age%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
136
f47f93534005 Documentation
wizard
parents: 50
diff changeset
137 msg_short_err = %error.Message%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
138 msg_full_err = %error%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
139
f47f93534005 Documentation
wizard
parents: 50
diff changeset
140 =end code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
141
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
142 =cut