blib/lib/Serge/Sync/Plugin/TranslationService/lokalise.pm

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