Bug 20115: Set the languages order with the pref values
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 31 Jan 2018 16:47:37 +0000 (13:47 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 4 Apr 2018 18:45:43 +0000 (15:45 -0300)
To avoid the languages to be ordered randomly, it is better to stick on
a default order.
Let's suppose that the order in the pref is correct.

Signed-off-by: Jesse Maseto <jesse@bywatersolutions.com>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
C4/Languages.pm

index 5e4dabd..1958c91 100644 (file)
@@ -342,8 +342,18 @@ sub _build_languages_arrayref {
             push ( @{ $language_groups->{$language_subtags_hashref->{language}} }, $language_subtags_hashref );
         }
         # $key is a language subtag like 'en'
-        while( my ($key, $value) = each %$language_groups) {
 
+        my %idx = map { $enabled_languages->[$_] => $_ } reverse 0 .. @$enabled_languages-1;
+        my @ordered_keys = sort {
+            my $aa = $language_groups->{$a}->[0]->{rfc4646_subtag};
+            my $bb = $language_groups->{$b}->[0]->{rfc4646_subtag};
+            ( exists $idx{$aa} and exists $idx{$bb} and ( $idx{$aa} cmp $idx{$bb} ) )
+            || ( exists $idx{$aa} and exists $idx{$bb} )
+            || exists $idx{$bb}
+        } keys %$language_groups;
+
+        for my $key ( @ordered_keys ) {
+            my $value = $language_groups->{$key};
             # is this language group enabled? are any of the languages within it enabled?
             my $enabled;
             for my $enabled_language (@enabled_languages) {