lib/Serge/Engine/Plugin/test_language.pm

filter: Statementstmt 70.4% 74 / 105 Branchbran 38.2% 13 / 34 Conditioncond 27.7% 5 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 73.3% 11 / 15 Podpod 0.0% 0 / 7 totaltotal 53.9% 103 / 191 SCAR 43.0
CC
26
Cov
59%
_expand_string
56.0
_fake_translate_string
12.9
adjust_phases
6.1
CRAP 74.0
« lib/Serge/Command/test_parser.pm ↑ Summary lib/Serge/Engine/Plugin/replace_strings.pm »
1 package Serge::Engine::Plugin::test_language;
2 2 ▶use parent Serge::Plugin::Base::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # Fake language used for testing purposes (i.e. "immediate translations").
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Serge::Util qw(is_flag_set set_flags);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $LANG = 'test';
12 2 our $LANGID = 0xffff;
13  
14 sub name {
15 0 ▶ return 'Test (fake) language translation provider';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
16 }
17  
18 sub init {
19 7 ▶ my $self = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
20  
21 7 $self->SUPER::init(@_);
22  
23 7 $self->merge_schema({
24 save_translations => 'BOOLEAN',
25 language => 'STRING',
26 transliterate => 'BOOLEAN',
27 expand_length => 'BOOLEAN',
28 fuzzy_translations => {
29 '*' => 'STRING'
30 },
31 translations => {
32 '*' => 'STRING'
33 }
34 });
35  
36 7 $self->add({
37 get_translation_pre => \&get_translation,
38 get_translation => \&get_translation,
39 can_generate_ts_file => \&can_process_ts_file,
40 can_process_ts_file => \&can_process_ts_file,
41 });
42 }
43  
44 sub validate_data {
45 7 ▶ my $self = shift;
Subroutine validate_data: called CC=3 SCAR=11.4
Pod undocumented
46  
47 7 $self->SUPER::validate_data;
48  
49 7 ▶ $self->{data}->{transliterate} = 1 unless exists $self->{data}->{transliterate};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{"transliterate"} 7 0 7
50  
51 7 ▶ $self->{language} = exists $self->{data}->{language} ? $self->{data}->{language} : $LANG;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'data'}{'language'} ? : 0 7 7
52 }
53  
54 sub adjust_phases {
55 7 ▶ my ($self, $phases) = @_;
Subroutine adjust_phases: called CC=5 SCAR=18.1
Pod undocumented
56  
57 7 $self->SUPER::adjust_phases($phases);
58  
59 # always tie to 'can_process_ts_file' phase
60 7 set_flags($phases, 'can_generate_ts_file', 'can_process_ts_file');
61  
62 # this plugin makes sense only when applied to either
63 # get_translation_pre or get_translation phase, but not both
64 7 my $f1 = is_flag_set($phases, 'get_translation_pre');
65 7 my $f2 = is_flag_set($phases, 'get_translation');
66 7 ▶ die "This plugin needs to be attached to either get_translation_pre or get_translation phase" if !$f1 && !$f2;
Branch: 1 branch
BranchTrueFalseTotal
unless $f1 or $f2 0 7 7
Condition33%1/3
$f1 or $f2
l!l&&r!l&&!r
700
MC/DC0%0/2
$f1 or $f2
$f1 $f2
Truth table33%1/3
A or B
A= $f1B= $f2
ABresult
1X1
011
000
67 7 ▶ die "This plugin needs to be attached to either get_translation_pre or get_translation phase, but not both" if $f1 && $f2;
Branch: 1 branch
BranchTrueFalseTotal
if $f1 and $f2 0 7 7
Condition33%1/3
$f1 and $f2
!ll&&!rl&&r
070
MC/DC0%0/2
$f1 and $f2
$f1 $f2
Truth table33%1/3
A and B
A= $f1B= $f2
ABresult
0X0
100
111
68 }
69  
70 # public static method
71 sub is_test_language {
72 116 ▶ my ($self, $lang) = @_;
Subroutine is_test_language: called CC=1 SCAR=0.0
Pod undocumented
73 116 return $lang eq $self->{language};
74 }
75  
76 # private static method
77 sub _fake_translate_string {
78 108 ▶ my ($self, $s) = @_;
Subroutine _fake_translate_string: called CC=11 SCAR=25.6
79  
80 # check 'fuzzy_translations' map and return such translations with a fuzzy flag set
81 108 ▶ return ($self->{data}->{fuzzy_translations}->{$s}, 1) if exists $self->{data}->{fuzzy_translations};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"fuzzy_translations"} 0 108 108
82  
83 # check 'translations' map and return such translations with no fuzzy flag set
84 108 ▶ return $self->{data}->{translations}->{$s} if exists $self->{data}->{translations};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"data"}{"translations"} 98 10 108
85  
86 10 my $out = $s;
87 10 my $out_plaintext = $s;
88  
89 10 my $has_plurr_like_formatting = $s =~ m/\{[^\}]*\{/;
90  
91 10 ▶ if ($self->{data}->{transliterate} && !$has_plurr_like_formatting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"}{"transliterate"} and not $has_plurr_like_formatting) 10 0 10
Condition33%1/3
$self->{"data"}{"transliterate"} and not $has_plurr_like_formatting
!ll&&!rl&&r
0010
MC/DC0%0/2
$self->{"data"}{"transliterate"} and not $has_plurr_like_formatting
$self->{"data"}{"transliterate"} not $has_plurr_like_formatting
Truth table33%1/3
A and not(B)
A= $self->{"data"}{"transliterate"}B= not $has_plurr_like_formatting
ABresult
0X0
100
111
92 # Map English alphabet to similarly looking Unicode symbols with accents, umlauts, etc.
93  
94 10 my $map = {
95 'A' => "\x{100}", 'B' => "\x{1e02}", 'C' => "\x{106}", 'D' => "\x{110}",
96 'E' => "\x{112}", 'F' => "\x{1e1e}", 'G' => "\x{11c}", 'H' => "\x{124}",
97 'I' => "\x{128}", 'J' => "\x{134}", 'K' => "\x{136}", 'L' => "\x{141}",
98 'M' => "\x{1e3e}", 'N' => "\x{145}", 'O' => "\x{150}", 'P' => "\x{1e54}",
99 'Q' => "\x{3a9}", 'R' => "\x{156}", 'S' => "\x{160}", 'T' => "\x{166}",
100 'U' => "\x{16e}", 'V' => "\x{1e7c}", 'W' => "\x{174}", 'X' => "\x{4b2}",
101 'Y' => "\x{176}", 'Z' => "\x{17d}", 'a' => "\x{e1}", 'b' => "\x{1e03}",
102 'c' => "\x{e7}", 'd' => "\x{111}", 'e' => "\x{113}", 'f' => "\x{1e1f}",
103 'g' => "\x{11f}", 'h' => "\x{125}", 'i' => "\x{129}", 'j' => "\x{135}",
104 'k' => "\x{137}", 'l' => "\x{13c}", 'm' => "\x{1e3f}", 'n' => "\x{14b}",
105 'o' => "\x{151}", 'p' => "\x{1e55}", 'q' => "\x{1eb}", 'r' => "\x{155}",
106 's' => "\x{161}", 't' => "\x{163}", 'u' => "\x{169}", 'v' => "\x{1e7d}",
107 'w' => "\x{175}", 'x' => "\x{4b3}", 'y' => "\x{177}", 'z' => "\x{17e}"
108 };
109  
110 # As PHP blocks can be inside tags, substitute them to ease parsing
111 10 $s =~ s/<\?(.*?)\?>/'__PHP__BLOCK__BEGIN__'._fake_translate_string_escape($1).'__PHP__BLOCK__END__'/ge;
112  
113 10 my $sprintf = '%[-+]*[0 #]*[\d]*(?:.\d+)*(?:h|l|I|I32|I64)*[cCdiouxXeEfgGnsS]';
114 10 my $escape = '\\\\[ntvbrfax]'; # '\' is double-escaped
115 10 my $var = '%(?:\w+:)*\w+(?:&\w)*%';
116 10 my $tag = '<[^\?]>|<[^\?].*?[^\?]>'; # do not count <? and ?> inside tags
117 10 my $php = '__PHP__BLOCK__BEGIN__.*?__PHP__BLOCK__END__';
118 10 my $entity = '\&(?:\w+);';
119 10 my $javafmt = '\{\d+\}';
120 10 my $tplfmt = '\$\{.+?\}';
121 10 my $unixpath = '(?:^|[\/\r\n\t ]+)(?:\/[\w\d\.\%\?\-\=\,\&]+)+';
122 10 my $url = '(?:http|https|ftp|ssh):\/\/[\/\w\d\.\%\?\-\=\,\&\+\#\@]+';
123 10 my $mailto = 'mailto:[\/\w\d\.\+\@]+';
124 10 my @chunks = split(/($var|$sprintf|$escape|$php|$tag|$entity|$javafmt|$tplfmt|$unixpath|$url|$mailto)/, $s);
125  
126 10 $out = '';
127 10 $out_plaintext = '';
128 10 my $translate;
129 10 foreach my $chunk (@chunks) {
130 20 $translate = !$translate;
131  
132 20 ▶ if ($translate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($translate) { } 10 10 20
133 10 my @chars = split(//, $chunk);
134 10 foreach my $char (@chars) {
135 0 ▶ $char = $map->{$char} || $char;
Condition0%0/3
$map->{$char} || $char
l!l&&r!l&&!r
000
MC/DC0%0/2
$map->{$char} || $char
$map->{$char} $char
Truth table0%0/3
A || B
A= $map->{$char}B= $char
ABresult
1X1
011
000
136 0 $out .= $char;
137 0 $out_plaintext .= $char;
138 }
139 } else {
140 10 $out .= $chunk;
141 }
142 }
143  
144 # Substitute temporary markers back
145  
146 10 $out =~ s/__PHP__BLOCK__BEGIN__(.*?)__PHP__BLOCK__END__/'<?'._fake_translate_string_unescape($1).'?>'/ge;
147 }
148  
149 # normalize plaintext string to see if it has
150  
151 10 $out_plaintext =~ s/\s{2,}/ /sg; # normalize whitespace
152 10 $out_plaintext =~ s/^\s+//sg; # trim left
153 10 $out_plaintext =~ s/\s+$//sg; # trim right
154  
155 10 ▶ $out = _expand_string($out) if $self->{data}->{expand_length} && $out_plaintext ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"}{"expand_length"} and $out_plaintext ne "" 0 10 10
Condition33%1/3
$self->{"data"}{"expand_length"} and $out_plaintext ne ""
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->{"data"}{"expand_length"} and $out_plaintext ne ""
$self->{"data"}{"expand_length"} $out_plaintext ne ""
Truth table33%1/3
A and B
A= $self->{"data"}{"expand_length"}B= $out_plaintext ne ""
ABresult
0X0
100
111
156  
157 10 return $out;
158 }
159  
160 sub _fake_translate_string_escape {
161 0 ▶ my $s = shift;
Subroutine _fake_translate_string_escape: not called CC=1 SCAR=6.9
162  
163 0 $s =~ s/</__LESS__THAN__/g;
164 0 $s =~ s/>/__GREATER__THAN__/g;
165  
166 0 return $s;
167 }
168  
169 sub _fake_translate_string_unescape {
170 0 ▶ my $s = shift;
Subroutine _fake_translate_string_unescape: not called CC=1 SCAR=6.9
171  
172 0 $s =~ s/__LESS__THAN__/</g;
173 0 $s =~ s/__GREATER__THAN__/>/g;
174  
175 0 return $s;
176 }
177  
178 sub _expand_string {
179 0 ▶ my $s = shift;
Subroutine _expand_string: not called CC=7 SCAR=40.3
180  
181 0 ▶ return $s if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 0 0
182  
183 0 my $count = length($s);
184  
185 0 my $coeff = 1.4;
186 0 ▶ $coeff = 1.5 if $count < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 200 0 0 0
187 0 ▶ $coeff = 1.7 if $count < 20;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 20 0 0 0
188 0 ▶ $coeff = 2 if $count < 10;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 10 0 0 0
189  
190 0 $count = int($count * ($coeff - 1));
191  
192 0 my $dummy_text = ' xxxxxxxxxx';
193 # make sure dummy text length exceeds the needed number of chars to copy
194 0 ▶ while (length($dummy_text) < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dummy_text < $count) 0 0 0
195 0 $dummy_text .= $dummy_text;
196 }
197  
198 0 my $start = '[';
199 0 my $stop = ']';
200 # if the source string already uses square brackets (for e.g. some sort of placeholders),
201 # use parenthesis as markers instead
202 0 ▶ if ($s =~ m/[\[\]]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /[\[\]]/) 0 0 0
203 0 $start = '(';
204 0 $stop = ')';
205 }
206  
207 0 return $start.$s.substr($dummy_text, 0, $count).$stop;
208 }
209  
210 sub get_translation {
211 108 ▶ my ($self, $phase, $string, $context, $namespace, $filepath, $lang) = @_;
Subroutine get_translation: called CC=2 SCAR=7.1
Pod undocumented
212  
213 108 ▶ return () unless $self->is_test_language($lang);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_test_language($lang) 0 108 108
214 108 return ($self->_fake_translate_string($string), undef, undef, $self->{data}->{save_translations});
215 }
216  
217 sub can_process_ts_file {
218 8 ▶ my ($self, $phase, $file, $lang) = @_;
Subroutine can_process_ts_file: called CC=3 SCAR=12.5
Pod undocumented
219  
220 # if this is a test language, do not import anything from translation file unless `save_translations' flag is on
221 8 ▶ return 0 if $self->is_test_language($lang) && !$self->{data}->{save_translations};
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_test_language($lang) and not $self->{"data"}{"save_translations"} 0 8 8
Condition33%1/3
$self->is_test_language($lang) and not $self->{"data"}{"save_translations"}
!ll&&!rl&&r
080
MC/DC0%0/2
$self->is_test_language($lang) and not $self->{"data"}{"save_translations"}
$self->is_test_language($lang) not $self->{"data"}{"save_translations"}
Truth table33%1/3
A and not(B)
A= $self->is_test_language($lang)B= not $self->{"data"}{"save_translations"}
ABresult
0X0
100
111
222  
223 # by default, allow to process any translation files for any given target language
224 8 return 1;
225 }
226  
227 2 1;
« lib/Serge/Command/test_parser.pm ↑ Summary lib/Serge/Engine/Plugin/replace_strings.pm »