File Coverage

lib/Serge/Sync/Plugin/TranslationService/lokalise.pm
Criterion Covered Total %
statement 69 74 93.2
branch 17 20 85.0
condition 2 3 66.6
subroutine 14 14 100.0
pod 0 8 0.0
total 102 119 85.7


line stmt bran cond sub pod time code
1             # ABSTRACT: Lokalise (https://lokalise.co/) synchronization plugin for Serge
2              
3             package Serge::Sync::Plugin::TranslationService::lokalise;
4 1     1   17561 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         2  
  1         9  
5              
6 1     1   1531 use strict;
  1         3  
  1         19  
7              
8 1     1   5 use File::Find qw(find);
  1         3  
  1         41  
9 1     1   5 use File::Spec::Functions qw(catfile abs2rel);
  1         2  
  1         35  
10 1     1   5 use Serge::Util qw(subst_macros);
  1         2  
  1         69  
11 1     1   390 use version;
  1         1622  
  1         5  
12              
13             our $VERSION = qv('0.900.2');
14              
15             sub name {
16 8     8 0 111880 return 'Lokalise translation software (https://lokalise.co/) synchronization plugin';
17             }
18              
19             sub init {
20 8     8 0 77 my $self = shift;
21              
22 8         33 $self->SUPER::init(@_);
23              
24 8         45 $self->{optimizations} = 1;
25              
26 8         52 $self->merge_schema({
27             config_file => 'STRING',
28             resource_directory => 'STRING',
29             languages => 'ARRAY',
30             file_mask => 'STRING',
31             file_format => 'STRING'
32             });
33             }
34              
35             sub validate_data {
36 8     8 0 6777 my ($self) = @_;
37              
38 8         29 $self->SUPER::validate_data;
39              
40 8         3838 $self->{data}->{config_file} = subst_macros($self->{data}->{config_file});
41 8         223 $self->{data}->{cleanup_mode} = subst_macros($self->{data}->{cleanup_mode});
42 8         200 $self->{data}->{languages} = subst_macros($self->{data}->{languages});
43 8         181 $self->{data}->{file_mask} = subst_macros($self->{data}->{file_mask});
44 8         169 $self->{data}->{file_format} = subst_macros($self->{data}->{file_format});
45              
46 8 100       196 die "'config_file' not defined" unless defined $self->{data}->{config_file};
47 7 100       50 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};
48              
49 6 100       159 die "'resource_directory' not defined" unless defined $self->{data}->{resource_directory};
50 5 100       40 die "'resource_directory', which is set to '$self->{data}->{resource_directory}', does not point to a valid folder.\n" unless -d $self->{data}->{resource_directory};
51            
52 4 100       73 die "'file_mask' not defined" unless defined $self->{data}->{file_mask};
53 3 100       31 die "'file_format' not defined" unless defined $self->{data}->{file_format};
54              
55 2 100 66     15 if (!defined $self->{data}->{languages} or scalar(@{$self->{data}->{languages}}) == 0) {
  1         10  
56 1         16 die "the list of languages is empty";
57             }
58             }
59              
60             sub run_lokalise_cli {
61 4     4 0 8 my ($self, $action) = @_;
62              
63 4         11 my $command = ' --config '.$self->{data}->{config_file}.' '.$action;
64              
65 4         28 $command = 'lokalise '.$command;
66 4         94 print "Running '$command'...\n";
67 4         29 return $self->run_cmd($command);
68             }
69              
70             sub get_lokalise_lang {
71 3     3 0 7 my ($self, $lang) = @_;
72              
73 3         13 $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR'
  1         6  
74              
75 3         7 return $lang;
76             }
77              
78             sub pull_ts {
79 1     1 0 324 my ($self, $langs) = @_;
80              
81 1         5 my $action = 'export --type '.$self->{data}->{file_format};
82 1         10 $action .= ' --use_original 1';
83 1         3 $action .= ' --unzip_to '.$self->{data}->{resource_directory};
84              
85 1 50       11 if ($langs) {
86 0         0 my @lokalise_langs = map {$self->get_lokalise_lang($_)} @$langs;
  0         0  
87              
88 0         0 my $langs_as_string = join(',', @lokalise_langs);
89              
90 0         0 $action .= ' --langs '.$langs_as_string;
91             }
92              
93 1         4 return $self->run_lokalise_cli($action);
94             }
95              
96             sub push_ts {
97 1     1 0 635 my ($self, $langs) = @_;
98              
99 1         4 my $langs_to_push = $self->get_langs($langs);
100              
101 1         4 foreach my $lang (@$langs_to_push) {
102 3         5 my $action = 'import';
103              
104 3         18 my $directory = catfile($self->{data}->{resource_directory}, $lang);
105 3         25 my $file_mask = catfile($directory, $self->{data}->{file_mask});
106              
107 3         24 $action .= ' --file '.$file_mask.' --lang_iso '.$self->get_lokalise_lang($lang);
108 3         7 $action .= ' --replace 1 --fill_empty 0 --distinguish 1';
109              
110 3         6 my $cli_return = $self->run_lokalise_cli($action, ());
111              
112 3 50       171 if ($cli_return != 0) {
113 0         0 return $cli_return;
114             }
115             }
116              
117 1         3 return 0;
118             }
119              
120             sub get_langs {
121 1     1 0 3 my ($self, $langs) = @_;
122              
123 1 50       3 if (!$langs) {
124 1         5 $langs = $self->{data}->{languages};
125             }
126              
127 1         8 return $langs;
128             }
129              
130             1;