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