| 1 | # ABSTRACT: Crowdin (https://crowdin.com) synchronization plugin for Serge | ||
| 2 | |||
| 3 | package Serge::Sync::Plugin::TranslationService::crowdin; | ||
| 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.901.0'); | ||
| 12 | |||
| 13 | sub name { | ||
| 14 | return 'Crowdin translation software (https://crowdin.com) synchronization plugin'; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | $self->SUPER::init(@_); | ||
| 21 | |||
| 22 | $self->{optimizations} = 1; | ||
| 23 | |||
| 24 | $self->merge_schema({ | ||
| 25 | config_file => 'STRING', | ||
| 26 | upload_translations => 'BOOLEAN' | ||
| 27 | }); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub validate_data { | ||
| 31 | my ($self) = @_; | ||
| 32 | |||
| 33 | $self->SUPER::validate_data; | ||
| 34 | |||
| 35 | $self->{data}->{config_file} = subst_macros($self->{data}->{config_file}); | ||
| 36 | $self->{data}->{upload_translations} = subst_macros($self->{data}->{upload_translations}); | ||
| 37 | |||
| 38 | die "'config_file' not defined" unless defined $self->{data}->{config_file}; | ||
| 39 | die "'config_file', which is set to '$self->{data}->{config_file}', does not point to a valid file.\n" unless -f $self->{data}->{config_file}; | ||
| 40 | |||
| 41 | $self->{data}->{upload_translations} = 1 unless defined $self->{data}->{upload_translations}; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub run_crowdin_cli { | ||
| 45 | my ($self, $action, $langs, $capture) = @_; | ||
| 46 | |||
| 47 | my $command = $action; | ||
| 48 | |||
| 49 | if ($langs) { | ||
| 50 | foreach my $lang (sort @$langs) { | ||
| 51 | $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR' | ||
| 52 | $command .= " -l=$lang"; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | $command .= ' --config '.$self->{data}->{config_file}; | ||
| 57 | |||
| 58 | $command = 'crowdin '.$command; | ||
| 59 | print "Running '$command'...\n"; | ||
| 60 | return $self->run_cmd($command, $capture); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub pull_ts { | ||
| 64 | my ($self, $langs) = @_; | ||
| 65 | |||
| 66 | return $self->run_crowdin_cli('download', $langs); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub push_ts { | ||
| 70 | my ($self, $langs) = @_; | ||
| 71 | |||
| 72 | my $cli_return = $self->run_crowdin_cli('upload sources', ()); | ||
| 73 | |||
| 74 | if ($cli_return != 0) { | ||
| 75 | return $cli_return; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($self->{data}->{upload_translations}) { | ||
| 79 | $cli_return = $self->run_crowdin_cli('upload translations', $langs); | ||
| 80 | } | ||
| 81 | |||
| 82 | return $cli_return; | ||
| 83 | } | ||
| 84 | |||
| 85 | 1; | ||