| 1 | package Serge::Engine::Plugin::parse_android; | ||||||||||
| 2 | 2 | ▶ | use parent Serge::Engine::Plugin::Base::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | no warnings qw(uninitialized); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | sub name { | ||||||||||
| 9 | 0 | ▶ | return 'Android strings.xml parser plugin'; | ||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 10 | } | ||||||||||
| 11 | |||||||||||
| 12 | sub parse { | ||||||||||
| 13 | 14 | ▶ | my ($self, $textref, $callbackref, $lang) = @_; | ||||||||
|
Subroutine
parse: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 14 | |||||||||||
| 15 | 14 | ▶ | die 'callbackref not specified' unless $callbackref; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | |||||||||||
| 17 | # Finding translatable strings in file | ||||||||||
| 18 | |||||||||||
| 19 | # Format is: | ||||||||||
| 20 | # | ||||||||||
| 21 | # <string name="...hint..." ...>...string...</string> | ||||||||||
| 22 | # | ||||||||||
| 23 | # or: | ||||||||||
| 24 | # | ||||||||||
| 25 | # <string-array name="...hint..." ...> | ||||||||||
| 26 | # <item>...string...</item> | ||||||||||
| 27 | # ... | ||||||||||
| 28 | # </string-array> | ||||||||||
| 29 | # | ||||||||||
| 30 | # or: | ||||||||||
| 31 | # | ||||||||||
| 32 | # <plurals name="...hint1..." ...> | ||||||||||
| 33 | # <item quantity="...hint2...">...string...</item> | ||||||||||
| 34 | # ... | ||||||||||
| 35 | # </plurals> | ||||||||||
| 36 | |||||||||||
| 37 | 14 | my $translated_text = $$textref; | |||||||||
| 38 | |||||||||||
| 39 | 14 | $translated_text =~ s|(<string [^\/\<\>]*?>)(.*?)(</string>)|$1.$self->parse_string_callback($callbackref, $1, $2, $lang).$3|sge; | |||||||||
| 40 | 14 | $translated_text =~ s|(<plurals [^\/\<\>]*?>)(.*?)(</plurals>)|$1.$self->parse_plurals_callback($callbackref, $1, $2, $lang).$3|sge; | |||||||||
| 41 | 14 | $translated_text =~ s|(<string-array [^\/\<\>]*?>)(.*?)(</string-array>)|$1.$self->parse_string_array_callback($callbackref, $1, $2, $lang).$3|sge; | |||||||||
| 42 | |||||||||||
| 43 | 14 | return $translated_text; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub parse_string_callback { | ||||||||||
| 47 | 98 | ▶ | my ($self, $callbackref, $opening_tag, $inner_xml, $lang) = @_; | ||||||||
|
Subroutine
parse_string_callback: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||
| 48 | |||||||||||
| 49 | 98 | ▶ | return $inner_xml if $opening_tag =~ m|\btranslatable="false"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 98 | ▶ | my $key = $1 if $opening_tag =~ m|\bname="(.*?)"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 98 | return $self->parse_callback($callbackref, $inner_xml, undef, $key, undef, $lang, $key); | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | sub parse_plurals_callback { | ||||||||||
| 56 | 0 | ▶ | my ($self, $callbackref, $opening_tag, $inner_xml, $lang) = @_; | ||||||||
|
Subroutine
parse_plurals_callback: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | return $inner_xml if $opening_tag =~ m|\btranslatable="false"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | ▶ | my $key = $1 if $opening_tag =~ m|\bname="(.*?)"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | |||||||||||
| 61 | 0 | $inner_xml =~ s|(<item .*?quantity=")(.*?)(".*?>)(.*?)(</item>)|$1.$2.$3.$self->parse_callback($callbackref, $4, undef, "$key:$2", undef, $lang, "$key:$2").$5|sge; | |||||||||
| 62 | 0 | return $inner_xml; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | sub parse_string_array_callback { | ||||||||||
| 66 | 0 | ▶ | my ($self, $callbackref, $opening_tag, $inner_xml, $lang) = @_; | ||||||||
|
Subroutine
parse_string_array_callback: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | return $inner_xml if $opening_tag =~ m|\btranslatable="false"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | ▶ | my $key = $1 if $opening_tag =~ m|\bname="(.*?)"|; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 0 | my $index = 0; | |||||||||
| 72 | 0 | $inner_xml =~ s|(<item>)(.*?)(</item>)|$1.$self->parse_callback($callbackref, $2, undef, "$key:item", undef, $lang, $key.':'.$index++).$3|sge; | |||||||||
| 73 | 0 | return $inner_xml; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | sub process_non_xml_chunks { | ||||||||||
| 77 | 196 | ▶ | my ($s, $callback) = @_; | ||||||||
|
Subroutine
process_non_xml_chunks: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 78 | |||||||||||
| 79 | 196 | my @chunks = split(/(<.*?>)/, $s); | |||||||||
| 80 | 196 | my $out = ''; | |||||||||
| 81 | 196 | my $translate; | |||||||||
| 82 | |||||||||||
| 83 | 196 | foreach my $chunk (@chunks) { | |||||||||
| 84 | 196 | $translate = !$translate; | |||||||||
| 85 | 196 | ▶ | &$callback(\$chunk) if $translate; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 196 | $out .= $chunk; | |||||||||
| 87 | } | ||||||||||
| 88 | 196 | return $out; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | sub unescape_callback { | ||||||||||
| 92 | 98 | ▶ | my $strref = shift; | ||||||||
|
Subroutine
unescape_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 93 | |||||||||||
| 94 | 98 | $$strref =~ s/\\'/'/g; # Android-specific apostrophe unescaping | |||||||||
| 95 | 98 | $$strref =~ s/\\"/"/g; # Android-specific quote unescaping | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub escape_callback { | ||||||||||
| 99 | 98 | ▶ | my $strref = shift; | ||||||||
|
Subroutine
escape_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 100 | |||||||||||
| 101 | 98 | $$strref =~ s/'/\\'/g; # Android-specific apostrophe escaping | |||||||||
| 102 | 98 | $$strref =~ s/"/\\"/g; # Android-specific quote escaping | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | sub parse_callback { | ||||||||||
| 106 | 98 | ▶ | my ($self, $callbackref, $string, $context, $hint, $flagsref, $lang, $key) = @_; | ||||||||
|
Subroutine
parse_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 107 | |||||||||||
| 108 | 98 | $string = process_non_xml_chunks($string, \&unescape_callback); | |||||||||
| 109 | |||||||||||
| 110 | 98 | my $translated_string = &$callbackref($string, $context, $hint, $flagsref, $lang, $key); | |||||||||
| 111 | |||||||||||
| 112 | 98 | $translated_string = process_non_xml_chunks($translated_string, \&escape_callback); | |||||||||
| 113 | |||||||||||
| 114 | 98 | return $translated_string; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 2 | 1; | |||||||||