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