OpenInteract2/I18N/Initializer.pm

filter: Statementstmt 15.5% 25 / 161 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 38.8% 7 / 18 Podpod 0.0% 0 / 5 totaltotal 10.4% 32 / 306 CC 50 SCAR 43.9
CC
50
Cov
10%
_read_msg_file
156.0
_generate_language_class
156.0
run
110.0
CRAP 80.9
« OpenInteract2/Manage/Website/TestLDAP.pm ↑ Summary OpenInteract2/Auth/AdminCheck.pm »
1 package OpenInteract2::I18N::Initializer;
2  
3 # $Id: Initializer.pm,v 1.14 2005/03/18 04:09:50 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 87 my ( $TEMPLATE, $BASE_CLASS );
14  
15 87 $OpenInteract2::I18N::Initializer::VERSION = sprintf("%d.%02d", q$Revision: 1.14 $ =~ /(\d+)\.(\d+)/);
16  
17 87 my ( $log );
18  
19 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
20 0 my ( $class ) = @_;
21 0 return bless( { _files => [] }, $class );
22 }
23  
24 # This may be really naive, but it should work for po/mo/msg files
25 # should match: en | en-US | en_US
26  
27 0 ▶sub is_valid_message_file {
Subroutine is_valid_message_file: not called CC=3 SCAR=24.8
Pod undocumented
28 0 my ( $class, $filename ) = @_;
29 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
30 0 my ( $lang ) = $filename =~ m/\b(\w\w|\w\w\-\w+|\w\w_\w+)\.\w+$/;
31 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Pulled language '${lang}' from file '${filename}'") 0 0 0
32 $log->debug( "Pulled language '$lang' from file '$filename'" );
33 0 return $lang;
34 }
35  
36  
37 0 ▶sub add_message_files {
Subroutine add_message_files: not called CC=6 SCAR=37.4
Pod undocumented
38 0 my ( $self, @files ) = @_;
39 0 ▶ return $self->{_files} unless ( scalar @files );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @files 0 0 0
40 0 ▶ $log ||= get_logger( LOG_INIT );
Condition0%0/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
41  
42 # ensure all files have an identifiable language
43 0 foreach my $msg_file ( @files ) {
44 0 my $lang = $self->is_valid_message_file( $msg_file );
45 0 ▶ unless ( $lang ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang) 0 0 0
46 0 $log->error( "File '$msg_file' does not have identifiable language" );
47 0 oi_error "Cannot identify language from message file ",
48 "'$msg_file'. It must end with a language code ",
49 "before the file extension. For example: ",
50 "'myapp-en.msg', 'MyReallyBigApp-es-MX.dat'";
51 }
52 }
53 $log->is_debug &&
54 0 ▶ $log->debug( "Adding message files: ", join( ', ', @files ) );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Adding message files: ", join(", ", @files)) 0 0 0
55 0 push @{ $self->{_files} }, @files;
56 0 return $self->{_files};
57 }
58  
59 0 ▶sub locate_global_message_files {
Subroutine locate_global_message_files: not called CC=4 SCAR=30.0
Pod undocumented
60 0 my ( $self ) = @_;
61 0 ▶ $log ||= get_logger( LOG_INIT );
Condition0%0/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
62 0 my $msg_dir = CTX->lookup_directory( 'msg' );
63 0 ▶ opendir( MSGDIR, $msg_dir )
Branch: 1 branch
BranchTrueFalseTotal
opendir MSGDIR, $msg_dir or oi_error("Cannot read from global message directory '${msg_dir}': $!") 0 0 0
64 || oi_error "Cannot read from global message directory '$msg_dir': $!";
65 0 my @msg_files = grep /\.(msg|mo|po)$/, readdir( MSGDIR );
66 0 closedir( MSGDIR );
67 0 my @full_msg_files = map { catfile( $msg_dir, $_ ) } @msg_files ;
68 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Found global message files: ", join(", ", @full_msg_files)) 0 0 0
69 $log->debug( "Found global message files: ",
70 join( ', ', @full_msg_files ) );
71 0 $self->add_message_files( @full_msg_files );
72 0 return \@full_msg_files;
73 }
74  
75 0 ▶sub run {
Subroutine run: not called CC=10 SCAR=47.0
Pod undocumented
76 0 my ( $self ) = @_;
77 0 ▶ $log ||= get_logger( LOG_INIT );
Condition0%0/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
78 0 ▶ unless ( ref $self->{_files} eq 'ARRAY' and @{ $self->{_files} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"_files"} eq "ARRAY" and @{$self->{"_files"};}) 0 0 0
Condition0%0/3
ref $self->{"_files"} eq "ARRAY" and @{$self->{"_files"};}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"_files"} eq "ARRAY" and @{$self->{"_files"};}
ref $self->{"_files"} eq "ARRAY" @{$self->{"_files"};}
Truth table0%0/3
A and B
A= ref $self->{"_files"} eq "ARRAY"B= @{$self->{"_files"};}
ABresult
0X0
100
111
79 0 $log->warn( "Asked to generate localization classes but no ",
80 "localization message files assigned; weird..." );
81 0 return [];
82 }
83  
84 0 $self->_check_for_gettext_files();
85  
86 # %all_messages is:
87 # ->lang->lang_key = msg
88 # ->lang->SOURCE->lang_key = file with key
89 # ...see _assign_file_messages() for where it gets filled
90  
91 0 my %all_messages = ();
92 0 foreach my $msg_file ( @{ $self->{_files} } ) {
93 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Reading messages from file '${msg_file}'") 0 0 0
94 $log->debug( "Reading messages from file '$msg_file'" );
95 0 my %file_messages = ();
96 0 ▶ if ( $msg_file =~ /\.msg$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($msg_file =~ /\.msg$/) { } 0 0 0
elsif ($msg_file =~ /\.(mo|po)$/) { } 0 0 0
97 0 %file_messages = $self->_read_msg_file( $msg_file );
98 }
99 elsif ( $msg_file =~ /\.(mo|po)$/ ) {
100 0 %file_messages = $self->_read_gettext_file( $msg_file );
101 }
102 0 $self->_assign_file_messages( $msg_file, \%all_messages, \%file_messages );
103 }
104  
105 # ...message key sources aren't needed anymore so delete
106  
107 0 ▶ while ( my ( $lang, $lang_msg ) = each %all_messages ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %all_messages) 0 0 0
108 0 delete $lang_msg->{SOURCE};
109 }
110  
111 # Now all messages are read in and merged, generate the classes
112  
113 0 my @generated_classes = ();
114 0 foreach my $lang ( keys %all_messages ) {
115 my $generated_class =
116 0 $self->_generate_language_class( $lang, $all_messages{ $lang } );
117 0 push @generated_classes, $generated_class;
118 }
119 0 return \@generated_classes;
120 }
121  
122  
123 ########################################
124 # private methods below here
125  
126 0 ▶sub _check_for_gettext_files {
Subroutine _check_for_gettext_files: not called CC=4 SCAR=30.0
127 0 my ( $self ) = @_;
128 0 my $has_gettext = grep /\.(mo|po)$/, @{ $self->{_files} };
129 0 ▶ if ( $has_gettext ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_gettext) 0 0 0
130 0 eval "require Locale::Maketext::Lexicon::Gettext";
131 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
132 0 oi_error "Locale::Maketext::Lexicon is required to parse mo/po ",
133 "localization files. Please install it.";
134 }
135 $log->is_info &&
136 0 ▶ $log->info( "Required Locale::Maketext::Lexicon::Gettext ok" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_info and $log->info("Required Locale::Maketext::Lexicon::Gettext ok") 0 0 0
137 }
138 }
139  
140 # merge messages from a file into all messages
141  
142 0 ▶sub _assign_file_messages {
Subroutine _assign_file_messages: not called CC=3 SCAR=24.8
143 0 my ( $self, $file, $all_messages, $file_messages ) = @_;
144 0 my $lang = $self->is_valid_message_file( $file );
145 0 ▶ while ( my ( $key, $value ) = each %{ $file_messages } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$file_messages;}) 0 0 0
146 0 ▶ if ( $all_messages->{ $lang }{ $key } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_messages->{$lang}{$key}) { } 0 0 0
147 0 my $source = $all_messages->{ $lang }{SOURCE}{ $key };
148 0 $log->warn(
149 "DUPLICATE MESSAGE KEY FOUND. Key '$key' from ",
150 "'$file' was already found in message file ",
151 "'$source' read in earlier. Existing key WILL NOT BE ",
152 "OVERWRITTEN, which may cause odd application behavior." );
153 }
154 else {
155 0 $all_messages->{ $lang }{ $key } = $value;
156 0 $all_messages->{ $lang }{SOURCE}{ $key } = $file;
157 }
158 }
159 }
160  
161 0 ▶sub _read_msg_file {
Subroutine _read_msg_file: not called CC=12 SCAR=50.5
162 0 my ( $self, $msg_file ) = @_;
163 0 my %messages = ();
164 0 my ( $current_key, $current_msg, $readmore );
165 0 ▶ open( MSG, '<', $msg_file )
Branch: 1 branch
BranchTrueFalseTotal
open MSG, "<", $msg_file or oi_error("Cannot read messages from '${msg_file}': $!") 0 0 0
166 || oi_error "Cannot read messages from '$msg_file': $!";
167 0 ▶ while ( <MSG> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MSG)) 0 0 0
168 0 chomp;
169  
170 # Skip comments and blanks unless we're in a readmore block
171 0 ▶ next if ( ! $readmore and /^\s*\#/ );
Branch: 1 branch
BranchTrueFalseTotal
if not $readmore and /^\s*\#/ 0 0 0
Condition0%0/3
not $readmore and /^\s*\#/
!ll&&!rl&&r
000
MC/DC0%0/2
not $readmore and /^\s*\#/
not $readmore /^\s*\#/
Truth table0%0/3
not(A) and B
A= not $readmoreB= /^\s*\#/
ABresult
0X0
100
111
172 0 ▶ next if ( ! $readmore and /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if not $readmore and /^\s*$/ 0 0 0
Condition0%0/3
not $readmore and /^\s*$/
!ll&&!rl&&r
000
MC/DC0%0/2
not $readmore and /^\s*$/
not $readmore /^\s*$/
Truth table0%0/3
not(A) and B
A= not $readmoreB= /^\s*$/
ABresult
0X0
100
111
173  
174 0 my $line = $_;
175 0 my $this_readmore = $line =~ s|\\\s*$||;
176  
177 # lop off spaces at the beginning of continued lines so
178 # they're more easily distinguished
179 0 ▶ if ( $readmore ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($readmore) { } 0 0 0
180 0 $line =~ s/^\s+//;
181 0 $current_msg .= $line;
182 }
183 else {
184 0 my ( $key, $msg ) = $line =~ /^\s*([\w\.]+)\s*=\s*(.*)$/;
185 0 ▶ if ( $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 0 0 0
186 0 ▶ if ( $current_key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_key) 0 0 0
187 0 $messages{ $current_key } = $current_msg;
188 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Set '${current_key}' = '${current_msg}'") 0 0 0
189 $log->debug( "Set '$current_key' = '$current_msg'" );
190 }
191 0 $current_key = $key;
192 0 $current_msg = $msg;
193 0 $readmore = undef;
194 }
195 }
196 0 $readmore = $this_readmore;
197 }
198 0 close( MSG );
199 0 ▶ $log->is_debug && $log->debug( "Set '$current_key' = '$current_msg'" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Set '${current_key}' = '${current_msg}'") 0 0 0
200 0 $messages{ $current_key } = $current_msg;
201 0 return %messages;
202 }
203  
204  
205 0 ▶sub _read_gettext_file {
Subroutine _read_gettext_file: not called CC=4 SCAR=30.0
206 0 my ( $self, $gettext_file ) = @_;
207 0 ▶ open( GETTEXT, '<', $gettext_file )
Branch: 1 branch
BranchTrueFalseTotal
open GETTEXT, "<", $gettext_file or oi_error("Failed to open gettext file: $!") 0 0 0
208 || oi_error "Failed to open gettext file: $!";
209 0 my $msg = Locale::Maketext::Lexicon::Gettext->parse( <GETTEXT> );
210 0 close( GETTEXT );
211 0 ▶ if ( $log->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_debug) 0 0 0
212 0 $log->debug( "Read following messages from '$gettext_file': " );
213 0 ▶ while ( my ( $key, $value ) = each %{ $msg } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$msg;}) 0 0 0
214 0 $log->debug( " '$key' = '$value'" );
215 }
216 }
217 0 return %{ $msg }
218 }
219  
220  
221 0 ▶sub _generate_language_class {
Subroutine _generate_language_class: not called CC=12 SCAR=50.5
222 0 my ( $self, $lang, $messages ) = @_;
223 0 ▶ $log ||= get_logger( LOG_INIT );
Condition0%0/3
$log ||= get_logger("OI2.INITIALIZE")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2.INITIALIZE")
$log get_logger("OI2.INITIALIZE")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2.INITIALIZE")
ABresult
1X1
011
000
224  
225 0 ▶ unless ( $lang ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang) 0 0 0
226 0 oi_error "Cannot generate maketext class without a language";
227 }
228  
229 0 my @base_class_pieces = ( 'OpenInteract2', 'I18N' );
230 0 my @lang_class_pieces = @base_class_pieces;
231  
232 # 'en' is always the default language no matter what your
233 # website's default language is
234  
235 0 ▶ unless ( $lang eq 'en' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lang eq "en") 0 0 0
236 0 push @base_class_pieces, 'en';
237 }
238  
239 0 ▶ if ( my @pieces = split( /[\-\_]/, $lang ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my @pieces = split(/[\-\_]/, $lang, 0)) { } 0 0 0
240 0 push @lang_class_pieces, @pieces; # 'es', 'mx';
241 0 pop @pieces;
242 0 push @base_class_pieces, @pieces; # 'es'
243 }
244 else {
245 0 push @lang_class_pieces, $lang; # 'es'
246 }
247 0 my $base_class = join( '::', @base_class_pieces );
248 0 my $lang_class = join( '::', @lang_class_pieces );
249 0 my %params = (
250 lang => $lang,
251 lang_class => $lang_class,
252 base_class => $base_class,
253 messages => $messages,
254 );
255  
256 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Trying to generate class '${lang_class}' for language ", "'${lang}' with base class '${base_class}'") 0 0 0
257 $log->debug( "Trying to generate class '$lang_class' for language ",
258 "'$lang' with base class '$base_class'" );
259 0 my ( $gen_class );
260  
261 0 ▶ $TEMPLATE ||= Template->new();
Condition0%0/3
$TEMPLATE ||= "Template"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$TEMPLATE ||= "Template"->new
$TEMPLATE "Template"->new
Truth table0%0/3
A ||= B
A= $TEMPLATEB= "Template"->new
ABresult
1X1
011
000
262 0 ▶ $BASE_CLASS ||= $self->_get_lang_template();
Condition0%0/3
$BASE_CLASS ||= $self->_get_lang_template
l!l&&r!l&&!r
000
MC/DC0%0/2
$BASE_CLASS ||= $self->_get_lang_template
$BASE_CLASS $self->_get_lang_template
Truth table0%0/3
A ||= B
A= $BASE_CLASSB= $self->_get_lang_template
ABresult
1X1
011
000
263  
264 0 ▶ $TEMPLATE->process( \$BASE_CLASS, \%params, \$gen_class )
Branch: 1 branch
BranchTrueFalseTotal
$TEMPLATE->process(\$BASE_CLASS, \%params, \$gen_class) or oi_error("Failed to process maketext subclass template: ", $TEMPLATE->error) 0 0 0
265 || oi_error "Failed to process maketext subclass template: ",
266 $TEMPLATE->error();
267 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Class generated ok; now eval'ing class with\n", $gen_class) 0 0 0
268 $log->debug( "Class generated ok; now eval'ing class with\n",
269 $gen_class );
270 0 eval $gen_class;
271 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
272 0 $log->error( "Failed to evaluate generated class\n$gen_class\n$@" );
273 0 oi_error "Failed to evaluate generated class '$lang_class': $@";
274 }
275 $log->is_debug &&
276 0 ▶ $log->debug( "Evaluated class $lang_class ok" );
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Evaluated class $lang_class ok") 0 0 0
277 0 return $lang_class;
278 }
279  
280 0 ▶sub _get_lang_template {
Subroutine _get_lang_template: not called CC=1 SCAR=6.9
281 0 return <<'TEMPLATE';
282 package [% lang_class %];
283  
284 use strict;
285  
286 use vars qw( %Lexicon );
287  
288 @[% lang_class %]::ISA = qw( [% base_class %] );
289  
290 sub get_oi2_lang { return '[% lang %]' }
291  
292 %Lexicon = (
293 [% FOREACH msg_key = messages.keys -%]
294 '[% msg_key %]' => qq{[% messages.$msg_key %]},
295 [% END -%]
296 );
297  
298 1;
299  
300 TEMPLATE
301 }
302  
303 87 1;
304  
305 __END__
« OpenInteract2/Manage/Website/TestLDAP.pm ↑ Summary OpenInteract2/Auth/AdminCheck.pm »