| 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")
MC/DC0%0/2
$log ||= get_logger("OI2")
$log
get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
| |||||||||||||||||||||||||||||
| 30 | 0 | my ( $lang ) = $filename =~ m/\b(\w\w|\w\w\-\w+|\w\w_\w+)\.\w+$/; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | $log->is_debug && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $log ||= get_logger( LOG_INIT ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$log ||= get_logger("OI2.INITIALIZE")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||
| 62 | 0 | my $msg_dir = CTX->lookup_directory( 'msg' ); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | opendir( MSGDIR, $msg_dir ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless ( ref $self->{_files} eq 'ARRAY' and @{ $self->{_files} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $self->{"_files"} eq "ARRAY" and @{$self->{"_files"};}
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"};}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 94 | $log->debug( "Reading messages from file '$msg_file'" ); | ||||||||||||||||||||||||||||
| 95 | 0 | my %file_messages = (); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | if ( $msg_file =~ /\.msg$/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 0 | eval "require Locale::Maketext::Lexicon::Gettext"; | |||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if ( $all_messages->{ $lang }{ $key } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 166 | || oi_error "Cannot read messages from '$msg_file': $!"; | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | while ( <MSG> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
not $readmore and /^\s*\#/
MC/DC0%0/2
not $readmore and /^\s*\#/
not $readmore
/^\s*\#/
Truth table0%0/3
not(A) and B
A= not $readmoreB= /^\s*\#/
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | next if ( ! $readmore and /^\s*$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $readmore and /^\s*$/
MC/DC0%0/2
not $readmore and /^\s*$/
not $readmore
/^\s*$/
Truth table0%0/3
not(A) and B
A= not $readmoreB= /^\s*$/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ( $current_key ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 0 | $messages{ $current_key } = $current_msg; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | $log->is_debug && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 212 | 0 | $log->debug( "Read following messages from '$gettext_file': " ); | |||||||||||||||||||||||||||
| 213 | 0 | ▶ | while ( my ( $key, $value ) = each %{ $msg } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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")
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")
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | unless ( $lang ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 236 | 0 | push @base_class_pieces, 'en'; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 0 | ▶ | if ( my @pieces = split( /[\-\_]/, $lang ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$TEMPLATE ||= "Template"->new
$TEMPLATE
"Template"->new
Truth table0%0/3
A ||= B
A= $TEMPLATEB= "Template"->new
| |||||||||||||||||||||||||||||
| 262 | 0 | ▶ | $BASE_CLASS ||= $self->_get_lang_template(); | ||||||||||||||||||||||||||
|
Condition0%0/3
$BASE_CLASS ||= $self->_get_lang_template
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
| |||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | $TEMPLATE->process( \$BASE_CLASS, \%params, \$gen_class ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | || oi_error "Failed to process maketext subclass template: ", | ||||||||||||||||||||||||||||
| 266 | $TEMPLATE->error(); | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | $log->is_debug && | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||