File Coverage

lib/Serge/Sync/Plugin/TranslationService/transifex.pm
Criterion Covered Total %
statement 49 49 100.0
branch 9 10 90.0
condition n/a
subroutine 11 11 100.0
pod 0 7 0.0
total 69 77 89.6


line stmt bran cond sub pod time code
1             # ABSTRACT: Transifex (https://www.transifex.com) synchronization plugin for Serge
2              
3             package Serge::Sync::Plugin::TranslationService::transifex;
4 1     1   13919 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         2  
  1         8  
5              
6 1     1   1803 use strict;
  1         3  
  1         35  
7              
8 1     1   6 use Serge::Util qw(subst_macros);
  1         2  
  1         86  
9 1     1   455 use version;
  1         1933  
  1         5  
10              
11             our $VERSION = qv('0.901.0');
12              
13             sub name {
14 4     4 0 41696 return 'Transifex translation software (https://www.transifex.com) synchronization plugin';
15             }
16              
17             sub init {
18 4     4 0 46 my $self = shift;
19              
20 4         19 $self->SUPER::init(@_);
21              
22 4         29 $self->{optimizations} = 1; # set to undef to disable optimizations
23              
24 4         26 $self->merge_schema({
25             root_directory => 'STRING',
26             push_translations => 'BOOLEAN'
27             });
28             }
29              
30             sub validate_data {
31 4     4 0 2553 my ($self) = @_;
32              
33 4         19 $self->SUPER::validate_data;
34              
35 4         1046 $self->{data}->{root_directory} = subst_macros($self->{data}->{root_directory});
36 4         133 $self->{data}->{push_translations} = subst_macros($self->{data}->{push_translations});
37              
38 4 50       116 die "'root_directory' not defined" unless defined $self->{data}->{root_directory};
39 4 100       37 die "'root_directory', which is set to '$self->{data}->{root_directory}', does not point to a valid file.\n" unless -d $self->{data}->{root_directory};
40              
41 3 100       87 $self->{data}->{push_translations} = 1 unless defined $self->{data}->{push_translations};
42             }
43              
44             sub run_transifex_cli {
45 6     6 0 16 my ($self, $action, $langs, $capture) = @_;
46              
47 6         12 my $cli_return = 0;
48              
49 6         12 my $command = $action;
50              
51 6         28 $command .= ' --root '.$self->{data}->{root_directory};
52              
53 6 100       54 if ($langs) {
54 2         6 my @locales = map {$self->get_transifex_locale($_)} @$langs;
  4         11  
55              
56 2         7 my $locales_as_string = join(',', @locales);
57              
58 2         7 $command .= ' --language '.$locales_as_string;
59             }
60              
61 6         15 $command = 'tx '.$command;
62 6         192 print "Running '$command'...\n";
63              
64 6         42 $cli_return = $self->run_cmd($command, $capture);
65              
66 6         352 return $cli_return;
67             }
68              
69             sub get_transifex_locale {
70 4     4 0 10 my ($self, $lang) = @_;
71              
72 4         20 $lang =~ s/-(\w+)$/'_'.uc($1)/e; # convert e.g. 'pt-br' to 'pt_BR'
  2         10  
73              
74 4         24 return $lang;
75             }
76              
77             sub pull_ts {
78 3     3 0 796 my ($self, $langs) = @_;
79              
80 3         13 return $self->run_transifex_cli('pull', $langs);
81             }
82              
83             sub push_ts {
84 3     3 0 1634 my ($self, $langs) = @_;
85              
86 3         7 my $cli_return = 0;
87              
88 3 100       17 if ($self->{data}->{push_translations}) {
89 2         21 $cli_return = $self->run_transifex_cli('push --source --translations', $langs);
90             } else {
91 1         10 $cli_return = $self->run_transifex_cli('push --source', $langs);
92             }
93              
94 3         12 return $cli_return;
95             }
96              
97             1;