| 1 | # ABSTRACT: Lokalise (https://lokalise.co/) synchronization plugin for Serge | ||
| 2 | |||
| 3 | package Serge::Sync::Plugin::TranslationService::lokalise; | ||
| 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 File::Find qw(find); | ||
| 9 | use File::Spec::Functions qw(catfile abs2rel); | ||
| 10 | use Serge::Util qw(subst_macros); | ||
| 11 | use version; | ||
| 12 | |||
| 13 | our $VERSION = qv('0.900.2'); | ||
| 14 | |||
| 15 | sub name { | ||
| 16 | return 'Lokalise translation software (https://lokalise.co/) synchronization plugin'; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub init { | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | $self->SUPER::init(@_); | ||
| 23 | |||
| 24 | $self->{optimizations} = 1; | ||
| 25 | |||
| 26 | $self->merge_schema({ | ||
| 27 | config_file => 'STRING', | ||
| 28 | resource_directory => 'STRING', | ||
| 29 | languages => 'ARRAY', | ||
| 30 | file_mask => 'STRING', | ||
| 31 | file_format => 'STRING' | ||
| 32 | }); | ||
| 33 | } | ||
| 34 | |||
| 35 | sub validate_data { | ||
| 36 | my ($self) = @_; | ||
| 37 | |||
| 38 | $self->SUPER::validate_data; | ||
| 39 | |||
| 40 | $self->{data}->{config_file} = subst_macros($self->{data}->{config_file}); | ||
| 41 | $self->{data}->{cleanup_mode} = subst_macros($self->{data}->{cleanup_mode}); | ||
| 42 | $self->{data}->{languages} = subst_macros($self->{data}->{languages}); | ||
| 43 | $self->{data}->{file_mask} = subst_macros($self->{data}->{file_mask}); | ||
| 44 | $self->{data}->{file_format} = subst_macros($self->{data}->{file_format}); | ||
| 45 | |||
| 46 | die "'config_file' not defined" unless defined $self->{data}->{config_file}; | ||
| 47 | 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}; | ||
| 48 | |||
| 49 | die "'resource_directory' not defined" unless defined $self->{data}->{resource_directory}; | ||
| 50 | die "'resource_directory', which is set to '$self->{data}->{resource_directory}', does not point to a valid folder.\n" unless -d $self->{data}->{resource_directory}; | ||
| 51 | |||
| 52 | die "'file_mask' not defined" unless defined $self->{data}->{file_mask}; | ||
| 53 | die "'file_format' not defined" unless defined $self->{data}->{file_format}; | ||
| 54 | |||
| 55 | if (!defined $self->{data}->{languages} or scalar(@{$self->{data}->{languages}}) == 0) { | ||
| 56 | die "the list of languages is empty"; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub run_lokalise_cli { | ||
| 61 | my ($self, $action) = @_; | ||
| 62 | |||
| 63 | my $command = ' --config '.$self->{data}->{config_file}.' '.$action; | ||
| 64 | |||
| 65 | $command = 'lokalise '.$command; | ||
| 66 | print "Running '$command'...\n"; | ||
| 67 | return $self->run_cmd($command); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub get_lokalise_lang { | ||
| 71 | my ($self, $lang) = @_; | ||
| 72 | |||
| 73 | $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR' | ||
| 74 | |||
| 75 | return $lang; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub pull_ts { | ||
| 79 | my ($self, $langs) = @_; | ||
| 80 | |||
| 81 | my $action = 'export --type '.$self->{data}->{file_format}; | ||
| 82 | $action .= ' --use_original 1'; | ||
| 83 | $action .= ' --unzip_to '.$self->{data}->{resource_directory}; | ||
| 84 | |||
| 85 | if ($langs) { | ||
| 86 | my @lokalise_langs = map {$self->get_lokalise_lang($_)} @$langs; | ||
| 87 | |||
| 88 | my $langs_as_string = join(',', @lokalise_langs); | ||
| 89 | |||
| 90 | $action .= ' --langs '.$langs_as_string; | ||
| 91 | } | ||
| 92 | |||
| 93 | return $self->run_lokalise_cli($action); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub push_ts { | ||
| 97 | my ($self, $langs) = @_; | ||
| 98 | |||
| 99 | my $langs_to_push = $self->get_langs($langs); | ||
| 100 | |||
| 101 | foreach my $lang (@$langs_to_push) { | ||
| 102 | my $action = 'import'; | ||
| 103 | |||
| 104 | my $directory = catfile($self->{data}->{resource_directory}, $lang); | ||
| 105 | my $file_mask = catfile($directory, $self->{data}->{file_mask}); | ||
| 106 | |||
| 107 | $action .= ' --file '.$file_mask.' --lang_iso '.$self->get_lokalise_lang($lang); | ||
| 108 | $action .= ' --replace 1 --fill_empty 0 --distinguish 1'; | ||
| 109 | |||
| 110 | my $cli_return = $self->run_lokalise_cli($action, ()); | ||
| 111 | |||
| 112 | if ($cli_return != 0) { | ||
| 113 | return $cli_return; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | return 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub get_langs { | ||
| 121 | my ($self, $langs) = @_; | ||
| 122 | |||
| 123 | if (!$langs) { | ||
| 124 | $langs = $self->{data}->{languages}; | ||
| 125 | } | ||
| 126 | |||
| 127 | return $langs; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; | ||