| 1 | # ABSTRACT: Weblate (https://weblate.org/) synchronization plugin for Serge | ||
| 2 | |||
| 3 | package Serge::Sync::Plugin::TranslationService::weblate; | ||
| 4 | use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use File::chdir; | ||
| 9 | use File::Find qw(find); | ||
| 10 | use File::Spec::Functions qw(catfile abs2rel); | ||
| 11 | use JSON -support_by_pp; # -support_by_pp is used to make Perl on Mac happy | ||
| 12 | use Serge::Util qw(subst_macros); | ||
| 13 | use version; | ||
| 14 | use Scalar::Util qw(reftype); | ||
| 15 | use File::Path qw(make_path); | ||
| 16 | use File::Basename; | ||
| 17 | |||
| 18 | our $VERSION = qv('0.900.3'); | ||
| 19 | |||
| 20 | sub name { | ||
| 21 | return 'Weblate translation software (https://weblate.org/) synchronization plugin'; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub init { | ||
| 25 | my $self = shift; | ||
| 26 | |||
| 27 | $self->SUPER::init(@_); | ||
| 28 | |||
| 29 | $self->{optimizations} = 1; # set to undef to disable optimizations | ||
| 30 | |||
| 31 | $self->merge_schema({ | ||
| 32 | root_directory => 'STRING', | ||
| 33 | project => 'STRING', | ||
| 34 | config_file => 'STRING', | ||
| 35 | languages => 'ARRAY' | ||
| 36 | }); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub validate_data { | ||
| 40 | my ($self) = @_; | ||
| 41 | |||
| 42 | $self->SUPER::validate_data; | ||
| 43 | |||
| 44 | $self->{data}->{root_directory} = subst_macros($self->{data}->{root_directory}); | ||
| 45 | $self->{data}->{project} = subst_macros($self->{data}->{project}); | ||
| 46 | $self->{data}->{config_file} = subst_macros($self->{data}->{config_file}); | ||
| 47 | $self->{data}->{languages} = subst_macros($self->{data}->{languages}); | ||
| 48 | |||
| 49 | die "'root_directory', which is set to '$self->{data}->{root_directory}', does not point to a valid folder." unless -d $self->{data}->{root_directory}; | ||
| 50 | die "'config_file' not defined" unless defined $self->{data}->{config_file}; | ||
| 51 | die "'config_file', which is set to '$self->{data}->{config_file}', does not point to a valid file.\n" unless -f $self->{data}->{config_file}; | ||
| 52 | die "'project' not defined" unless defined $self->{data}->{project}; | ||
| 53 | |||
| 54 | if (!exists $self->{data}->{languages} or scalar(@{$self->{data}->{languages}}) == 0) { | ||
| 55 | die "the list of destination languages is empty"; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | sub pull_ts { | ||
| 60 | my ($self, $langs) = @_; | ||
| 61 | |||
| 62 | my $langs_to_push = $self->get_all_langs($langs); | ||
| 63 | my %files = $self->translation_files($langs_to_push); | ||
| 64 | |||
| 65 | foreach my $key (sort keys %files) { | ||
| 66 | my $file = $files{$key}; | ||
| 67 | my $full_path = catfile($self->{data}->{root_directory}, $file); | ||
| 68 | |||
| 69 | my ($file_name,$folder_path,$file_suffix) = fileparse($full_path); | ||
| 70 | |||
| 71 | if (!(-d $folder_path)) { | ||
| 72 | make_path($folder_path); | ||
| 73 | } | ||
| 74 | |||
| 75 | my $cli_return = $self->run_weblate_cli('download --output "'.$file.'" '.$key, 0); | ||
| 76 | |||
| 77 | if ($cli_return != 0) { | ||
| 78 | return $cli_return; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub push_ts { | ||
| 86 | my ($self, $langs) = @_; | ||
| 87 | |||
| 88 | my $langs_to_push = $self->get_all_langs($langs); | ||
| 89 | my %files = $self->translation_files($langs_to_push); | ||
| 90 | |||
| 91 | foreach my $key (sort keys %files) { | ||
| 92 | my $file = $files{$key}; | ||
| 93 | my $full_path = catfile($self->{data}->{root_directory}, $file); | ||
| 94 | |||
| 95 | if (-f $full_path) { | ||
| 96 | my $command = 'upload --overwrite --input "' . $file . '"'; | ||
| 97 | $command .= ' --method replace'; | ||
| 98 | if ($self->{data}->{fuzzy}) { | ||
| 99 | $command .= ' --fuzzy '.$self->{data}->{fuzzy}; | ||
| 100 | } | ||
| 101 | $command .= ' '.$key; | ||
| 102 | |||
| 103 | my $cli_return = $self->run_weblate_cli($command, 0); | ||
| 104 | |||
| 105 | if ($cli_return != 0) { | ||
| 106 | return $cli_return; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub translation_files { | ||
| 115 | my ($self, $langs) = @_; | ||
| 116 | |||
| 117 | my $json_components = $self->run_weblate_cli('--format json list-components '.$self->{data}->{project}, 1); | ||
| 118 | |||
| 119 | my $json_components_tree = $self->parse_json($json_components); | ||
| 120 | my $json_components_list = $self->parse_list($json_components_tree); | ||
| 121 | my @components = map { $_->{slug} } @$json_components_list; | ||
| 122 | |||
| 123 | my %translations = (); | ||
| 124 | |||
| 125 | my %langs_hash = map { $_ => 1 } @$langs; | ||
| 126 | |||
| 127 | foreach my $component (@components) { | ||
| 128 | my $json_translations = $self->run_weblate_cli('--format json list-translations '.$self->{data}->{project}.'/'.$component, 1); | ||
| 129 | |||
| 130 | my $json_translations_tree = $self->parse_json($json_translations); | ||
| 131 | my $json_translations_list = $self->parse_list($json_translations_tree); | ||
| 132 | |||
| 133 | foreach my $translation (@$json_translations_list) { | ||
| 134 | my $language = $translation->{language}->{code}; | ||
| 135 | my $filename = $translation->{filename}; | ||
| 136 | my $language_code = $translation->{language_code}; | ||
| 137 | |||
| 138 | if (exists $langs_hash{$language_code}) { | ||
| 139 | $translations{$self->{data}->{project}.'/'.$component.'/'.$language} = $filename; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | return %translations; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub get_all_langs { | ||
| 148 | my ($self, $langs) = @_; | ||
| 149 | |||
| 150 | if (!$langs) { | ||
| 151 | $langs = $self->{data}->{languages}; | ||
| 152 | } | ||
| 153 | |||
| 154 | my @all_langs = (); | ||
| 155 | |||
| 156 | push @all_langs, @$langs; | ||
| 157 | |||
| 158 | return \@all_langs; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub run_weblate_cli { | ||
| 162 | my ($self, $action, $capture) = @_; | ||
| 163 | |||
| 164 | my $cli_return = 0; | ||
| 165 | |||
| 166 | my $command = 'wlc --config '.$self->{data}->{config_file}.' '.$action; | ||
| 167 | |||
| 168 | print "Running $action ...\n"; | ||
| 169 | |||
| 170 | my $json_response = $self->run_in($self->{data}->{root_directory}, $command, 1); | ||
| 171 | |||
| 172 | if ($capture) { | ||
| 173 | return $json_response; | ||
| 174 | } | ||
| 175 | |||
| 176 | if ($json_response == '') { | ||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | return $self->parse_result($json_response); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub parse_result { | ||
| 184 | my ($self, $json_response) = @_; | ||
| 185 | |||
| 186 | my $json_tree = $self->parse_json($json_response); | ||
| 187 | |||
| 188 | if (reftype($json_tree) == 'ARRAY') { | ||
| 189 | return 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $result = $json_tree->{result}; | ||
| 193 | |||
| 194 | if ($result == 'true') { | ||
| 195 | return 0; | ||
| 196 | } | ||
| 197 | |||
| 198 | return 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub parse_list { | ||
| 202 | my ($self, $json_tree) = @_; | ||
| 203 | |||
| 204 | if (reftype($json_tree) == 'ARRAY') { | ||
| 205 | return $json_tree; | ||
| 206 | } | ||
| 207 | |||
| 208 | return $json_tree->{results}; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub find_lang_files { | ||
| 212 | my ($self, $directory) = @_; | ||
| 213 | |||
| 214 | my @files = (); | ||
| 215 | |||
| 216 | find(sub { | ||
| 217 | push @files, abs2rel($File::Find::name, $directory) if(-f $_); | ||
| 218 | }, $directory); | ||
| 219 | |||
| 220 | return @files; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub parse_json { | ||
| 224 | my ($self, $json) = @_; | ||
| 225 | |||
| 226 | my $tree; | ||
| 227 | eval { | ||
| 228 | ($tree) = from_json($json, {relaxed => 1}); | ||
| 229 | }; | ||
| 230 | if ($@ || !$tree) { | ||
| 231 | my $error_text = $@; | ||
| 232 | if ($error_text) { | ||
| 233 | $error_text =~ s/\t/ /g; | ||
| 234 | $error_text =~ s/^\s+//s; | ||
| 235 | } else { | ||
| 236 | $error_text = "from_json() returned empty data structure"; | ||
| 237 | } | ||
| 238 | |||
| 239 | die $error_text; | ||
| 240 | } | ||
| 241 | |||
| 242 | return $tree; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub get_langs { | ||
| 246 | my ($self, $langs) = @_; | ||
| 247 | |||
| 248 | if (!$langs) { | ||
| 249 | $langs = $self->{data}->{languages}; | ||
| 250 | } | ||
| 251 | |||
| 252 | return $langs; | ||
| 253 | } | ||
| 254 | |||
| 255 | 1; |