From c7f22f84ada7ad803ce30d253bd95555ad0aab64 Mon Sep 17 00:00:00 2001 From: Joshua Ferraro Date: Sat, 5 Jan 2008 00:50:45 -0600 Subject: [PATCH] additional updates to language support Signed-off-by: Joshua Ferraro --- C4/Languages.pm | 62 +++++++++++++++++++++++++++++++++++++++++++++- C4/Output.pm | 37 +++++++++++++-------------- tools/koha-news.pl | 2 +- 3 files changed, 79 insertions(+), 22 deletions(-) diff --git a/C4/Languages.pm b/C4/Languages.pm index 7a2d00b11e..2d5ceb8ec4 100644 --- a/C4/Languages.pm +++ b/C4/Languages.pm @@ -39,7 +39,7 @@ use C4::Languages; =cut $VERSION = 3.00; @ISA = qw(Exporter); -@EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages get_bidi regex_lang_subtags language_get_description); +@EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages get_bidi regex_lang_subtags language_get_description accept_language); my $DEBUG = 0; =head2 getFrameworkLanguages @@ -392,6 +392,66 @@ sub get_bidi { return $bidi; }; +sub accept_language { + # referenced http://search.cpan.org/src/CGILMORE/I18N-AcceptLanguage-1.04/lib/I18N/AcceptLanguage.pm + my ($clientPreferences,$supportedLanguages) = @_; + # There should be no whitespace anways, but a cleanliness/sanity check + $clientPreferences =~ s/\s//g; + + # Prepare the list of client-acceptable languages + my @languages = (); + foreach my $tag (split(/,/, $clientPreferences)) { + my ($language, $quality) = split(/\;/, $tag); + $quality =~ s/^q=//i if $quality; + $quality = 1 unless $quality; + next if $quality <= 0; + # We want to force the wildcard to be last + $quality = 0 if ($language eq '*'); + # Pushing lowercase language here saves processing later + push(@languages, { quality => $quality, + language => $language, + lclanguage => lc($language) }); + } + # Prepare the list of server-supported languages + my %supportedLanguages = (); + my %secondaryLanguages = (); + foreach my $language (@$supportedLanguages) { + warn "SUP: ".$language->{language_code}; + $supportedLanguages{lc($language->{language_code})} = $language->{language_code}; + if ($language->{language_code} =~ /^([^-]+)-?/) { + $secondaryLanguages{lc($1)} = $language->{language_code}; + } + } + + # Reverse sort the list, making best quality at the front of the array + @languages = sort { $b->{quality} <=> $a->{quality} } @languages; + my $secondaryMatch = ''; + foreach my $tag (@languages) { + if (exists($supportedLanguages{$tag->{lclanguage}})) { + # Client en-us eq server en-us + return $supportedLanguages{$tag->{language}} if exists($supportedLanguages{$tag->{language}}); + return $supportedLanguages{$tag->{lclanguage}}; + } elsif (exists($secondaryLanguages{$tag->{lclanguage}})) { + # Client en eq server en-us + return $secondaryLanguages{$tag->{language}} if exists($secondaryLanguages{$tag->{language}}); + return $supportedLanguages{$tag->{lclanguage}}; + } elsif ($tag->{lclanguage} =~ /^([^-]+)-/ && exists($secondaryLanguages{$1}) && $secondaryMatch eq '') { + # Client en-gb eq server en-us + $secondaryMatch = $secondaryLanguages{$1}; + } elsif ($tag->{lclanguage} =~ /^([^-]+)-/ && exists($secondaryLanguages{$1}) && $secondaryMatch eq '') { + # Client en-us eq server en + $secondaryMatch = $supportedLanguages{$1}; + } elsif ($tag->{lclanguage} eq '*') { + # * matches every language not already specified. + # It doesn't care which we pick, so let's pick the default, + # if available, then the first in the array. + #return $acceptor->defaultLanguage() if $acceptor->defaultLanguage(); + return $supportedLanguages->[0]; + } + } + # No primary matches. Secondary? (ie, en-us requested and en supported) + return $secondaryMatch if $secondaryMatch; +} 1; __END__ diff --git a/C4/Output.pm b/C4/Output.pm index 2873c64bfa..c96573fe0c 100644 --- a/C4/Output.pm +++ b/C4/Output.pm @@ -29,7 +29,7 @@ use strict; require Exporter; use C4::Context; -use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description); +use C4::Languages qw(getTranslatedLanguages get_bidi regex_lang_subtags language_get_description accept_language ); use HTML::Template::Pro; use vars qw($VERSION @ISA @EXPORT); @@ -49,12 +49,7 @@ C4::Output - Functions for managing templates @ISA = qw(Exporter); push @EXPORT, qw( - &themelanguage &gettemplate setlanguagecookie pagination_bar -); - -#Output -push @EXPORT, qw( - &output_html_with_http_headers + &themelanguage &gettemplate setlanguagecookie pagination_bar &output_html_with_http_headers ); @@ -78,7 +73,6 @@ sub gettemplate { } my $path = C4::Context->preference('intranet_includes') || 'includes'; - # warn "PATH : $path"; my ( $theme, $lang ) = themelanguage( $htdocs, $tmplbase, $interface, $query ); my $opacstylesheet = C4::Context->preference('opacstylesheet'); @@ -108,18 +102,18 @@ sub gettemplate { ); # Bidirectionality - my $language_subtags_hashref = regex_lang_subtags($lang); + my $current_lang = regex_lang_subtags($lang); my $bidi; - $bidi = get_bidi($language_subtags_hashref->{script}) if $language_subtags_hashref->{script}; + $bidi = get_bidi($current_lang->{script}) if $current_lang->{script}; # Languages - my $current_lang = regex_lang_subtags($lang); my @template_languages; my $languages_loop = getTranslatedLanguages($interface,$theme); + for my $language_hashref (@$languages_loop) { $language_hashref->{'current_lang'} = $current_lang->{'language'}; $language_hashref->{'native_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'language_code'},'language'); - warn "($language_hashref->{'language_code'},$language_hashref->{'current_lang'},$language_hashref->{'script_code'}"; + #warn "($language_hashref->{'language_code'},$language_hashref->{'current_lang'},$language_hashref->{'script_code'}"; $language_hashref->{'locale_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'current_lang'},'language'); $language_hashref->{'language_description'} = language_get_description($language_hashref->{'language_code'},$language_hashref->{'current_lang'},'language'); $language_hashref->{'script_description'} = language_get_description($language_hashref->{'script_code'},$language_hashref->{'current_lang'},'script'); @@ -142,21 +136,24 @@ sub gettemplate { #--------------------------------------------------------------------------------------------------------- # FIXME - POD sub themelanguage { - my ( $htdocs, $tmpl, $section, $query ) = @_; + my ( $htdocs, $tmpl, $interface, $query ) = @_; + + # Set some defaults for language and theme + # First, check the user's preferences + my $lang; + $lang = accept_language($ENV{HTTP_ACCEPT_LANGUAGE},getTranslatedLanguages($interface,'prog')); - # if (!$query) { - # warn "no query"; - # } + # But, if there's a cookie set, obey it + $lang = $query->cookie('KohaOpacLanguage') if $query->cookie('KohaOpacLanguage'); - # set some defaults for language and theme - my $lang = $query->cookie('KohaOpacLanguage'); + # Fall back to English $lang = 'en' unless $lang; my $theme = 'prog'; my $dbh = C4::Context->dbh; my @languages; my @themes; - if ( $section eq "intranet" ) { + if ( $interface eq "intranet" ) { @languages = split " ", C4::Context->preference("opaclanguages"); @themes = split " ", C4::Context->preference("template"); pop @languages, $lang if $lang; @@ -185,7 +182,7 @@ sub themelanguage { foreach my $la (@languages) { for ( my $pass = 1 ; $pass <= 2 ; $pass += 1 ) { $la =~ s/([-_])/ $1 eq '-'? '_': '-' /eg if $pass == 2; - if ( -e "$htdocs/$th/$la/modules/$tmpl" ) { + if ( -e "$htdocs/$th/$la/".($interface eq 'intranet'?"modules":"")."/$tmpl" ) { $theme = $th; $lang = $la; last THEME; diff --git a/tools/koha-news.pl b/tools/koha-news.pl index f979fb781f..ff763e08bf 100755 --- a/tools/koha-news.pl +++ b/tools/koha-news.pl @@ -29,7 +29,7 @@ use C4::Context; use C4::Dates qw(format_date_in_iso); use C4::Output; use C4::NewsChannels; -use C4::Languages; +use C4::Languages qw(getTranslatedLanguages); use Date::Calc qw/Date_to_Days Today/; my $cgi = new CGI; -- 2.39.5