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
|
|
19909
|
use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
12
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
1994
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
25
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use File::Find qw(find); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
52
|
|
9
|
1
|
|
|
1
|
|
6
|
use File::Spec::Functions qw(catfile abs2rel); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
10
|
1
|
|
|
1
|
|
7
|
use Serge::Util qw(subst_macros); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
81
|
|
11
|
1
|
|
|
1
|
|
506
|
use version; |
|
1
|
|
|
|
|
2057
|
|
|
1
|
|
|
|
|
6
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = qv('0.902.0'); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub name { |
16
|
8
|
|
|
8
|
0
|
148044
|
return 'Lokalise translation software (https://lokalise.co/) synchronization plugin'; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub init { |
20
|
8
|
|
|
8
|
0
|
109
|
my $self = shift; |
21
|
|
|
|
|
|
|
|
22
|
8
|
|
|
|
|
46
|
$self->SUPER::init(@_); |
23
|
|
|
|
|
|
|
|
24
|
8
|
|
|
|
|
63
|
$self->{optimizations} = 1; |
25
|
|
|
|
|
|
|
|
26
|
8
|
|
|
|
|
68
|
$self->merge_schema({ |
27
|
|
|
|
|
|
|
config_file => 'STRING', |
28
|
|
|
|
|
|
|
root_directory => 'STRING', |
29
|
|
|
|
|
|
|
resource_directory => 'STRING', |
30
|
|
|
|
|
|
|
languages => 'ARRAY', |
31
|
|
|
|
|
|
|
file_format => 'STRING' |
32
|
|
|
|
|
|
|
}); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub validate_data { |
36
|
8
|
|
|
8
|
0
|
8279
|
my ($self) = @_; |
37
|
|
|
|
|
|
|
|
38
|
8
|
|
|
|
|
39
|
$self->SUPER::validate_data; |
39
|
|
|
|
|
|
|
|
40
|
8
|
|
|
|
|
3862
|
$self->{data}->{config_file} = subst_macros($self->{data}->{config_file}); |
41
|
8
|
|
|
|
|
288
|
$self->{data}->{cleanup_mode} = subst_macros($self->{data}->{cleanup_mode}); |
42
|
8
|
|
|
|
|
242
|
$self->{data}->{languages} = subst_macros($self->{data}->{languages}); |
43
|
8
|
|
|
|
|
223
|
$self->{data}->{file_format} = subst_macros($self->{data}->{file_format}); |
44
|
|
|
|
|
|
|
|
45
|
8
|
|
|
|
|
218
|
$self->{data}->{resource_directory} = subst_macros($self->{data}->{resource_directory}); |
46
|
|
|
|
|
|
|
|
47
|
8
|
100
|
|
|
|
213
|
die "'config_file' not defined" unless defined $self->{data}->{config_file}; |
48
|
7
|
100
|
|
|
|
68
|
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}; |
49
|
|
|
|
|
|
|
|
50
|
6
|
100
|
|
|
|
214
|
die "'resource_directory' not defined" unless defined $self->{data}->{resource_directory}; |
51
|
5
|
100
|
|
|
|
51
|
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}; |
52
|
|
|
|
|
|
|
|
53
|
4
|
100
|
|
|
|
96
|
die "'file_format' not defined" unless defined $self->{data}->{file_format}; |
54
|
|
|
|
|
|
|
|
55
|
3
|
100
|
66
|
|
|
42
|
if (!defined $self->{data}->{languages} or scalar(@{$self->{data}->{languages}}) == 0) { |
|
2
|
|
|
|
|
21
|
|
56
|
1
|
|
|
|
|
23
|
die "the list of languages is empty"; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub run_lokalise_cli { |
61
|
4
|
|
|
4
|
0
|
11
|
my ($self, $action) = @_; |
62
|
|
|
|
|
|
|
|
63
|
4
|
|
|
|
|
18
|
my $command = ' --config '.$self->{data}->{config_file}.' '.$action; |
64
|
|
|
|
|
|
|
|
65
|
4
|
|
|
|
|
38
|
$command = 'lokalise2 '.$command; |
66
|
4
|
|
|
|
|
145
|
print "Running '$command'...\n"; |
67
|
4
|
|
|
|
|
40
|
return $self->run_cmd($command); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub get_lokalise_lang { |
71
|
2
|
|
|
2
|
0
|
6
|
my ($self, $lang) = @_; |
72
|
|
|
|
|
|
|
|
73
|
2
|
|
|
|
|
7
|
$lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR' |
|
0
|
|
|
|
|
0
|
|
74
|
|
|
|
|
|
|
|
75
|
2
|
|
|
|
|
6
|
return $lang; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub pull_ts { |
79
|
2
|
|
|
2
|
0
|
712
|
my ($self, $langs) = @_; |
80
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
12
|
my $action = 'file download --format '.$self->{data}->{file_format}; |
82
|
2
|
|
|
|
|
24
|
$action .= ' --unzip-to '.$self->{data}->{resource_directory}; |
83
|
|
|
|
|
|
|
|
84
|
2
|
50
|
|
|
|
21
|
if ($langs) { |
85
|
0
|
|
|
|
|
0
|
my @lokalise_langs = map {$self->get_lokalise_lang($_)} @$langs; |
|
0
|
|
|
|
|
0
|
|
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
0
|
my $langs_as_string = join(',', @lokalise_langs); |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
$action .= ' --filter-langs '.$langs_as_string; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
9
|
return $self->run_lokalise_cli($action); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub push_ts { |
96
|
2
|
|
|
2
|
0
|
1487
|
my ($self, $langs) = @_; |
97
|
|
|
|
|
|
|
|
98
|
2
|
|
|
|
|
8
|
my $langs_to_push = $self->get_langs($langs); |
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
8
|
foreach my $lang (@$langs_to_push) { |
101
|
6
|
|
|
|
|
12
|
my $action = 'file upload'; |
102
|
|
|
|
|
|
|
|
103
|
6
|
|
|
|
|
43
|
my $lang_files_path = catfile($self->{data}->{resource_directory}, $lang); |
104
|
6
|
|
|
|
|
87
|
my @files = $self->find_lang_files($lang_files_path); |
105
|
|
|
|
|
|
|
|
106
|
6
|
|
|
|
|
35
|
foreach my $file (@files) { |
107
|
2
|
|
|
|
|
12
|
my $full_file_path = catfile($lang_files_path, $file); |
108
|
2
|
|
|
|
|
14
|
$action .= ' --file ' . $full_file_path . ' --lang-iso ' . $self->get_lokalise_lang($lang); |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
7
|
my $cli_return = $self->run_lokalise_cli($action, ()); |
111
|
|
|
|
|
|
|
|
112
|
2
|
50
|
|
|
|
150
|
if ($cli_return != 0) { |
113
|
0
|
|
|
|
|
0
|
return $cli_return; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
8
|
return 0; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub get_langs { |
122
|
2
|
|
|
2
|
0
|
7
|
my ($self, $langs) = @_; |
123
|
|
|
|
|
|
|
|
124
|
2
|
50
|
|
|
|
7
|
if (!$langs) { |
125
|
2
|
|
|
|
|
11
|
$langs = $self->{data}->{languages}; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
2
|
|
|
|
|
19
|
return $langs; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub find_lang_files { |
132
|
6
|
|
|
6
|
0
|
13
|
my ($self, $directory) = @_; |
133
|
|
|
|
|
|
|
|
134
|
6
|
|
|
|
|
13
|
my @files = (); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
find(sub { |
137
|
4
|
100
|
|
4
|
|
198
|
push @files, abs2rel($File::Find::name, $directory) if(-f $_); |
138
|
6
|
|
|
|
|
828
|
}, $directory); |
139
|
|
|
|
|
|
|
|
140
|
6
|
|
|
|
|
258
|
return @files; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |