| 1 | package Serge::Engine::Plugin::serialize_po; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use parent Serge::Engine::Plugin::Base::Serializer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Unicode::Normalize; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Serge; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Serge::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub name { | ||||||||||||||||||||||||||||
| 12 | 0 | ▶ | return '.PO (Gettext) Serializer'; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub serialize { | ||||||||||||||||||||||||||||
| 16 | 8 | ▶ | my ($self, $units, $file, $lang) = @_; | ||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=7 SCAR=20.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | my $locale = locale_from_lang($lang); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # Only show MAJOR.MINOR version in the header to reduce noise | ||||||||||||||||||||||||||||
| 21 | 8 | my $version = $Serge::VERSION; | |||||||||||||||||||||||||||
| 22 | 8 | ▶ | if ($version =~ m/^(\d+\.\d+)(\.\d+)+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | $version = $1; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 8 | my $text = qq|msgid "" | |||||||||||||||||||||||||||
| 27 | msgstr "" | ||||||||||||||||||||||||||||
| 28 | "Content-Type: text/plain; charset=UTF-8\\n" | ||||||||||||||||||||||||||||
| 29 | "Content-Transfer-Encoding: 8bit\\n" | ||||||||||||||||||||||||||||
| 30 | "Language: $locale\\n" | ||||||||||||||||||||||||||||
| 31 | "Generated-By: Serge $version\\n" | ||||||||||||||||||||||||||||
| 32 | |; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 8 | foreach my $unit (@$units) { | |||||||||||||||||||||||||||
| 35 | 54 | $text .= "\n"; # add whitespace before the entry | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # The ordering of the lines matches the Pootle style and is the following: | ||||||||||||||||||||||||||||
| 38 | # # Translator's comments | ||||||||||||||||||||||||||||
| 39 | # #. Developer's comments (hints) | ||||||||||||||||||||||||||||
| 40 | # #: Reference lines | ||||||||||||||||||||||||||||
| 41 | # #, flags | ||||||||||||||||||||||||||||
| 42 | # msgid "..." | ||||||||||||||||||||||||||||
| 43 | # msgctxt "..." | ||||||||||||||||||||||||||||
| 44 | # msgstr "..." | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 54 | ▶ | if ($unit->{comment}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $unit->{comment} =~ s/\n/\n# /sg; | |||||||||||||||||||||||||||
| 48 | 0 | $text .= '# '.$unit->{comment}."\n"; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 54 | my $dev_comment = $unit->{hint}; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 54 | ▶ | if ($dev_comment ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 54 | $dev_comment =~ s/\n/\n#. /sg; | |||||||||||||||||||||||||||
| 55 | 54 | $text .= "#. $dev_comment\n"; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 54 | $text .= "#: File: $file\n"; | |||||||||||||||||||||||||||
| 59 | 54 | $text .= "#: ID: $unit->{key}\n"; | |||||||||||||||||||||||||||
| 60 | 54 | ▶ | $text .= "#, fuzzy\n" if $unit->{fuzzy}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Print the translation entry | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 54 | ▶ | $text .= "msgctxt ".po_wrap($unit->{context})."\n" if $unit->{context} ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 54 | $text .= join("\n", po_serialize_msgid($unit->{source}))."\n"; | |||||||||||||||||||||||||||
| 66 | # for now, just use one `msgstr[0]=""` placeholder for plural strings, | ||||||||||||||||||||||||||||
| 67 | # disregarding the number of plurals supported by a language | ||||||||||||||||||||||||||||
| 68 | 54 | $text .= join("\n", po_serialize_msgstr($unit->{target}, po_is_msgid_plural($unit->{source})))."\n"; | |||||||||||||||||||||||||||
| 69 | } # foreach | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | return $text; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | sub deserialize { | ||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my ($self, $textref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
deserialize: not called CC=27 SCAR=66.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my @units; | |||||||||||||||||||||||||||
| 78 | my $header_skipped; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # scan the file and extracting all the entries | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Sample entry: | ||||||||||||||||||||||||||||
| 83 | # | ||||||||||||||||||||||||||||
| 84 | # # Optional Translator's comment | ||||||||||||||||||||||||||||
| 85 | # #: File: ./_help.pl | ||||||||||||||||||||||||||||
| 86 | # #: ID: 7d150b9e4cd75b658d5f8d89300fd697 | ||||||||||||||||||||||||||||
| 87 | # msgctxt "PageHeading" | ||||||||||||||||||||||||||||
| 88 | # msgid "Help" | ||||||||||||||||||||||||||||
| 89 | # msgstr "Help" | ||||||||||||||||||||||||||||
| 90 | # | ||||||||||||||||||||||||||||
| 91 | # or | ||||||||||||||||||||||||||||
| 92 | # | ||||||||||||||||||||||||||||
| 93 | # #: File: ./_help.pl | ||||||||||||||||||||||||||||
| 94 | # #: ID: 7d150b9e4cd75b658d5f8d89300fd697 | ||||||||||||||||||||||||||||
| 95 | # msgctxt "PageHeading" | ||||||||||||||||||||||||||||
| 96 | # msgid "Help" | ||||||||||||||||||||||||||||
| 97 | # msgstr "" | ||||||||||||||||||||||||||||
| 98 | # "line1\n" | ||||||||||||||||||||||||||||
| 99 | # "line2" | ||||||||||||||||||||||||||||
| 100 | # ... | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # normalize line breaks (Windows->Unix) | ||||||||||||||||||||||||||||
| 103 | 0 | $$textref =~ s/\r\n/\n/sg; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # join multi-line entries | ||||||||||||||||||||||||||||
| 106 | 0 | $$textref =~ s/"\n"//sg; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my @blocks = split(/\n\n/, $$textref); | |||||||||||||||||||||||||||
| 109 | 0 | foreach my $block (@blocks) { | |||||||||||||||||||||||||||
| 110 | 0 | my @lines = split(/\n/, $block); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my @comments; | |||||||||||||||||||||||||||
| 113 | my $key; | ||||||||||||||||||||||||||||
| 114 | 0 | my @strings; | |||||||||||||||||||||||||||
| 115 | 0 | my @translations; | |||||||||||||||||||||||||||
| 116 | 0 | my $context; | |||||||||||||||||||||||||||
| 117 | 0 | my $flags_str; | |||||||||||||||||||||||||||
| 118 | 0 | my $fuzzy = 0; | |||||||||||||||||||||||||||
| 119 | 0 | my $skip_system_comment; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # fix for poedit | ||||||||||||||||||||||||||||
| 122 | my $key_prefix_line; | ||||||||||||||||||||||||||||
| 123 | # end fix for poedit | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 126 | # guard against IME editors inserting bogus unprintable symbols into strings | ||||||||||||||||||||||||||||
| 127 | # that cause rendered resource files to be invalid. | ||||||||||||||||||||||||||||
| 128 | 0 | $line =~ s/[\000-\011\013-\037]//g; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # use Unicode::Normalize to recompose (where possible) + reorder canonically the Unicode string | ||||||||||||||||||||||||||||
| 131 | # this will prevent equivalent Unicode entities from being different in the terms of UTF8 sequences | ||||||||||||||||||||||||||||
| 132 | 0 | $line = NFC($line); | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $skip_system_comment = 1 if ((!$skip_system_comment) && ($line =~ m/^# ===/)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $skip_system_comment and $line =~ /^# ===/
MC/DC0%0/2
not $skip_system_comment and $line =~ /^# ===/
not $skip_system_comment
$line =~ /^# ===/
Truth table0%0/3
not(A) and B
A= not $skip_system_commentB= $line =~ /^# ===/
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | push (@comments, $1) if (!$skip_system_comment && ($line =~ m/^# (.*)$/)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $skip_system_comment and $line =~ /^# (.*)$/
MC/DC0%0/2
not $skip_system_comment and $line =~ /^# (.*)$/
not $skip_system_comment
$line =~ /^# (.*)$/
Truth table0%0/3
not(A) and B
A= not $skip_system_commentB= $line =~ /^# (.*)$/
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # poedit breaks lines like: | ||||||||||||||||||||||||||||
| 138 | # #: ID: xxxxxxxxx | ||||||||||||||||||||||||||||
| 139 | # into two lines: | ||||||||||||||||||||||||||||
| 140 | # #: ID: | ||||||||||||||||||||||||||||
| 141 | # #: xxxxxxxxx | ||||||||||||||||||||||||||||
| 142 | # so we need to handle this in order not to reject such files | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # fix for poedit | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ($key_prefix_line) { # if the previous line was "ID:" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $key = $1 if $line =~ m/^#: (.*)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | $key_prefix_line = ($line =~ m/^#: ID:$/); | |||||||||||||||||||||||||||
| 149 | # end fix for poedit | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $key = $1 if $line =~ m/^#: ID: (.*)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | $strings[0] = $1 if $line =~ m/^msgid "(.*)"$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | $strings[1] = $1 if $line =~ m/^msgid_plural "(.*)"$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | @translations = ($1) if $line =~ m/^msgstr "(.*)"$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | $translations[$1] = ($2) if $line =~ m/^msgstr\[(\d+)\] "(.*)"$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $context = $1 if $line =~ m/^msgctxt "(.*)"$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | $flags_str = $1 if $line =~ m/^#, (.*)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # remove empty trailing comment lines that might have separated user comments from system comments | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | ▶ | while (($#comments >= 0) && ($comments[$#comments] eq '')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$#comments >= 0 and $comments[$#comments] eq ""
MC/DC0%0/2
$#comments >= 0 and $comments[$#comments] eq ""
$#comments >= 0
$comments[$#comments] eq ""
Truth table0%0/3
A and B
A= $#comments >= 0B= $comments[$#comments] eq ""
| |||||||||||||||||||||||||||||
| 163 | 0 | pop @comments; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $string = glue_plural_string(@strings); | |||||||||||||||||||||||||||
| 167 | 0 | my $translation = glue_plural_string(@translations); | |||||||||||||||||||||||||||
| 168 | 0 | my $comment = join("\n", @comments); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 0 | unescape_strref(\$string); | |||||||||||||||||||||||||||
| 171 | 0 | unescape_strref(\$context); | |||||||||||||||||||||||||||
| 172 | 0 | unescape_strref(\$translation); | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # Skip blocks where both translation and comment are not set. If one wants to clear the translation, | ||||||||||||||||||||||||||||
| 175 | # he should at least set the comment (or leave an old one if it existed). | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | next unless ($translation or $comment); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$translation or $comment
MC/DC0%0/2
$translation or $comment
$translation
$comment
Truth table0%0/3
A or B
A= $translationB= $comment
| |||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # Skip blocks with no key defined (e.g. header entry) | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($string eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | if (!$header_skipped) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | $header_skipped = 1; | |||||||||||||||||||||||||||
| 184 | 0 | next; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ($key ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | # if key is defined for an empty string, just warn that and empty item is found | ||||||||||||||||||||||||||||
| 189 | # and continue (previously, the script was not safeguarded against empty strings, | ||||||||||||||||||||||||||||
| 190 | # so there can be such entries which can just be skipped) | ||||||||||||||||||||||||||||
| 191 | 0 | print "\t\t? [empty string] $key\n"; | |||||||||||||||||||||||||||
| 192 | 0 | next; | |||||||||||||||||||||||||||
| 193 | } else { | ||||||||||||||||||||||||||||
| 194 | # but if there is no key set, treat this as a seriously malformed file | ||||||||||||||||||||||||||||
| 195 | 0 | print "ERROR: Malformed entry or header found in the middle of the .po file, skipping the rest of the file\n"; | |||||||||||||||||||||||||||
| 196 | 0 | return; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # sanity check: skip blocks that have no ID defined | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($key eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | print "\t\t? [empty key]\n"; | |||||||||||||||||||||||||||
| 204 | 0 | next; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # sanity check: the extracted key should match the generated one for given string/context | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if ($key ne generate_key($string, $context)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | print "\t\t? [bad key] $key\n"; | |||||||||||||||||||||||||||
| 211 | 0 | next; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # read fuzzy flag | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | my @flags = split(/,[\t ]*/, lc($flags_str)); | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | $fuzzy = is_flag_set(\@flags, 'fuzzy'); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # sanity check: fuzzy flag with empty translation makes no sense | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if (!$translation && $fuzzy) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $translation and $fuzzy
MC/DC0%0/2
not $translation and $fuzzy
not $translation
$fuzzy
Truth table0%0/3
not(A) and B
A= not $translationB= $fuzzy
| |||||||||||||||||||||||||||||
| 223 | 0 | print "\t\t? [empty translation marked as fuzzy] $key\n"; | |||||||||||||||||||||||||||
| 224 | 0 | $fuzzy = 0; # clear the fuzzy flag | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | push @units, { | |||||||||||||||||||||||||||
| 228 | key => $key, | ||||||||||||||||||||||||||||
| 229 | source => $string, | ||||||||||||||||||||||||||||
| 230 | context => $context, | ||||||||||||||||||||||||||||
| 231 | target => $translation, | ||||||||||||||||||||||||||||
| 232 | comment => $comment, # translator's comments | ||||||||||||||||||||||||||||
| 233 | fuzzy => $fuzzy, | ||||||||||||||||||||||||||||
| 234 | flags => \@flags, | ||||||||||||||||||||||||||||
| 235 | }; | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | return \@units; | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 2 | 1; | |||||||||||||||||||||||||||