annotate Lib/IMPL/Resources/Strings.pm @ 180:d1676be8afcc

Перекодировка в utf-8
author sourcer
date Fri, 30 Dec 2011 23:40:00 +0300
parents 4267a2ac3d46
children 2b9b55cfb79b
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::Storage;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
5 package IMPL::Resources::Strings;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
6 use File::Spec;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
7
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
8 our $Locale ||= 'default';
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
9 our $Base ||='locale';
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
10 our $Encoding ||= 'utf-8';
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
11 our @Locations;
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 @Locations = ('.') unless @Locations;
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
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
18 my ($class,$pathModule) = caller;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
19
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
20 my ($vol,$dir,$file) = File::Spec->splitpath($pathModule);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
21 my $baseDir = File::Spec->catpath($vol,$dir,'');
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
22
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
23 my @pathClass = split /::/,$class;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
24 my $fileClass = pop @pathClass;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
25
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
26 my @ways = map {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
27 my @path = ($_);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
28 push @path,$Base;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
29 push @path,$Locale;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
30
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
31 File::Spec->catfile(@path,@pathClass,$fileClass);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
32 } @Locations;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
33
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
34 push @ways, File::Spec->catfile($baseDir,'locale',$Locale,$fileClass);
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
35
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
36
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
37
50
wizard@linux-odin.local
parents: 49
diff changeset
38 my $stringsStorage = findResource($class,@ways);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
39
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
40 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
41
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
42 sub findResource {
50
wizard@linux-odin.local
parents: 49
diff changeset
43 my ($class,$refWays) = @_;
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
44
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
45
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
46 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
47
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
48
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
50 sub parseResource {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
51 my ($fname) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
52
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
53 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
54
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
55 my %Map;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
56 my $line = 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
57 while (<$hRes>) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
58 chomp;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
59 $line ++ and next if /^\s*$/;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
60
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
61 if (/^(\w+)\s*=\s*(.*)$/) {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
62 $Map{$1} = $2;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
63 } else {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
64 die "Invalid resource format in $fname at $line";
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 $line ++;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
67 }
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 return \%Map;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
70 }
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
71
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
72 package IMPL::Resources::Strings::Storage;
166
4267a2ac3d46 Added Class::Template,
wizard
parents: 66
diff changeset
73 use parent qw(IMPL::Object);
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
74
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
75 sub get {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
76 my ($this,$msg_name) = @_;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
77 }
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
78
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
79 1;
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
80
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
81 __END__
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 =pod
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
84
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
85 =head1 NAME
f47f93534005 Documentation
wizard
parents: 50
diff changeset
86
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
87 C<IMPL::Resources::Strings> - Строковые ресурсы
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
88
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
89 =head1 SYNOPSIS
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
90
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
91 =begin code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
92
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
93 package Foo;
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 use IMPL::Resources::Strings {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
96 msg_say_hello => "Hello, %name!",
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
97 msg_module_name => "Simple Foo class"
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
98 }, auto => 1, locale => 'en-us';
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 sub InviteUser {
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
101 my ($this,$uname) = @_;
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 print msg_say_hello(name => $uname);
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
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
107 =end code
f47f93534005 Documentation
wizard
parents: 50
diff changeset
108
f47f93534005 Documentation
wizard
parents: 50
diff changeset
109 =head1 DESCRIPTION
f47f93534005 Documentation
wizard
parents: 50
diff changeset
110
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
111 Импортирует в целевой модуль функции, которые возвращают локализованные
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
112 параметризованные сообщения.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
113
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
114 При импорте ищутся модули по следующему алгоритму:
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
115
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
116 В каталогах из массива C<@Locations> ищется файл с относительным путем
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
117 C<$Base/$Locale/$ModName>, где C<$Base>, C<$Locale> - глобальные переменные
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
118 модуля C<IMPL::Resourses::Strings>, а переменная C<$ModName> получена
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
119 путем замены 'C<::>' в имени целевого модуля на 'C</>'.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
120
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
121 Если файл не был найден, то производится поиск в каталоге, где
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
122 расположен сам модуль, файла с относительным путем C<locale/$Locale/$ShortModName>,
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
123 где C<$ShortModeName> - последняя часть после 'C<::>' из имени целевого модуля.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
124
180
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
125 Если файл не найден, то используются строки, указанные при объявлении
d1676be8afcc Перекодировка в utf-8
sourcer
parents: 166
diff changeset
126 сообщений в целевом модуле.
66
f47f93534005 Documentation
wizard
parents: 50
diff changeset
127
f47f93534005 Documentation
wizard
parents: 50
diff changeset
128 =head1 FORMAT
f47f93534005 Documentation
wizard
parents: 50
diff changeset
129
f47f93534005 Documentation
wizard
parents: 50
diff changeset
130 =begin code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
131
f47f93534005 Documentation
wizard
parents: 50
diff changeset
132 msg_name = any text with named %params%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
133 msg_hello = hello, %name%!!!
f47f93534005 Documentation
wizard
parents: 50
diff changeset
134 msg_resolve = this is a value of the property: %user.age%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
135
f47f93534005 Documentation
wizard
parents: 50
diff changeset
136 msg_short_err = %error.Message%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
137 msg_full_err = %error%
f47f93534005 Documentation
wizard
parents: 50
diff changeset
138
f47f93534005 Documentation
wizard
parents: 50
diff changeset
139 =end code text
f47f93534005 Documentation
wizard
parents: 50
diff changeset
140
49
16ada169ca75 migrating to the Eclipse IDE
wizard@linux-odin.local
parents: 46
diff changeset
141 =cut