blib/lib/Serge/Sync/Plugin/TranslationService/lingohub.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: Lingohub (https://www.lingohub.com) synchronization plugin for Serge
2  
3 package Serge::Sync::Plugin::TranslationService::lingohub;
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  
12 use version;
13  
14 our $VERSION = qv('0.904.2');
15  
16 sub name {
17 return 'Lingohub translation software (https://www.lingohub.com) synchronization plugin';
18 }
19  
20 sub init {
21 my $self = shift;
22  
23 $self->SUPER::init(@_);
24  
25 $self->{optimizations} = 1; # set to undef to disable optimizations
26  
27 $self->merge_schema({
28 project => 'STRING',
29 root_directory => 'STRING',
30 resource_directory => 'STRING',
31 source_language => 'STRING',
32 target_languages => 'ARRAY'
33 });
34 }
35  
36 sub validate_data {
37 my ($self) = @_;
38  
39 $self->SUPER::validate_data;
40  
41 $self->{data}->{project} = subst_macros($self->{data}->{project});
42 $self->{data}->{root_directory} = subst_macros($self->{data}->{root_directory});
43 $self->{data}->{resource_directory} = subst_macros($self->{data}->{resource_directory});
44 $self->{data}->{source_language} = subst_macros($self->{data}->{source_language});
45 $self->{data}->{target_languages} = subst_macros($self->{data}->{target_languages});
46  
47 die "'project' not defined" unless defined $self->{data}->{project};
48 die "'resource_directory' not defined" unless defined $self->{data}->{resource_directory};
49 die "'root_directory', which is set to '$self->{data}->{root_directory}', does not point to a valid folder." unless -d $self->{data}->{root_directory};
50  
51 my $target_languages_count = 0;
52  
53 if (defined $self->{data}->{target_languages}) {
54 $target_languages_count = scalar(@{$self->{data}->{target_languages}});
55 }
56  
57 die "the list of target languages is empty" unless $target_languages_count != 0;
58  
59 $self->{data}->{source_language} = 'en' unless defined $self->{data}->{source_language};
60 }
61  
62 sub pull_ts {
63 my ($self, $langs) = @_;
64  
65 my $langs_to_pull = $self->get_all_langs($langs);
66  
67 foreach my $lang (@$langs_to_pull) {
68 my $directory = catfile($self->{data}->{resource_directory}, $lang);
69 my $cli_return = $self->run_lingohub_cli("resource:down --locale '$lang' --directory $directory --all");
70  
71 if ($cli_return != 0) {
72 return $cli_return;
73 }
74 }
75  
76 return 0;
77 }
78  
79 sub push_ts {
80 my ($self, $langs) = @_;
81  
82 my $langs_to_push = $self->get_all_langs($langs);
83  
84 foreach my $lang (@$langs_to_push) {
85 my $directory = catfile($self->{data}->{resource_directory}, $lang);
86  
87 my $lang_files_path = catfile($self->{data}->{root_directory}, $directory);
88 my @files = $self->find_lang_files($lang_files_path);
89  
90 foreach my $file (@files) {
91 my $resource = catfile($directory, $file);
92 my $cli_return = $self->run_lingohub_cli("resource:up $resource --locale '$lang'");
93  
94 if ($cli_return != 0) {
95 return $cli_return;
96 }
97 }
98 }
99  
100 return 0;
101 }
102  
103 sub run_lingohub_cli {
104 my ($self, $action, $capture) = @_;
105  
106 my $cli_return = 0;
107  
108 my $command = $action;
109  
110 $command = 'lingohub '.$command;
111 $command .= " --project '$self->{data}->{project}'";
112 print "Running '$command ...\n";
113  
114 $cli_return = $self->run_in($self->{data}->{root_directory}, $command, $capture);
115  
116 return $cli_return;
117 }
118  
119 sub get_all_langs {
120 my ($self, $langs) = @_;
121  
122 if (!$langs) {
123 $langs = $self->{data}->{target_languages};
124 }
125  
126 my @all_langs = ($self->{data}->{source_language});
127  
128 push @all_langs, @$langs;
129  
130 return \@all_langs;
131 }
132  
133 sub find_lang_files {
134 my ($self, $directory) = @_;
135  
136 my @files = ();
137  
138 find(sub {
139 push @files, abs2rel($File::Find::name, $directory) if(-f $_);
140 }, $directory);
141  
142 return @files;
143 }
144  
145 1;
↑ Summary