annotate lib/IMPL/Resources/Strings.pm @ 412:30e8c6a74937 ref20150831

working on di container (role based registrations)
author cin
date Mon, 21 Sep 2015 19:54:10 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Resources::Strings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use File::Spec;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use List::Util qw(first);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::require {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 StringMap => 'IMPL::Resources::StringLocaleMap'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 our @Locations;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 my %maps;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 sub import {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 my ($self,$refStrings,%options) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 no strict 'refs';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 my $class = caller;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 my $methods = $options{methods};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21 if (ref $refStrings eq 'HASH') {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 my $map = $self->_GetMapForClass($class,$refStrings);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 while(my ($str,$format) = each %$refStrings) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 *{"${class}::$str"} = sub {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 shift if $methods;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 my $args = @_ == 1 ? shift : { @_ };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 return $map->GetString($str,$args);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35 sub _GetResourceLocations {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 my ($self,$class) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 my @classNamespace = split /::/,$class;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 my $classShortName = pop @classNamespace;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my @paths = map File::Spec->catdir($_,@classNamespace), @Locations;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 # Foo::Bar -> 'Foo/Bar.pm'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 my $classModuleName = File::Spec->catfile(@classNamespace,"${classShortName}.pm");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 # 'Foo/Bar.pm' -> '/full/path/to/Foo/Bar.pm'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 my $fullModulePath = first { -f } map( File::Spec->catfile($_,$classModuleName), @INC );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 if ($fullModulePath) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo/locale/'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 my ($vol,$dir,$file) = File::Spec->splitpath($fullModulePath);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 push @paths, File::Spec->catpath($vol,File::Spec->catdir($dir,'locale'),'');
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 return \@paths, $classShortName;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 sub _GetMapForClass {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 my ($self,$class,$data) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my $map;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 unless ($map) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 my ($paths,$name) = $self->_GetResourceLocations($class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $map = StringMap->new($data);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $map->name($name);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $map->paths($paths);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 $maps{$class} = $map;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 return $map;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88 C<IMPL::Resources::Strings> - Строковые ресурсы
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 =begin code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 package Foo;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 use IMPL::Resources::Strings {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 msg_say_hello => "Hello, %name%!",
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 msg_module_name => "Simple Foo class"
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 sub InviteUser {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 my ($this,$uname) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 print msg_say_hello(name => $uname);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 =end code
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 Импортирует в целевой модуль функции, которые возвращают локализованные
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 параметризованные сообщения.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 При импорте ищутся модули по следующему алгоритму:
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 В каталогах из массива C<@Locations> ищется файл с относительным путем
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 C<$Locale/$ModName>, где C<$Locale> - глобальная переменная
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 модуля C<IMPL::Resourses::Strings>, а переменная C<$ModName> получена
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 путем замены 'C<::>' в имени целевого модуля на 'C</>'.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 Если файл не был найден, то производится поиск в каталоге, где
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 расположен сам модуль, файла с относительным путем C<locale/$Locale/$ShortModName>,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 где C<$ShortModeName> - последняя часть после 'C<::>' из имени целевого модуля.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 Если файл не найден, то используются строки, указанные при объявлении
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127 сообщений в целевом модуле.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 =head1 FORMAT
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 =begin code text
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 msg_name = any text with named %params%
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 msg_hello = hello, %name%!!!
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 msg_resolve = this is a value of the property: %user.age%
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 msg_short_err = %error.Message%
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 msg_full_err = %error%
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 =end code text
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142 =cut