File Coverage

lib/Serge/Sync/Plugin/TranslationService/crowdin.pm
Criterion Covered Total %
statement 56 57 98.2
branch 22 24 91.6
condition n/a
subroutine 10 10 100.0
pod 0 6 0.0
total 88 97 90.7


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   15807 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         3  
  1         8  
5              
6 1     1   1925 use strict;
  1         3  
  1         35  
7              
8 1     1   6 use Serge::Util qw(subst_macros);
  1         3  
  1         86  
9 1     1   460 use version;
  1         2038  
  1         6  
10              
11             our $VERSION = qv('0.902.0');
12              
13             sub name {
14 5     5 0 59174 return 'Crowdin translation software (https://crowdin.com) synchronization plugin';
15             }
16              
17             sub init {
18 5     5 0 62 my $self = shift;
19              
20 5         28 $self->SUPER::init(@_);
21              
22 5         34 $self->{optimizations} = 1;
23              
24 5         37 $self->merge_schema({
25             config_file => 'STRING',
26             upload_translations => 'BOOLEAN',
27             import_duplicates => 'BOOLEAN',
28             import_eq_suggestions => 'BOOLEAN',
29             auto_approve_imported => 'BOOLEAN',
30             });
31             }
32              
33             sub validate_data {
34 5     5 0 5343 my ($self) = @_;
35              
36 5         24 $self->SUPER::validate_data;
37              
38 5         1970 $self->{data}->{config_file} = subst_macros($self->{data}->{config_file});
39 5         179 $self->{data}->{upload_translations} = subst_macros($self->{data}->{upload_translations});
40 5         148 $self->{data}->{import_duplicates} = subst_macros($self->{data}->{import_duplicates});
41 5         148 $self->{data}->{import_eq_suggestions} = subst_macros($self->{data}->{import_eq_suggestions});
42 5         143 $self->{data}->{auto_approve_imported} = subst_macros($self->{data}->{auto_approve_imported});
43              
44 5 50       143 die "'config_file' not defined" unless defined $self->{data}->{config_file};
45 5 100       49 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};
46              
47 4 100       141 $self->{data}->{upload_translations} = 1 unless defined $self->{data}->{upload_translations};
48 4 100       75 $self->{data}->{import_duplicates} = 0 unless defined $self->{data}->{import_duplicates};
49 4 100       67 $self->{data}->{import_eq_suggestions} = 0 unless defined $self->{data}->{import_eq_suggestions};
50 4 100       65 $self->{data}->{auto_approve_imported} = 0 unless defined $self->{data}->{auto_approve_imported};
51             }
52              
53             sub run_crowdin_cli {
54 11     11 0 36 my ($self, $action, $langs, $capture) = @_;
55              
56 11         22 my $command = $action;
57              
58 11 100       31 if ($langs) {
59 2         19 foreach my $lang (sort @$langs) {
60 4         19 $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR'
  2         10  
61 4         14 $command .= " -l=$lang";
62             }
63             }
64              
65 11         56 $command .= ' --config '.$self->{data}->{config_file};
66              
67 11         98 $command = 'crowdin '.$command;
68 11         339 print "Running '$command'...\n";
69 11         78 return $self->run_cmd($command, $capture);
70             }
71              
72             sub pull_ts {
73 4     4 0 1012 my ($self, $langs) = @_;
74              
75 4         15 return $self->run_crowdin_cli('download', $langs);
76             }
77              
78             sub push_ts {
79 4     4 0 2520 my ($self, $langs) = @_;
80              
81 4         12 my $cli_return = $self->run_crowdin_cli('upload sources', ());
82              
83 4 50       224 if ($cli_return != 0) {
84 0         0 return $cli_return;
85             }
86              
87 4 100       23 if ($self->{data}->{upload_translations}) {
88 3         27 my $action = 'upload translations';
89 3 100       11 if ($self->{data}->{import_duplicates}) {
90 1         10 $action .= ' --import-duplicates';
91             }
92 3 100       21 if ($self->{data}->{import_eq_suggestions}) {
93 1         9 $action .= ' --import-eq-suggestions'
94             }
95 3 100       21 if ($self->{data}->{auto_approve_imported}) {
96 1         8 $action .= ' --auto-approve-imported'
97             }
98 3         18 $cli_return = $self->run_crowdin_cli($action, $langs);
99             }
100              
101 4         167 return $cli_return;
102             }
103              
104             1;