From 2b120378a55eb4cf6c4bca51ffbcaf6bf55cd4b1 Mon Sep 17 00:00:00 2001
From: Kevin Carnes <kevin.carnes@ub.lu.se>
Date: Wed, 28 Sep 2022 10:39:30 +0200
Subject: [PATCH] Bug 31640: Fuzzy translations of preferences can cause
 missing sections and inaccurate translations
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch ignores fuzzy translations for preferences and warns if there are multiple sections with the same translated name.

Test Plan:
1)  Install Swedish translations (./misc/translator/translate install sv-SE)
2)  Go to Koha administration in the staff interface
3)  Click Global system preferences
4)  Select I18N/L10N preferences
5)  Enable Swedish in the language preference for staff interface
6)  Save all I18N/L10N preferences
7)  Return to Koha administration
8)  Select Svenska as the language if the page is not in Swedish
9)  Click on Globala systeminställningar
10) Select Cirkulation
11) Observer that there is only SelfCheckInMainUserBlock or
	AutoSelfCheckAllowed, but not both
12) Apply the patch
13) Install Swedish translations (./misc/translator/translate install sv-SE)
14) Go to Koha administration
15) Select Svenska as the language if the page is not in Swedish
16) Click on Globala systeminställningar
17) Select Cirkulation
18) Observe that SelfCheckInMainUserBlock and AutoSelfCheckAllowed
	are both present

https://bugs.koha-community.org/show_bug.cgi?id=31640
---
 misc/translator/LangInstaller.pm | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/misc/translator/LangInstaller.pm b/misc/translator/LangInstaller.pm
index 7ba313ecaa..d99baaae6a 100644
--- a/misc/translator/LangInstaller.pm
+++ b/misc/translator/LangInstaller.pm
@@ -147,7 +147,7 @@ sub get_trans_text {
     my ($self, $msgid, $default) = @_;
 
     my $po = $self->{po}->{Locale::PO->quote($msgid)};
-    if ($po) {
+    if ( $po and not defined( $po->fuzzy() ) ) {
         my $msgstr = Locale::PO->dequote($po->msgstr);
         if ($msgstr and length($msgstr) > 0) {
             return $msgstr;
@@ -174,6 +174,18 @@ sub get_translated_tab_content {
         } keys %$tab_content
     };
 
+    if ( keys %$translated_tab_content != keys %$tab_content ) {
+        my %duplicates;
+        for my $section (keys %$tab_content) {
+            push @{$duplicates{$self->get_trans_text("$file $section", $section)}}, $section;
+        }
+        for my $translation (keys %duplicates) {
+            if (@{$duplicates{$translation}} > 1) {
+                warn qq(In file "$file", "$translation" is a translation for sections ") . join('", "', @{$duplicates{$translation}}) . '"';
+            }
+        }
+    }
+
     return $translated_tab_content;
 }
 
@@ -251,6 +263,17 @@ sub install_prefs {
             } keys %$pref
         };
 
+        if ( keys %$translated_pref != keys %$pref ) {
+            my %duplicates;
+            for my $tab (keys %$pref) {
+                push @{$duplicates{$self->get_trans_text($file, $tab)}}, $tab;
+            }
+            for my $translation (keys %duplicates) {
+                if (@{$duplicates{$translation}} > 1) {
+                    warn qq(In file "$file", "$translation" is a translation for tabs ") . join('", "', @{$duplicates{$translation}}) . '"';
+                }
+            }
+        }
 
         my $file_trans = $self->{po_path_lang} . "/$file";
         print "Write $file\n" if $self->{verbose};
-- 
2.25.1