diff --git a/doc/sources/admin/portalcustom.rst b/doc/sources/admin/portalcustom.rst index 2b85ca266..a551cceed 100644 --- a/doc/sources/admin/portalcustom.rst +++ b/doc/sources/admin/portalcustom.rst @@ -290,6 +290,9 @@ For example ``templates/myskin/en.json``: You can also create a file called ``all.json`` to override messages in all languages. +.. versionchanged:: 2.0.15 + Translations in lemonldap-ng.ini now take priority over translations from skin files + Menu tabs ~~~~~~~~~ diff --git a/doc/sources/admin/upgrade_2_0_x.rst b/doc/sources/admin/upgrade_2_0_x.rst index 47e1dd707..2cf9d05d7 100644 --- a/doc/sources/admin/upgrade_2_0_x.rst +++ b/doc/sources/admin/upgrade_2_0_x.rst @@ -29,6 +29,22 @@ None 2.0.15 ------ +Translation overrides in lemonldap-ng.ini now take priority over skin +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Previously, the presence of a translation JSON file within a skin prevented +translation messages defined in ``lemonldap-ng.ini`` from being used. + +Additionally, it was not possible to translate strings in email templates using +a custom skin file. + +These two bugs are now fixed, be sure to check that you do not have duplicate +translations in ``lemonldap-ng.ini`` and in your skin files (``*.json``). If you do, +the translation in ``lemonldap-ng.ini`` will now take priority. + +See :ref:`documentation on translating messages ` for +details + New Captcha API ~~~~~~~~~~~~~~~ diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/SMTP.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/SMTP.pm index a4463586b..c6935dd67 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/SMTP.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/SMTP.pm @@ -73,8 +73,9 @@ sub translate { . " ($self->{conf}->{templateDir}/$lang_code.json or $self->{conf}->{templateDir}/common/mail/en.json)"; $json = join '', ; close F; - my $lang = from_json( $json, { allow_nonref => 1 } ); - my $langOver = from_json( $self->p->trOver, { allow_nonref => 1 } ); + my $lang = from_json( $json, { allow_nonref => 1 } ); + my $langOver = + from_json( $self->p->getTrOver($req), { allow_nonref => 1 } ); if ($langOver) { for my $k ( keys %{ $langOver->{all} || {} } ) { diff --git a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm index 3f77b406a..df5440bfe 100644 --- a/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm +++ b/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm @@ -872,6 +872,52 @@ sub _dump { return; } +# Warning: this function returns a JSON string +sub getTrOver { + my ( $self, $req, $templateDir ) = @_; + + my $templateDir //= $self->conf->{templateDir} . '/' . $self->getSkin($req); + + unless ( $self->trOverCache->{$templateDir} ) { + + # Override messages + my $trOverMessages = JSON::from_json( $self->trOver ); + + opendir( DIR, $templateDir ); + my @langfiles = grep( /\.json$/, readdir(DIR) ); + close(DIR); + + foreach my $file (@langfiles) { + my ($lang) = ( $file =~ /^(\w+)\.json/ ); + $self->logger->debug("Use $file to override messages"); + if ( open my $json, "<", $templateDir . "/" . $file ) { + my $trdata; + eval { + local $/ = undef; + $trdata = JSON::from_json(<$json>); + }; + if ($@) { + $self->logger->warn("Ignoring $file because of error: $@"); + } + if ( ref($trdata) eq "HASH" ) { + for my $msg ( keys %$trdata ) { + + # lemonldap-ng.ini has priority + $trOverMessages->{$lang}->{$msg} //= $trdata->{$msg}; + } + } + } + else { + $self->logger->error("Unable to read $file"); + } + } + + $self->trOverCache->{$templateDir} = JSON::to_json($trOverMessages); + } + + return $self->trOverCache->{$templateDir}; +} + sub sendHtml { my ( $self, $req, $template, %args ) = @_; @@ -888,29 +934,7 @@ sub sendHtml { $self->logger->debug("-> Trying to load $tmpl"); } - # Override messages - my $trOverMessages = JSON::from_json( $self->trOver ); - - unless ( $self->trOverCache->{$templateDir} ) { - opendir( DIR, $templateDir ); - my @langfiles = grep( /\.json$/, readdir(DIR) ); - close(DIR); - - foreach my $file (@langfiles) { - my ($lang) = ( $file =~ /^(\w+)\.json/ ); - $self->logger->debug("Use $file to override messages"); - if ( open my $json, "<", $templateDir . "/" . $file ) { - local $/ = undef; - $trOverMessages->{$lang} = JSON::from_json(<$json>); - } - else { - $self->logger->error("Unable to read $file"); - } - } - - $self->trOverCache->{$templateDir} = JSON::to_json($trOverMessages); - } - $args{params}->{TROVER} = $self->trOverCache->{$templateDir}; + $args{params}->{TROVER} = $self->getTrOver( $req, $templateDir ); my $res = $self->SUPER::sendHtml( $req, $template, %args ); push @{ $res->[1] },