| 1 | # ABSTRACT: Mojito (http://www.mojito.global/) synchronization plugin for Serge | ||
| 2 | |||
| 3 | package Serge::Sync::Plugin::TranslationService::mojito; | ||
| 4 | |||
| 5 | 1 | ▶ | use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use strict; | ||
| 8 | |||
| 9 | use Serge::Util qw(subst_macros culture_from_lang locale_from_lang); | ||
| 10 | use version; | ||
| 11 | |||
| 12 | our $VERSION = qv('0.907.2'); | ||
| 13 | |||
| 14 | sub name { | ||
| 15 | return 'Mojito translation server (http://www.mojito.global/) synchronization plugin'; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $self->SUPER::init(@_); | ||
| 22 | |||
| 23 | $self->{optimizations} = 1; | ||
| 24 | |||
| 25 | $self->merge_schema({ | ||
| 26 | repository_name => 'STRING', | ||
| 27 | application_properties => 'STRING', | ||
| 28 | source_files_path => 'STRING', | ||
| 29 | source_locale => 'STRING', | ||
| 30 | localized_files_path => 'STRING', | ||
| 31 | import_translations => 'BOOLEAN', | ||
| 32 | inheritance_mode => 'STRING', | ||
| 33 | file_type => 'STRING', | ||
| 34 | status_equal_target => 'STRING', | ||
| 35 | status_pull => 'STRING', | ||
| 36 | destination_locales => 'ARRAY' | ||
| 37 | }); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub validate_data { | ||
| 41 | my ($self) = @_; | ||
| 42 | |||
| 43 | $self->SUPER::validate_data; | ||
| 44 | |||
| 45 | $self->{data}->{application_properties} = subst_macros($self->{data}->{application_properties}); | ||
| 46 | $self->{data}->{repository_name} = subst_macros($self->{data}->{repository_name}); | ||
| 47 | $self->{data}->{source_files_path} = subst_macros($self->{data}->{source_files_path}); | ||
| 48 | $self->{data}->{localized_files_path} = subst_macros($self->{data}->{localized_files_path}); | ||
| 49 | $self->{data}->{import_translations} = subst_macros($self->{data}->{import_translations}); | ||
| 50 | $self->{data}->{source_locale} = subst_macros($self->{data}->{source_locale}); | ||
| 51 | $self->{data}->{inheritance_mode} = subst_macros($self->{data}->{inheritance_mode}); | ||
| 52 | $self->{data}->{file_type} = subst_macros($self->{data}->{file_type}); | ||
| 53 | $self->{data}->{status_equal_target} = subst_macros($self->{data}->{status_equal_target}); | ||
| 54 | $self->{data}->{status_pull} = subst_macros($self->{data}->{status_pull}); | ||
| 55 | $self->{data}->{destination_locales} = subst_macros($self->{data}->{destination_locales}); | ||
| 56 | |||
| 57 | die "'repository_name' not defined" unless defined $self->{data}->{repository_name}; | ||
| 58 | |||
| 59 | if ($self->{data}->{application_properties} ne '') { | ||
| 60 | die "'application_properties', which is set to '$self->{data}->{application_properties}', does not point to a valid file.\n" unless -f $self->{data}->{application_properties}; | ||
| 61 | } | ||
| 62 | |||
| 63 | die "'source_files_path' not defined" unless defined $self->{data}->{source_files_path}; | ||
| 64 | die "'source_files_path', which is set to '$self->{data}->{source_files_path}', does not point to a valid directory.\n" unless -d $self->{data}->{source_files_path}; | ||
| 65 | |||
| 66 | die "'localized_files_path' not defined" unless defined $self->{data}->{localized_files_path}; | ||
| 67 | die "'localized_files_path', which is set to '$self->{data}->{localized_files_path}', does not point to a valid directory.\n" unless -d $self->{data}->{localized_files_path}; | ||
| 68 | |||
| 69 | $self->{data}->{import_translations} = 1 unless defined $self->{data}->{import_translations}; | ||
| 70 | $self->{data}->{inheritance_mode} = 'REMOVE_UNTRANSLATED' unless defined $self->{data}->{inheritance_mode}; | ||
| 71 | $self->{data}->{status_pull} = 'ACCEPTED' unless defined $self->{data}->{status_pull}; | ||
| 72 | |||
| 73 | if (!defined $self->{data}->{destination_locales} or scalar(@{$self->{data}->{destination_locales}}) == 0) { | ||
| 74 | die "the list of destination languages is empty"; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub run_mojito_cli { | ||
| 79 | my ($self, $action, $langs, $capture) = @_; | ||
| 80 | |||
| 81 | my $command = $action; | ||
| 82 | |||
| 83 | $command .= ' -r '.$self->{data}->{repository_name}; | ||
| 84 | $command .= ' -s '.$self->{data}->{source_files_path}; | ||
| 85 | if ($self->{data}->{application_properties} ne '') { | ||
| 86 | $command .= ' --spring.config.location='.$self->{data}->{application_properties}; | ||
| 87 | } | ||
| 88 | |||
| 89 | if (defined $self->{data}->{source_locale}) { | ||
| 90 | $command .= ' -sl '.$self->{data}->{source_locale}; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ($langs) { | ||
| 94 | my @locale_mapping = map {$self->get_mojito_locale_mapping($_)} @$langs; | ||
| 95 | |||
| 96 | my $locale_mapping_as_string = join(',', @locale_mapping); | ||
| 97 | |||
| 98 | $command .= ' --locale-mapping '.$locale_mapping_as_string; | ||
| 99 | } | ||
| 100 | |||
| 101 | if (defined $self->{data}->{file_type}) { | ||
| 102 | $command .= ' -ft '.$self->{data}->{file_type}; | ||
| 103 | } | ||
| 104 | |||
| 105 | $command = 'mojito '.$command; | ||
| 106 | print "Running '$command'...\n"; | ||
| 107 | |||
| 108 | return $self->run_cmd($command, $capture); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub get_mojito_locale_mapping { | ||
| 112 | my ($self, $lang) = @_; | ||
| 113 | |||
| 114 | my $locale = locale_from_lang($lang); | ||
| 115 | |||
| 116 | my $bcp47_locale = culture_from_lang($lang); | ||
| 117 | |||
| 118 | return $locale.':'.$bcp47_locale; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub pull_ts { | ||
| 122 | my ($self, $langs) = @_; | ||
| 123 | |||
| 124 | my $action = 'pull --inheritance-mode '.$self->{data}->{inheritance_mode}.' -t '.$self->{data}->{localized_files_path}; | ||
| 125 | $action .= ' --status '.$self->{data}->{status_pull}; | ||
| 126 | |||
| 127 | return $self->run_mojito_cli($action, $self->get_langs($langs)); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub push_ts { | ||
| 131 | my ($self, $langs) = @_; | ||
| 132 | |||
| 133 | my $cli_return = $self->run_mojito_cli('push', ()); | ||
| 134 | |||
| 135 | if ($cli_return != 0) { | ||
| 136 | return $cli_return; | ||
| 137 | } | ||
| 138 | |||
| 139 | if ($self->{data}->{import_translations}) { | ||
| 140 | my $action = 'import -t '.$self->{data}->{localized_files_path}; | ||
| 141 | |||
| 142 | if ($self->{data}->{status_equal_target} ne '') { | ||
| 143 | $action .= ' --status-equal-target '.$self->{data}->{status_equal_target}; | ||
| 144 | } | ||
| 145 | |||
| 146 | $cli_return = $self->run_mojito_cli($action, $self->get_langs($langs)); | ||
| 147 | } | ||
| 148 | |||
| 149 | return $cli_return; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get_langs { | ||
| 153 | my ($self, $langs) = @_; | ||
| 154 | |||
| 155 | if (!$langs) { | ||
| 156 | $langs = $self->{data}->{destination_locales}; | ||
| 157 | } | ||
| 158 | |||
| 159 | return $langs; | ||
| 160 | } | ||
| 161 | |||
| 162 | 1; | ||