From 2d66d5e471148147eb97b23743a1d678bea14e7f Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Fri, 15 Nov 2024 16:12:33 +0100 Subject: [PATCH] Bug 38460: [WIP] Enable translations for authorised values (alternative) As it's a WIP, not every place shows the translated description. One place where it works is the items table in the "edit item" page Test plan: 1. Edit one of LOC authorised values, you should see links to translate descriptions (for both staff and opac) Add translations for a language 2. Switch to this language, if not already done 3. Find a biblio record, edit one of its items and change the loc field to the value you translated. Save. 4. Now in the table at the top of the page you should see the translated description --- C4/Biblio.pm | 25 ++++++++----------- Koha/AuthorisedValue.pm | 12 +++++++++ Koha/Schema/Result/AuthorisedValue.pm | 2 ++ Koha/Template/Plugin/AuthorisedValues.pm | 8 +++--- .../en/modules/admin/authorised_values.tt | 3 +++ 5 files changed, 33 insertions(+), 17 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 3e494ba174..fae5e3b03c 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -1559,21 +1559,18 @@ sub GetAuthorisedValueDesc { my $dbh = C4::Context->dbh; if ( $category ne "" ) { - $cache_key = "AV_descriptions:" . $category; - my $av_descriptions = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); - if ( !$av_descriptions ) { - $av_descriptions = { - map { $_->authorised_value => { lib => $_->lib, lib_opac => $_->lib_opac } } - Koha::AuthorisedValues->search( - { category => $category }, - { columns => [ 'authorised_value', 'lib_opac', 'lib' ] } - )->as_list - }; - $cache->set_in_cache( $cache_key, $av_descriptions ); + my $memory_cache = Koha::Cache::Memory::Lite->get_instance(); + my $cache_key = 'GetAuthorisedValueDesc:authorised_values'; + my $authorised_values = $memory_cache->get_from_cache($cache_key); + unless ($authorised_values) { + $authorised_values = { map { $_->authorised_value => $_ } Koha::AuthorisedValues->as_list }; + $memory_cache->set_in_cache( $cache_key, $authorised_values ); } - return ( $opac && $av_descriptions->{$value}->{'lib_opac'} ) - ? $av_descriptions->{$value}->{'lib_opac'} - : $av_descriptions->{$value}->{'lib'}; + my $av = $authorised_values->{$value}; + return unless $av; + + my $lang = C4::Languages::getlanguage(); + return $opac ? $av->opac_translated_description($lang) : $av->localization( 'lib', $lang ); } else { return $value; # if nothing is found return the original value } diff --git a/Koha/AuthorisedValue.pm b/Koha/AuthorisedValue.pm index 7a306bfae5..c66c28cb73 100644 --- a/Koha/AuthorisedValue.pm +++ b/Koha/AuthorisedValue.pm @@ -102,6 +102,18 @@ sub opac_description { return $self->lib_opac() || $self->lib(); } +=head3 opac_translated_description + +my $description = $av->opac_translated_description($lang); + +=cut + +sub opac_translated_description { + my ($self, $lang) = @_; + + return $self->localization('lib_opac', $lang) || $self->localization('lib', $lang); +} + =head3 to_api_mapping This method returns the mapping for representing a Koha::AuthorisedValue object diff --git a/Koha/Schema/Result/AuthorisedValue.pm b/Koha/Schema/Result/AuthorisedValue.pm index 874c166b02..dba1adb05b 100644 --- a/Koha/Schema/Result/AuthorisedValue.pm +++ b/Koha/Schema/Result/AuthorisedValue.pm @@ -176,6 +176,8 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-01-21 13:39:29 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:LH9dpEEzlVVsjNVv/jnONg +__PACKAGE__->load_components('+Koha::Schema::Component::Localization'); +__PACKAGE__->localization_add_relationships('id', 'lib', 'lib_opac'); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/Koha/Template/Plugin/AuthorisedValues.pm b/Koha/Template/Plugin/AuthorisedValues.pm index 2e72aaffe7..c80ebf5abf 100644 --- a/Koha/Template/Plugin/AuthorisedValues.pm +++ b/Koha/Template/Plugin/AuthorisedValues.pm @@ -22,16 +22,18 @@ use Template::Plugin; use base qw( Template::Plugin ); use C4::Koha qw( GetAuthorisedValues ); +use C4::Languages; use Koha::AuthorisedValues; sub GetByCode { my ( $self, $category, $code, $opac ) = @_; - my $av = Koha::AuthorisedValues->search( { category => $category, authorised_value => $code } ); + my $av = Koha::AuthorisedValues->search( { category => $category, authorised_value => $code } ); + my $lang = C4::Languages::getlanguage(); return $av->count ? $opac - ? $av->next->opac_description - : $av->next->lib + ? $av->next->opac_translated_description($lang) + : $av->next->localization( 'lib', $lang ) : $code; } diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt index 519476a6f3..df3dc02360 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt @@ -190,10 +190,12 @@
  • + [% INCLUDE 'localization-link.inc' source='AuthorisedValue' object_id=av.id property='lib' %]
  • + [% INCLUDE 'localization-link.inc' source='AuthorisedValue' object_id=av.id property='lib_opac' %]
  • @@ -403,6 +405,7 @@ [% MACRO jsinclude BLOCK %] [% Asset.js("js/admin-menu.js") | $raw %] [% INCLUDE 'datatables.inc' %] + [% INCLUDE 'greybox.inc' %]