File Coverage

lib/Serge/Sync/Plugin/TranslationService/crowdin.pm
Criterion Covered Total %
statement 43 44 97.7
branch 10 12 83.3
condition n/a
subroutine 10 10 100.0
pod 0 6 0.0
total 63 72 87.5


line stmt bran cond sub pod time code
1             # ABSTRACT: Crowdin (https://crowdin.com) synchronization plugin for Serge
2              
3             package Serge::Sync::Plugin::TranslationService::crowdin;
4 1     1   15203 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         3  
  1         11  
5              
6 1     1   1877 use strict;
  1         2  
  1         89  
7              
8 1     1   7 use Serge::Util qw(subst_macros);
  1         3  
  1         91  
9 1     1   448 use version;
  1         2310  
  1         7  
10              
11             our $VERSION = qv('0.901.0');
12              
13             sub name {
14 4     4 0 45348 return 'Crowdin translation software (https://crowdin.com) synchronization plugin';
15             }
16              
17             sub init {
18 4     4 0 58 my $self = shift;
19              
20 4         25 $self->SUPER::init(@_);
21              
22 4         29 $self->{optimizations} = 1;
23              
24 4         27 $self->merge_schema({
25             config_file => 'STRING',
26             upload_translations => 'BOOLEAN'
27             });
28             }
29              
30             sub validate_data {
31 4     4 0 2713 my ($self) = @_;
32              
33 4         24 $self->SUPER::validate_data;
34              
35 4         1295 $self->{data}->{config_file} = subst_macros($self->{data}->{config_file});
36 4         182 $self->{data}->{upload_translations} = subst_macros($self->{data}->{upload_translations});
37              
38 4 50       116 die "'config_file' not defined" unless defined $self->{data}->{config_file};
39 4 100       42 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};
40              
41 3 100       122 $self->{data}->{upload_translations} = 1 unless defined $self->{data}->{upload_translations};
42             }
43              
44             sub run_crowdin_cli {
45 8     8 0 24 my ($self, $action, $langs, $capture) = @_;
46              
47 8         19 my $command = $action;
48              
49 8 100       25 if ($langs) {
50 2         12 foreach my $lang (sort @$langs) {
51 4         21 $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR'
  2         10  
52 4         14 $command .= " -l=$lang";
53             }
54             }
55              
56 8         42 $command .= ' --config '.$self->{data}->{config_file};
57              
58 8         69 $command = 'crowdin '.$command;
59 8         253 print "Running '$command'...\n";
60 8         64 return $self->run_cmd($command, $capture);
61             }
62              
63             sub pull_ts {
64 3     3 0 997 my ($self, $langs) = @_;
65              
66 3         17 return $self->run_crowdin_cli('download', $langs);
67             }
68              
69             sub push_ts {
70 3     3 0 1871 my ($self, $langs) = @_;
71              
72 3         10 my $cli_return = $self->run_crowdin_cli('upload sources', ());
73              
74 3 50       172 if ($cli_return != 0) {
75 0         0 return $cli_return;
76             }
77              
78 3 100       18 if ($self->{data}->{upload_translations}) {
79 2         23 $cli_return = $self->run_crowdin_cli('upload translations', $langs);
80             }
81              
82 3         168 return $cli_return;
83             }
84              
85             1;