blib/lib/Serge/Sync/Plugin/TranslationService/locize.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: Locize (https://locize.com/ synchronization plugin for Serge
2  
3 package Serge::Sync::Plugin::TranslationService::locize;
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 Serge::Util qw(subst_macros);
9 use File::Find qw(find);
10 use File::Spec::Functions qw(catfile abs2rel);
11 use version;
12  
13 our $VERSION = qv('0.901.2');
14  
15 sub name {
16 return 'Locize translation software (https://locize.com/) 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 format => 'STRING',
29 path => 'STRING'
30 });
31 }
32  
33 sub validate_data {
34 my ($self) = @_;
35  
36 $self->SUPER::validate_data;
37  
38 $self->{data}->{config_file} = subst_macros($self->{data}->{config_file});
39 $self->{data}->{format} = subst_macros($self->{data}->{format});
40 $self->{data}->{path} = subst_macros($self->{data}->{path});
41  
42 die "'config_file' not defined" unless defined $self->{data}->{config_file};
43 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};
44  
45 die "'format' not defined" unless defined $self->{data}->{format};
46  
47 die "'path', which is set to '$self->{data}->{path}', does not point to a valid folder." unless -d $self->{data}->{path};
48 }
49  
50 sub pull_ts {
51 my ($self, $langs) = @_;
52  
53 if ($langs) {
54 foreach my $lang (@$langs) {
55 my $cli_return = $self->locize_download($lang);
56  
57 if ($cli_return != 0) {
58 return $cli_return;
59 }
60 }
61 } else {
62 my $cli_return = $self->locize_download();
63  
64 if ($cli_return != 0) {
65 return $cli_return;
66 }
67 }
68  
69 return 0;
70 }
71  
72 sub push_ts {
73 my ($self, $langs) = @_;
74  
75 if ($langs) {
76 foreach my $lang (@$langs) {
77 my $cli_return = $self->locize_sync($lang);
78  
79 if ($cli_return != 0) {
80 return $cli_return;
81 }
82 }
83 } else {
84 my $cli_return = $self->locize_sync();
85  
86 if ($cli_return != 0) {
87 return $cli_return;
88 }
89 }
90  
91 return 0;
92 }
93  
94 sub locize_download {
95 my ($self, $lang) = @_;
96  
97 my $action = 'download';
98  
99 my $download_path = $self->{data}->{path};
100 $download_path = catfile($self->{data}->{path}, $self->get_locize_lang($lang)) if $lang;
101  
102 $action .= ' --path '.$download_path;
103 $action .= ' --language '.$self->get_locize_lang($lang) if $lang;
104  
105 return $self->run_locize_cli($action);
106 }
107  
108 sub locize_sync {
109 my ($self, $lang) = @_;
110  
111 my $action = 'sync';
112  
113 $action .= ' --path '.$self->{data}->{path};
114 $action .= ' --language '.$self->get_locize_lang($lang) if $lang;
115 $action .= ' --update-values true --reference-language-only false';
116  
117 return $self->run_locize_cli($action);
118 }
119  
120 sub run_locize_cli {
121 my ($self, $action) = @_;
122  
123 my $command = $action;
124  
125 $command .= ' --config-path '.$self->{data}->{config_file};
126 $command .= ' --format '.$self->{data}->{format};
127  
128 $command = 'locize '.$command;
129 print "Running '$command'...\n";
130 return $self->run_cmd($command);
131 }
132  
133 sub get_locize_lang {
134 my ($self, $lang) = @_;
135  
136 $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR'
137  
138 return $lang;
139 }
140  
141 1;
↑ Summary