| 1 | package Serge::Engine::Plugin::parse_ts; | ||
| 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 | 1 | ▶ | no warnings qw(uninitialized); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use File::Path; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Serge::Mail; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Serge::Util qw(full_locale_from_lang xml_escape_strref xml_unescape_strref); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use XML::Twig; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | sub name { | ||
| 14 | return 'Qt Linguist TS parser plugin'; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | $self->SUPER::init(@_); | ||
| 21 | |||
| 22 | $self->{errors} = {}; | ||
| 23 | |||
| 24 | $self->merge_schema({ | ||
| 25 | email_from => 'STRING', | ||
| 26 | email_to => 'ARRAY', | ||
| 27 | email_subject => 'STRING', | ||
| 28 | }); | ||
| 29 | |||
| 30 | $self->add('after_job', \&report_errors); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub validate_data { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | $self->SUPER::validate_data; | ||
| 37 | |||
| 38 | if (!defined $self->{data}->{email_from}) { | ||
| 39 | print "WARNING: 'email_from' is not defined. Will skip sending any reports.\n"; | ||
| 40 | } | ||
| 41 | |||
| 42 | if (!defined $self->{data}->{email_to}) { | ||
| 43 | print "WARNING: 'email_to' is not defined. Will skip sending any reports.\n"; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | sub report_errors { | ||
| 48 | my ($self, $phase) = @_; | ||
| 49 | |||
| 50 | my $email_from = $self->{data}->{email_from}; | ||
| 51 | if (!$email_from) { | ||
| 52 | $self->{errors} = {}; | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | my $email_to = $self->{data}->{email_to}; | ||
| 57 | if (!$email_to) { | ||
| 58 | $self->{errors} = {}; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $email_subject = $self->{data}->{email_subject} || ("[".$self->{parent}->{id}.']: .TS Parse Errors'); | ||
| 63 | |||
| 64 | my $text; | ||
| 65 | foreach my $key (sort keys %{$self->{errors}}) { | ||
| 66 | my $pre_contents = $self->{errors}->{$key}; | ||
| 67 | xml_escape_strref(\$pre_contents); | ||
| 68 | $text .= "<hr />\n<p><b style='color: red'>$key</b> <pre>".$pre_contents."</pre></p>\n"; | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->{errors} = {}; | ||
| 72 | |||
| 73 | if ($text) { | ||
| 74 | $text = qq| | ||
| 75 | <html> | ||
| 76 | <head> | ||
| 77 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
| 78 | </head> | ||
| 79 | <body style="font-family: sans-serif; font-size: 120%"> | ||
| 80 | |||
| 81 | <p> | ||
| 82 | # This is an automatically generated message. | ||
| 83 | |||
| 84 | The following parsing errors were found when attempting to localize resource files. | ||
| 85 | </p> | ||
| 86 | |||
| 87 | $text | ||
| 88 | |||
| 89 | </body> | ||
| 90 | </html> | ||
| 91 | |; | ||
| 92 | |||
| 93 | Serge::Mail::send_html_message( | ||
| 94 | $email_from, # from | ||
| 95 | $email_to, # to (list) | ||
| 96 | $email_subject, # subject | ||
| 97 | $text # message body | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub parse { | ||
| 103 | my ($self, $textref, $callbackref, $lang) = @_; | ||
| 104 | |||
| 105 | die 'callbackref not specified' unless $callbackref; | ||
| 106 | |||
| 107 | my $tree; | ||
| 108 | eval { | ||
| 109 | $tree = XML::Twig->new()->parse($$textref); | ||
| 110 | $tree->set_indent(' ' x 4); | ||
| 111 | }; | ||
| 112 | if ($@) { | ||
| 113 | my $error_text = $@; | ||
| 114 | $error_text =~ s/\t/ /g; | ||
| 115 | $error_text =~ s/^\s+//s; | ||
| 116 | |||
| 117 | $self->{errors}->{$self->{parent}->{engine}->{current_file_rel}} = $error_text; | ||
| 118 | |||
| 119 | die $error_text; | ||
| 120 | } | ||
| 121 | |||
| 122 | $self->parse_tree($tree, $callbackref, $lang); | ||
| 123 | |||
| 124 | return $lang ? $tree->sprint(pretty_print => 'indented') : undef; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub parse_tree { | ||
| 128 | my ($self, $tree, $callbackref, $lang) = @_; | ||
| 129 | |||
| 130 | my @units = $tree->findnodes('//message'); | ||
| 131 | map { | ||
| 132 | $self->parse_message($_, $callbackref, $lang); | ||
| 133 | } @units; | ||
| 134 | |||
| 135 | # update 'language' attribute on <TS> tag | ||
| 136 | if ($lang) { | ||
| 137 | $tree->root->set_att('language' => &full_locale_from_lang($lang)); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | sub parse_message { | ||
| 142 | my ($self, $node, $callbackref, $lang) = @_; | ||
| 143 | |||
| 144 | my $has_source = $node->has_child('source') && $node->first_child('source')->text ne ''; | ||
| 145 | my $has_translation = $node->has_child('translation') && $node->first_child('translation')->text ne ''; | ||
| 146 | |||
| 147 | # in import mode, try to get translations from <translation> tag | ||
| 148 | # and skip units without such tag; also skip nodes with an empty source (for sanity) | ||
| 149 | |||
| 150 | return if ($self->{import_mode} && (!$has_translation || !$has_source)); | ||
| 151 | |||
| 152 | my $source = $node->first_child('source')->text; | ||
| 153 | my $translation = $node->first_child('translation')->text; | ||
| 154 | my $context = $self->get_context($node); | ||
| 155 | my $hint = $self->get_hint($node); | ||
| 156 | my $key = $context.':'.$node->first_child('source')->text; | ||
| 157 | |||
| 158 | my $text = ($self->{import_mode} && $has_translation) ? $translation : $source; | ||
| 159 | |||
| 160 | my $translation = &$callbackref($text, $context, $hint, undef, $lang, $key); | ||
| 161 | |||
| 162 | if ($lang) { | ||
| 163 | my $el = $node->first_child('translation'); | ||
| 164 | $el->set_text($translation); | ||
| 165 | $el->strip_att('type'); # remove type attr (e.g. "unfinished") | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | sub get_context { | ||
| 170 | my ($self, $node) = @_; | ||
| 171 | |||
| 172 | my @ctx; | ||
| 173 | |||
| 174 | my $parent = $node->parent; | ||
| 175 | if ($parent->tag eq 'context') { | ||
| 176 | push @ctx, $parent->first_child('name')->text; | ||
| 177 | } | ||
| 178 | |||
| 179 | # according to Qt Linguist TS file format spec, | ||
| 180 | # (http://doc.qt.io/qt-5/linguist-ts-file-format.html) | ||
| 181 | # <extracomment> is used to hold a comment, and <comment> | ||
| 182 | # is used to hold the context (sic!) | ||
| 183 | |||
| 184 | my $comment_node = $node->first_child('comment'); | ||
| 185 | if ($comment_node) { | ||
| 186 | push @ctx, $comment_node->text; | ||
| 187 | } | ||
| 188 | |||
| 189 | return length(@ctx) > 0 ? join("\n", @ctx) : ''; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub get_hint { | ||
| 193 | my ($self, $node) = @_; | ||
| 194 | |||
| 195 | # see the note above on <comment> vs <extracomment> | ||
| 196 | |||
| 197 | my $comment_node = $node->first_child('extracomment'); | ||
| 198 | if ($comment_node) { | ||
| 199 | return $comment_node->text; | ||
| 200 | } | ||
| 201 | |||
| 202 | return ''; | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||