| 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.903.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 | import_duplicates => 'BOOLEAN', | ||
| 28 | import_eq_suggestions => 'BOOLEAN', | ||
| 29 | auto_approve_imported => 'BOOLEAN', | ||
| 30 | }); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub validate_data { | ||
| 34 | my ($self) = @_; | ||
| 35 | |||
| 36 | $self->SUPER::validate_data; | ||
| 37 | |||
| 38 | $self->{data}->{config_file} = subst_macros($self->{data}->{config_file}); | ||
| 39 | $self->{data}->{upload_translations} = subst_macros($self->{data}->{upload_translations}); | ||
| 40 | $self->{data}->{import_duplicates} = subst_macros($self->{data}->{import_duplicates}); | ||
| 41 | $self->{data}->{import_eq_suggestions} = subst_macros($self->{data}->{import_eq_suggestions}); | ||
| 42 | $self->{data}->{auto_approve_imported} = subst_macros($self->{data}->{auto_approve_imported}); | ||
| 43 | |||
| 44 | die "'config_file' not defined" unless defined $self->{data}->{config_file}; | ||
| 45 | 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}; | ||
| 46 | |||
| 47 | $self->{data}->{upload_translations} = 1 unless defined $self->{data}->{upload_translations}; | ||
| 48 | $self->{data}->{import_duplicates} = 0 unless defined $self->{data}->{import_duplicates}; | ||
| 49 | $self->{data}->{import_eq_suggestions} = 0 unless defined $self->{data}->{import_eq_suggestions}; | ||
| 50 | $self->{data}->{auto_approve_imported} = 0 unless defined $self->{data}->{auto_approve_imported}; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub run_crowdin_cli { | ||
| 54 | my ($self, $action, $langs, $capture) = @_; | ||
| 55 | |||
| 56 | my $command = $action; | ||
| 57 | |||
| 58 | if ($langs) { | ||
| 59 | foreach my $lang (sort @$langs) { | ||
| 60 | $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR' | ||
| 61 | $command .= " -l=$lang"; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | $command .= ' --config '.$self->{data}->{config_file}; | ||
| 66 | |||
| 67 | $command = 'crowdin '.$command; | ||
| 68 | print "Running '$command'...\n"; | ||
| 69 | return $self->run_cmd($command, $capture); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub pull_ts { | ||
| 73 | my ($self, $langs) = @_; | ||
| 74 | |||
| 75 | return $self->run_crowdin_cli('download', $langs); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub push_ts { | ||
| 79 | my ($self, $langs) = @_; | ||
| 80 | |||
| 81 | my $cli_return = $self->run_crowdin_cli('upload sources', ()); | ||
| 82 | |||
| 83 | if ($cli_return != 0) { | ||
| 84 | return $cli_return; | ||
| 85 | } | ||
| 86 | |||
| 87 | if ($self->{data}->{upload_translations}) { | ||
| 88 | my $action = 'upload translations'; | ||
| 89 | if ($self->{data}->{import_duplicates}) { | ||
| 90 | $action .= ' --import-duplicates'; | ||
| 91 | } | ||
| 92 | if ($self->{data}->{import_eq_suggestions}) { | ||
| 93 | $action .= ' --import-eq-suggestions' | ||
| 94 | } | ||
| 95 | if ($self->{data}->{auto_approve_imported}) { | ||
| 96 | $action .= ' --auto-approve-imported' | ||
| 97 | } | ||
| 98 | $cli_return = $self->run_crowdin_cli($action, $langs); | ||
| 99 | } | ||
| 100 | |||
| 101 | return $cli_return; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||