| 1 | package Serge::Engine::Plugin::parse_strings; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Engine::Plugin::Base::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # TODO: | ||||||||||||||||||||||||||||
| 7 | # - Decode \\Uxxxx in strings | ||||||||||||||||||||||||||||
| 8 | # - Support "key" = "value" pairs that span multiple lines | ||||||||||||||||||||||||||||
| 9 | # - Better handling of comments | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub name { | ||||||||||||||||||||||||||||
| 12 | 0 | ▶ | return 'MacOS/iOS .strings parser plugin'; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub parse { | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: not called CC=16 SCAR=56.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 0 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $translated_text; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Finding translatable strings in file | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Format is: | ||||||||||||||||||||||||||||
| 25 | # =================================================== | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # /* optional single-line comment that is skipped, as it is followed by a blank line */ | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # /* optional single-line comment that is treated as hint for the line below */ | ||||||||||||||||||||||||||||
| 30 | # "some key" = "value" // optional comment that is treated as additional hint | ||||||||||||||||||||||||||||
| 31 | # // or | ||||||||||||||||||||||||||||
| 32 | # some_key = "value" // optional comment that is treated as additional hint | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # =================================================== | ||||||||||||||||||||||||||||
| 35 | # The key name is also appended to a hint message. | ||||||||||||||||||||||||||||
| 36 | # If the key contains '##suffix', the 'suffix' is used as a context string | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | my @all_hints; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | foreach my $line (split(/\n/, $$textref)) { | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | my $key; | |||||||||||||||||||||||||||
| 43 | my $hint; | ||||||||||||||||||||||||||||
| 44 | 0 | my $context; | |||||||||||||||||||||||||||
| 45 | 0 | my $orig_str; | |||||||||||||||||||||||||||
| 46 | 0 | my $translated_str; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($line =~ m/^[\t ]*$/) { # blank line | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 49 | 0 | @all_hints = (); # reset accumulator array | |||||||||||||||||||||||||||
| 50 | } elsif ($line =~ m/^[\t ]*\/\*[\t ]*(.*?)[\t ]*\*\/[\t ]*$/) { # a /* */ comment line | ||||||||||||||||||||||||||||
| 51 | 0 | my $s = $1; | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ($s && ($s !~ m/^Class = /)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$s and not $s =~ /^Class = /
MC/DC0%0/2
$s and not $s =~ /^Class = /
$s
not $s =~ /^Class = /
Truth table0%0/3
A and not(B)
A= $sB= not $s =~ /^Class = /
| |||||||||||||||||||||||||||||
| 53 | 0 | push(@all_hints, $s); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | } elsif ($line =~ m/^[\t ]*("(.*?)"|([\w\#]+))[\t ]*=[\t ]*"(.*)"[\t ]*;[\t ]*(\/\/[\t ]*(.*?))?$/) { # a "key"="value" line | ||||||||||||||||||||||||||||
| 56 | 0 | $orig_str = $4; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | push(@all_hints, $6) if $6; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $key = $2 || $3; | ||||||||||||||||||||||||||
|
Condition0%0/3
$2 || $3
MC/DC0%0/2
$2 || $3
$2
$3
Truth table0%0/3
A || B
A= $2B= $3
| |||||||||||||||||||||||||||||
| 59 | 0 | ($hint, $context) = split(/##/, $key, 2); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # skip placeholder strings | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ($orig_str =~ m/^Placeholder:/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ($lang) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | $translated_text .= $line."\n"; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | next; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | push(@all_hints, $hint) if $hint; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if ($orig_str) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | # remove all hint lines which are equal to the source string | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | @all_hints = map { $_ eq $orig_str ? () : $_ } @all_hints; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | my $str = $orig_str; | |||||||||||||||||||||||||||
| 76 | 0 | $str =~ s/\\"/"/g; | |||||||||||||||||||||||||||
| 77 | 0 | $str =~ s/\\n/\n/g; | |||||||||||||||||||||||||||
| 78 | 0 | $str =~ s/\\\\/\\/g; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | $translated_str = &$callbackref($str, $context, join("\n", @all_hints), undef, $lang, $key); | |||||||||||||||||||||||||||
| 81 | 0 | @all_hints = (); # reset accumulator array | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($lang) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 0 | $translated_str =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 86 | 0 | $translated_str =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 87 | 0 | $translated_str =~ s/"/\\"/g; | |||||||||||||||||||||||||||
| 88 | 0 | $line =~ s/\Q"$orig_str"\E[\t ]*;/"$translated_str";/; | |||||||||||||||||||||||||||
| 89 | 0 | $translated_text .= $line."\n"; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return $translated_text; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | 1; | |||||||||||||||||||||||||||