annotate Lib/IMPL/Resources/Strings.pm @ 360:39842eedd923

language detection from request
author sergey
date Tue, 26 Nov 2013 03:22:44 +0400
parents f4e14f32cf54
children 2eed076cb944
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);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
6 use IMPL::Resources::Format qw(FormatMessage);
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
7 use IMPL::require {
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
8 Resources => 'IMPL::Resources'
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
9 };
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
10
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;
292
cin
parents: 267
diff changeset
13 my %maps;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
14
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
15 sub import {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
16 my ($self,$refStrings,%options) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
17
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
18 no strict 'refs';
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
19
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
20 my $class = caller;
292
cin
parents: 267
diff changeset
21 my $methods = $options{methods};
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
22
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
23 if (ref $refStrings eq 'HASH') {
292
cin
parents: 267
diff changeset
24 my $map = ( $maps{$class} ||= {} );
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
25 while(my ($name,$format) = each %$refStrings) {
292
cin
parents: 267
diff changeset
26 $map->{default}{$name} = $format;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
27
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
28 *{"${class}::$name"} = sub {
292
cin
parents: 267
diff changeset
29 shift if $methods;
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
30 my $args = @_ == 1 ? shift : { @_ };
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
31
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
32 return _FormatMapMessage($class,$name,$map,Resources->currentLocale,$args);
245
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 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
36 }
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
37
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
38 sub _FormatMapMessage {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
39 my ($class,$msg,$map,$locale,$args) = @_;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
40
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
41 if (not exists $map->{$locale} ) {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
42 $map->{$locale} = LoadStrings($class,$locale);
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 return FormatMessage( ($map->{$locale} || $map->{default})->{$msg}, $args );
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
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
48 sub LoadStrings {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
49 my ($class,$locale) = @_;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
50
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
51 # Foo::Bar -> ('Foo','Bar')
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
52 my @classNamespace = split /::/,$class;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
53
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
54 my $classShortName = pop @classNamespace;
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 -> 'Foo/Bar.pm'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
57 my $classModuleName = File::Spec->catfile(@classNamespace,"${classShortName}.pm");
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
58
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
59 # 'Foo/Bar.pm' -> '/full/path/to/Foo/Bar.pm'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
60 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
61
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
62 my @ways = map {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
63 my @path = ($_);
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
64 push @path,Resources->currentLocale;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
65
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
66 File::Spec->catfile($_,Resources->currentLocale,@classNamespace,$classShortName);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
67 } @Locations;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
68
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
69
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
70 if ($fullModulePath) {
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
71
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
72 # '/full/path/to/Foo/Bar.pm' -> '/full/path/to/Foo'
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
73 my ($vol,$dir,$file) = File::Spec->splitpath($fullModulePath);
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
74 my $baseDir = File::Spec->catpath($vol,$dir,'');
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
75
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
76 # '/full/path/to/Foo' -> '/full/path/to/Foo/locale/En_US/Bar'
318
1838bdb4d238 corrected support of resources localization
cin
parents: 292
diff changeset
77 push @ways, File::Spec->catfile($baseDir,'locale',Resources->currentLocale,$classShortName);
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
78 }
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
79
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
80 my $mapFile = first { -f } @ways;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
81
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
82 return unless $mapFile;
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
83
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
84 return ParseStringsMap($mapFile);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
85 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
86
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
87 sub ParseStringsMap {
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
88 my ($fname) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
89
337
f4e14f32cf54 fixed bugs with string and local $_
cin
parents: 318
diff changeset
90 open my $hRes, "<:encoding($Encoding)", $fname or die "Failed to open file $fname: $!";
f4e14f32cf54 fixed bugs with string and local $_
cin
parents: 318
diff changeset
91 local $_;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
92 my %Map;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
93 my $line = 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
94 while (<$hRes>) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
95 chomp;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
96 $line ++ and next if /^\s*$/;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
97
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
98 if (/^(\w+)\s*=\s*(.*)$/) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
99 $Map{$1} = $2;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
100 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
101 die "Invalid resource format in $fname at $line";
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 $line ++;
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 return \%Map;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
109 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
110
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
111 __END__
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
112
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
113 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
114
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
115 =head1 NAME
f47f93534005 Documentation
wizard
parents: 50
diff changeset
116
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
117 C<IMPL::Resources::Strings> - Строковые ресурсы
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
118
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
119 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
120
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
121 =begin code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
122
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
123 package Foo;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
124
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
125 use IMPL::Resources::Strings {
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
126 msg_say_hello => "Hello, %name%!",
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
127 msg_module_name => "Simple Foo class"
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 245
diff changeset
128 };
49
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 sub InviteUser {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
131 my ($this,$uname) = @_;
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 print msg_say_hello(name => $uname);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
134
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
135 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
136
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
137 =end code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
138
f47f93534005 Documentation
wizard
parents: 50
diff changeset
139 =head1 DESCRIPTION
f47f93534005 Documentation
wizard
parents: 50
diff changeset
140
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
141 Импортирует в целевой модуль функции, которые возвращают локализованные
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
142 параметризованные сообщения.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
143
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
144 При импорте ищутся модули по следующему алгоритму:
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
145
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
146 В каталогах из массива C<@Locations> ищется файл с относительным путем
245
7c517134c42f Added Unsupported media type Web exception
sergey
parents: 211
diff changeset
147 C<$Locale/$ModName>, где C<$Locale> - глобальная переменная
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
148 модуля C<IMPL::Resourses::Strings>, а переменная C<$ModName> получена
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
149 путем замены 'C<::>' в имени целевого модуля на 'C</>'.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
150
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
151 Если файл не был найден, то производится поиск в каталоге, где
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
152 расположен сам модуль, файла с относительным путем C<locale/$Locale/$ShortModName>,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
153 где C<$ShortModeName> - последняя часть после 'C<::>' из имени целевого модуля.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
154
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
155 Если файл не найден, то используются строки, указанные при объявлении
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
156 сообщений в целевом модуле.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
157
f47f93534005 Documentation
wizard
parents: 50
diff changeset
158 =head1 FORMAT
f47f93534005 Documentation
wizard
parents: 50
diff changeset
159
f47f93534005 Documentation
wizard
parents: 50
diff changeset
160 =begin code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
161
f47f93534005 Documentation
wizard
parents: 50
diff changeset
162 msg_name = any text with named %params%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
163 msg_hello = hello, %name%!!!
f47f93534005 Documentation
wizard
parents: 50
diff changeset
164 msg_resolve = this is a value of the property: %user.age%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
165
f47f93534005 Documentation
wizard
parents: 50
diff changeset
166 msg_short_err = %error.Message%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
167 msg_full_err = %error%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
168
f47f93534005 Documentation
wizard
parents: 50
diff changeset
169 =end code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
170
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
171 =cut