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