From f6f6d8be7e3dc2651ce53dae9f3bdd469de59f1a Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Fri, 19 Jul 2024 12:54:38 +0000 Subject: [PATCH] Bug 37472: Trigger translations from plugin install This patch allows a plugin installation to update translated languages to include any translation files included in the plugin --- Koha/Plugins.pm | 29 +++++++-- Koha/Plugins/Base.pm | 23 +++++++ misc/translator/LangInstaller.pm | 108 +++++++++++++++++-------------- 3 files changed, 103 insertions(+), 57 deletions(-) diff --git a/Koha/Plugins.pm b/Koha/Plugins.pm index e2cffee473a..8522dedda48 100644 --- a/Koha/Plugins.pm +++ b/Koha/Plugins.pm @@ -278,7 +278,6 @@ sub InstallPlugins { my @plugin_classes = $self->plugins(); my @plugins; - my @plugins_to_translate; foreach my $plugin_class (@plugin_classes) { if ( can_load( modules => { $plugin_class => undef }, verbose => $verbose, nocache => 1 ) ) { @@ -309,16 +308,32 @@ sub InstallPlugins { } push @plugins, $plugin; + } + } - my @path_params = split( /\//, $plugin->{_bundle_path} ); - my $translation_data = { - bundle_path => $plugin->{_bundle_path}, - name => $path_params[-1], - }; + my @plugin_languages; + my $dev_detected; + foreach my $plugin (@plugins) { + my $plugin_langs = $plugin->get_translated_languages(); + + foreach my $lang (@$plugin_langs) { + push( @plugin_languages, $lang ) unless grep( $_ eq $lang, @plugin_languages ); } + + $dev_detected = 1 if ( $plugin->{_bundle_path} =~ /kohadev/i ); } - # Add translator stuff here + # Install translations if a language is already installed on the system + if ( scalar(@plugin_languages) > 0 ) { + my @translated_languages = map { $_->{rfc4646_subtag} } + @{ C4::Languages::getTranslatedLanguages( "both", C4::Context->preference('template') ) }; + foreach my $lang (@plugin_languages) { + next unless grep( $_ eq $lang, @translated_languages ); + my $translate_cmd = "koha-translate --update $lang"; + $translate_cmd .= " --dev kohadev" if $dev_detected; + system $translate_cmd; + } + } Koha::Cache::Memory::Lite->clear_from_cache(ENABLED_PLUGINS_CACHE_KEY); diff --git a/Koha/Plugins/Base.pm b/Koha/Plugins/Base.pm index 8e547fba0d6..231894cee4b 100644 --- a/Koha/Plugins/Base.pm +++ b/Koha/Plugins/Base.pm @@ -392,6 +392,29 @@ sub disable { return $self; } +=head2 get_translated_languages + +Identifies whether a plugin has any translated languages + +=cut + +sub get_translated_languages { + my ($self) = @_; + + my @path_params = split( /\//, $self->{_bundle_path} ); + my $name = $path_params[-1]; + my $po_path = $self->{_bundle_path} . "/translator/po"; + + if ( -e $po_path ) { + opendir my $fh, $po_path or die $!; + my @langs = map { ($_) =~ /(.*)-$name-js/ } + grep { $_ =~ /.*-$name-js/ } readdir($fh); + closedir $fh; + return \@langs; + } + return (); +} + 1; __END__ diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm index a9b1e123205..22af173b083 100644 --- a/misc/translator/LangInstaller.pm +++ b/misc/translator/LangInstaller.pm @@ -41,37 +41,12 @@ sub set_lang { "/prog/$lang/modules/admin/preferences"; } -sub _identify_translatable_plugins { - my ($args) = @_; - - my @plugins = Koha::Plugins::GetPlugins( - { - metadata => { has_translations => 1 }, - } - ); - - return () if scalar( @plugins == 0 ); - - my @plugin_dirs; - foreach my $plugin (@plugins) { - my @path_params = split( /\//,$plugin->{_bundle_path}); - my $translation_data = { - bundle_path => $plugin->{_bundle_path}, - name => $path_params[-1], - }; - - push @plugin_dirs, $translation_data; - } - return \@plugin_dirs; -} - sub new { my ($class, $args) = @_; my $lang = $args->{lang}; my $pref_only = $args->{pref_only}; my $verbose = $args->{verbose}; - my $plugin_dirs = $args->{plugin_dirs} || _identify_translatable_plugins(); my $self = { }; @@ -88,7 +63,7 @@ sub new { $self->{po2json} = "yarn run po2json"; $self->{gzip} = `which gzip`; $self->{gunzip} = `which gunzip`; - $self->{plugin_dirs} = $plugin_dirs; + $self->{plugin_dirs} = _identify_translatable_plugins(); chomp $self->{msgfmt}; chomp $self->{gzip}; chomp $self->{gunzip}; @@ -352,19 +327,21 @@ sub install_tmpl { } } - for my $plugin ( @{$self->{plugin_dirs}} ) { - print - " Install templates from plugin: '$plugin->{name}'\n", - if $self->{verbose}; - - my $trans_dir = $plugin->{bundle_path} . "/views/en/"; - my $lang_dir = $plugin->{bundle_path} . "/views/$self->{lang}"; - mkdir $lang_dir unless -d $lang_dir; - - system "$self->{process} install " - . "-i $trans_dir " - . "-o $lang_dir " - . "-s $plugin->{bundle_path}/translator/po/$self->{lang}-$plugin->{name}-template.po -r " + for my $plugin ( @{ $self->{plugin_dirs} } ) { + if ( grep( $self->{lang} eq $_, @{ $plugin->{plugin_langs} } ) ) { + print + " Install templates from plugin: '$plugin->{name}'\n", + if $self->{verbose}; + + my $trans_dir = $plugin->{bundle_path} . "/views/en/"; + my $lang_dir = $plugin->{bundle_path} . "/views/$self->{lang}"; + mkdir $lang_dir unless -d $lang_dir; + + system "$self->{process} install " + . "-i $trans_dir " + . "-o $lang_dir " + . "-s $plugin->{bundle_path}/translator/po/$self->{lang}-$plugin->{name}-template.po -r "; + } } } @@ -511,17 +488,21 @@ sub install_messages { # Add plugin javascript to the same file to avoid needing multiple imports in doc-head template files my $js_po_data = decode_json($json); - foreach my $plugin ( @{$self->{plugin_dirs}} ) { - my $tmp_pluginpo = sprintf '/tmp/%s-%s.po', $plugin->{name}, $self->{lang}; - my $plugin_po2json_cmd = sprintf '%s %s %s', $self->{po2json}, $plugin->{bundle_path} . '/translator/po/' . $self->{lang} . '-' . $plugin->{name} . '-js.po', $tmp_pluginpo; - `$plugin_po2json_cmd`; - my $plugin_json = read_file($tmp_pluginpo); - - my $plugin_js_po_data = decode_json($plugin_json); - delete $plugin_js_po_data->{''}; - - foreach my $key (keys %$plugin_js_po_data) { - $js_po_data->{$key} = $plugin_js_po_data->{$key} unless exists $js_po_data->{$key}; + foreach my $plugin ( @{ $self->{plugin_dirs} } ) { + if ( grep( $self->{lang} eq $_, @{ $plugin->{plugin_langs} } ) ) { + my $tmp_pluginpo = sprintf '/tmp/%s-%s.po', $plugin->{name}, $self->{lang}; + my $plugin_po2json_cmd = sprintf '%s %s %s', $self->{po2json}, + $plugin->{bundle_path} . '/translator/po/' . $self->{lang} . '-' . $plugin->{name} . '-js.po', + $tmp_pluginpo; + `$plugin_po2json_cmd`; + my $plugin_json = read_file($tmp_pluginpo); + + my $plugin_js_po_data = decode_json($plugin_json); + delete $plugin_js_po_data->{''}; + + foreach my $key ( keys %$plugin_js_po_data ) { + $js_po_data->{$key} = $plugin_js_po_data->{$key} unless exists $js_po_data->{$key}; + } } } my $combined_json = encode_json($js_po_data); @@ -595,6 +576,33 @@ sub get_all_langs { @files = map { $_ =~ s/-pref.(po|po.gz)$//r } @files; } +sub _identify_translatable_plugins { + my ($args) = @_; + + my @plugins = Koha::Plugins::GetPlugins( + { + metadata => { has_translations => 1 }, + } + ); + + return () if scalar( @plugins == 0 ); + + my @plugin_dirs; + foreach my $plugin (@plugins) { + my $plugin_langs = $plugin->get_translated_languages(); + my @path_params = split( /\//, $plugin->{_bundle_path} ); + my $translation_data = { + bundle_path => $plugin->{_bundle_path}, + name => $path_params[-1], + plugin_langs => $plugin_langs + }; + + push @plugin_dirs, $translation_data; + } + return \@plugin_dirs; +} + + 1; -- 2.39.3 (Apple Git-146)