| 1 | package Serge::Engine::Plugin::control_commands; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use parent Serge::Plugin::Base::Callback; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Serge::Util qw(subst_macros); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | sub name { | ||||||||||||||||||||||||||||
| 10 | 0 | ▶ | return "Do actions based on commands provided in translator's comments"; | ||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Examples: | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # Set (or replace existing) extra comment for the entire item (all language-specific units); | ||||||||||||||||||||||||||||
| 16 | # Text is optional. using just `@` clears the extra comment | ||||||||||||||||||||||||||||
| 17 | # @ [Text] | ||||||||||||||||||||||||||||
| 18 | # | ||||||||||||||||||||||||||||
| 19 | # Append extra comment paragraph (`\n\n` + text) for the entire item (all language-specific units) | ||||||||||||||||||||||||||||
| 20 | # + <Text> | ||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||
| 22 | # Add (append) `#tag1` to the end of the text; remove `#tag2` | ||||||||||||||||||||||||||||
| 23 | # #tag1 -#tag2 | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # Skip string (mark as skipped in Serge database and remove from all .po files) | ||||||||||||||||||||||||||||
| 26 | # @skip | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Rewrite all translations for the same source string with the provided translation value. | ||||||||||||||||||||||||||||
| 29 | # If translation is empty, this will simply remove the translation | ||||||||||||||||||||||||||||
| 30 | # @rewrite_all | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Rewrite all translations for the same source string with the provided value | ||||||||||||||||||||||||||||
| 33 | # and mark translations as fuzzy. If the translation is empty, this has the same effect | ||||||||||||||||||||||||||||
| 34 | # as @rewrite_all (because empty translations can't be fuzzy) | ||||||||||||||||||||||||||||
| 35 | # @rewrite_all_as_fuzzy | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub init { | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | $self->SUPER::init(@_); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | $self->merge_schema({ | |||||||||||||||||||||||||||
| 43 | set_fuzzy_on_comment_change => 'BOOLEAN', | ||||||||||||||||||||||||||||
| 44 | }); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | $self->add({ | |||||||||||||||||||||||||||
| 47 | rewrite_parsed_ts_file_item => \&rewrite_parsed_ts_file_item | ||||||||||||||||||||||||||||
| 48 | }); | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub validate_data { | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
validate_data: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $self->SUPER::validate_data; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $self->{data}->{set_fuzzy_on_comment_change} = 1 unless exists $self->{data}->{set_fuzzy_on_comment_change}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub rewrite_parsed_ts_file_item { | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | my ($self, $phase, $file, $lang, $item_id, $strref, $flagsref, | ||||||||||||||||||||||||||
|
Subroutine
rewrite_parsed_ts_file_item: not called CC=27 SCAR=66.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | $translationref, $commentref, $fuzzyref, $item_commentref) = @_; | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my $s = $$commentref; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Set (or replace existing) extra comment for the entire item (all language-specific units); | ||||||||||||||||||||||||||||
| 66 | # Text is optional. using just `@` clears the extra comment | ||||||||||||||||||||||||||||
| 67 | # @ [Text] | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ($s =~ m/^@(\s+(.*?))?$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | my $value = $2; | |||||||||||||||||||||||||||
| 70 | 0 | $$commentref = undef; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | preprocess_value(\$value); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | print "COMMAND: replace item comment: '$value'\n"; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($self->{data}->{set_fuzzy_on_comment_change} && ($value ne '')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"data"}{"set_fuzzy_on_comment_change"} and $value ne ""
MC/DC0%0/2
$self->{"data"}{"set_fuzzy_on_comment_change"} and $value ne ""
$self->{"data"}{"set_fuzzy_on_comment_change"}
$value ne ""
Truth table0%0/3
A and B
A= $self->{"data"}{"set_fuzzy_on_comment_change"}B= $value ne ""
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $$fuzzyref = 1 if $$translationref ne ''; # for the current unit to match the state in the db | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | $self->set_fuzzy_for_all_translations($item_id); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | $$item_commentref = $value; | |||||||||||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Append extra comment paragraph (`\n\n` + text) for the entire item (all language-specific units) | ||||||||||||||||||||||||||||
| 86 | # + <Text> | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($s =~ m/^\+\s+(.*)$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | my $value = $1; | |||||||||||||||||||||||||||
| 89 | 0 | $$commentref = undef; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | preprocess_value(\$value); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | print "COMMAND: append item comment: '$value'\n"; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($self->{data}->{set_fuzzy_on_comment_change} && ($value ne '')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->{"data"}{"set_fuzzy_on_comment_change"} and $value ne ""
MC/DC0%0/2
$self->{"data"}{"set_fuzzy_on_comment_change"} and $value ne ""
$self->{"data"}{"set_fuzzy_on_comment_change"}
$value ne ""
Truth table0%0/3
A and B
A= $self->{"data"}{"set_fuzzy_on_comment_change"}B= $value ne ""
| |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | $$fuzzyref = 1 if $$translationref ne ''; # for the current unit to match the state in the db | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $self->set_fuzzy_for_all_translations($item_id); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $item_props = $self->{parent}->{engine}->{db}->get_item_props($item_id); | |||||||||||||||||||||||||||
| 101 | 0 | my $comment = $item_props->{comment}; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $comment .= "\n\n" if defined $comment && ($value ne ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $comment and $value ne ""
MC/DC0%0/2
defined $comment and $value ne ""
defined $comment
$value ne ""
Truth table0%0/3
A and B
A= defined $commentB= $value ne ""
| |||||||||||||||||||||||||||||
| 104 | 0 | $comment .= $value; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | $$item_commentref = $comment; | |||||||||||||||||||||||||||
| 107 | 0 | return; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Rewrite all translations of the same string in the database | ||||||||||||||||||||||||||||
| 111 | # with the provided translation | ||||||||||||||||||||||||||||
| 112 | # @rewrite_all | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ($s =~ m/^\@(rewrite_all|rewrite_all_as_fuzzy)$/s) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | print "COMMAND: replace all translations for '$$strref' with '$$translationref'\n"; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | my $tr = $$translationref; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | $tr = undef if $tr eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | $$commentref = undef; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # if translation itself is empty, we can't simply leave both translation | ||||||||||||||||||||||||||||
| 122 | # and comment empty, otherwise this situation will be treated as 'no translation' | ||||||||||||||||||||||||||||
| 123 | # and translations will be substituted again; thus, in this case, we set | ||||||||||||||||||||||||||||
| 124 | # comment to some meaningful value which is not further treated as a command | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | $$commentref = 'Translation removed' unless defined $tr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | my $where_tr = defined $tr ? 't.string != ?' : 't.string IS NOT NULL'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $sqlquery = " | |||||||||||||||||||||||||||
| 130 | SELECT t.id | ||||||||||||||||||||||||||||
| 131 | FROM strings s | ||||||||||||||||||||||||||||
| 132 | JOIN items i ON i.string_id = s.id | ||||||||||||||||||||||||||||
| 133 | JOIN translations t ON t.item_id = i.id | ||||||||||||||||||||||||||||
| 134 | WHERE s.string = ? | ||||||||||||||||||||||||||||
| 135 | AND t.language = ? | ||||||||||||||||||||||||||||
| 136 | AND $where_tr | ||||||||||||||||||||||||||||
| 137 | AND i.id != ? | ||||||||||||||||||||||||||||
| 138 | "; | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | my $sth = $self->{parent}->{engine}->{db}->prepare($sqlquery); | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $n = 1; | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | $sth->bind_param($n++, $$strref) || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $sth->bind_param($n++, $lang) || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (defined $tr) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | $sth->bind_param($n++, $tr) || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | # skip the item itself, since its translation will be set anyway | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | $sth->bind_param($n++, $item_id) || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | $sth->execute || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | my @translation_ids; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | while (my $hr = $sth->fetchrow_hashref()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | push @translation_ids, $hr->{id}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 0 | $sth->finish; | |||||||||||||||||||||||||||
| 157 | 0 | $sth = undef; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (scalar @translation_ids > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | my $extra_msg = $s eq '@rewrite_all_as_fuzzy' ? ' + set fuzzy flag' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | print "Found ", scalar @translation_ids, " translations to replace$extra_msg\n"; | |||||||||||||||||||||||||||
| 162 | 0 | foreach my $id (@translation_ids) { | |||||||||||||||||||||||||||
| 163 | 0 | my $props = $self->{parent}->{engine}->{db}->get_translation_props($id); | |||||||||||||||||||||||||||
| 164 | 0 | print "\tTranslation $id (was: '$props->{string}')\n"; | |||||||||||||||||||||||||||
| 165 | 0 | ▶ | $self->{parent}->{engine}->{db}->update_translation_props($id, { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$s eq "\@rewrite_all_as_fuzzy" and defined $tr
MC/DC0%0/2
$s eq "\@rewrite_all_as_fuzzy" and defined $tr
$s eq "\@rewrite_all_as_fuzzy"
defined $tr
Truth table0%0/3
A and B
A= $s eq "\@rewrite_all_as_fuzzy"B= defined $tr
| |||||||||||||||||||||||||||||
| 166 | string => $tr, | ||||||||||||||||||||||||||||
| 167 | comment => $$commentref, | ||||||||||||||||||||||||||||
| 168 | fuzzy => $s eq '@rewrite_all_as_fuzzy' && defined $tr ? 1 : 0, | ||||||||||||||||||||||||||||
| 169 | merge => 1 | ||||||||||||||||||||||||||||
| 170 | }); | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } else { | ||||||||||||||||||||||||||||
| 173 | 0 | print "No translations to replace\n"; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub set_fuzzy_for_all_translations { | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | my ($self, $item_id) = @_; | ||||||||||||||||||||||||||
|
Subroutine
set_fuzzy_for_all_translations: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | my $sqlquery = " | |||||||||||||||||||||||||||
| 182 | SELECT id | ||||||||||||||||||||||||||||
| 183 | FROM translations | ||||||||||||||||||||||||||||
| 184 | WHERE item_id = ? | ||||||||||||||||||||||||||||
| 185 | AND string IS NOT NULL | ||||||||||||||||||||||||||||
| 186 | AND string != '' | ||||||||||||||||||||||||||||
| 187 | AND fuzzy = 0; | ||||||||||||||||||||||||||||
| 188 | "; | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | my $sth = $self->{parent}->{engine}->{db}->prepare($sqlquery); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | $sth->bind_param(1, $item_id) || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | $sth->execute || die $sth->errstr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | my @translation_ids; | |||||||||||||||||||||||||||
| 196 | 0 | ▶ | while (my $hr = $sth->fetchrow_hashref()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 0 | push @translation_ids, $hr->{id}; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 0 | $sth->finish; | |||||||||||||||||||||||||||
| 200 | 0 | $sth = undef; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if (scalar @translation_ids > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 0 | print "Found ", scalar @translation_ids, " translations to set fuzzy flag for\n"; | |||||||||||||||||||||||||||
| 204 | 0 | foreach my $id (@translation_ids) { | |||||||||||||||||||||||||||
| 205 | 0 | print "\tSetting fuzzy flag for translation $id\n"; | |||||||||||||||||||||||||||
| 206 | 0 | $self->{parent}->{engine}->{db}->update_translation_props($id, { | |||||||||||||||||||||||||||
| 207 | fuzzy => 1 | ||||||||||||||||||||||||||||
| 208 | }); | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } else { | ||||||||||||||||||||||||||||
| 211 | 0 | print "No translations to set fuzzy flag for\n"; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | sub preprocess_value { | ||||||||||||||||||||||||||||
| 216 | 0 | ▶ | my ($sref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
preprocess_value: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | # TODO: move this replacement logic into plugin | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # replace links like: | ||||||||||||||||||||||||||||
| 221 | # https://www.dropbox.com/s/xxxxxx/foo.png?dl=0 | ||||||||||||||||||||||||||||
| 222 | # with: | ||||||||||||||||||||||||||||
| 223 | # https://dl.dropboxusercontent.com/s/xxxxxx/foo.png | ||||||||||||||||||||||||||||
| 224 | # | ||||||||||||||||||||||||||||
| 225 | # (see http://ryanmo.co/2013/11/03/dropboxsharedlinks/) | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 0 | $$sref =~ s|https://www\.dropbox\.com/s/(.*?)\?dl=0|https://dl.dropboxusercontent.com/s/$1|g; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 1 | 1; | |||||||||||||||||||||||||||