+sub language_get_description {
+ my ($script,$lang,$type) = @_;
+ my $dbh = C4::Context->dbh;
+ my $desc;
+ my $sth = $dbh->prepare("SELECT description FROM language_descriptions WHERE subtag=? AND lang=? AND type=?");
+ #warn "QUERY: SELECT description FROM language_descriptions WHERE subtag=$script AND lang=$lang AND type=$type";
+ $sth->execute($script,$lang,$type);
+ while (my $descriptions = $sth->fetchrow_hashref) {
+ $desc = $descriptions->{'description'};
+ }
+ unless ($desc) {
+ $sth = $dbh->prepare("SELECT description FROM language_descriptions WHERE subtag=? AND lang=? AND type=?");
+ $sth->execute($script,'en',$type);
+ while (my $descriptions = $sth->fetchrow_hashref) {
+ $desc = $descriptions->{'description'};
+ }
+ }
+ return $desc;
+}
+=head2 regex_lang_subtags
+
+This internal sub takes a string composed according to RFC 4646 as
+an input and returns a reference to a hash containing keys and values
+for ( language, script, region, variant, extension, privateuse )
+
+=cut
+
+sub regex_lang_subtags {
+ my $string = shift;
+
+ # Regex for recognizing RFC 4646 well-formed tags
+ # http://www.rfc-editor.org/rfc/rfc4646.txt
+
+ # regexes based on : http://unicode.org/cldr/data/tools/java/org/unicode/cldr/util/data/langtagRegex.txt
+ # The structure requires no forward references, so it reverses the order.
+ # The uppercase comments are fragments copied from RFC 4646
+ #
+ # Note: the tool requires that any real "=" or "#" or ";" in the regex be escaped.
+
+ my $alpha = qr/[a-zA-Z]/ ; # ALPHA
+ my $digit = qr/[0-9]/ ; # DIGIT
+ my $alphanum = qr/[a-zA-Z0-9]/ ; # ALPHA / DIGIT
+ my $x = qr/[xX]/ ; # private use singleton
+ my $singleton = qr/[a-w y-z A-W Y-Z]/ ; # other singleton
+ my $s = qr/[-]/ ; # separator -- lenient parsers will use [-_]
+
+ # Now do the components. The structure is slightly different to allow for capturing the right components.
+ # The notation (?:....) is a non-capturing version of (...): so the "?:" can be deleted if someone doesn't care about capturing.
+
+ my $extlang = qr{(?: $s $alpha{3} )}x ; # *3("-" 3ALPHA)
+ my $language = qr{(?: $alpha{2,3} | $alpha{4,8} )}x ;
+ #my $language = qr{(?: $alpha{2,3}$extlang{0,3} | $alpha{4,8} )}x ; # (2*3ALPHA [ extlang ]) / 4ALPHA / 5*8ALPHA
+
+ my $script = qr{(?: $alpha{4} )}x ; # 4ALPHA
+
+ my $region = qr{(?: $alpha{2} | $digit{3} )}x ; # 2ALPHA / 3DIGIT
+
+ my $variantSub = qr{(?: $digit$alphanum{3} | $alphanum{5,8} )}x ; # *("-" variant), 5*8alphanum / (DIGIT 3alphanum)
+ my $variant = qr{(?: $variantSub (?: $s$variantSub )* )}x ; # *("-" variant), 5*8alphanum / (DIGIT 3alphanum)
+
+ my $extensionSub = qr{(?: $singleton (?: $s$alphanum{2,8} )+ )}x ; # singleton 1*("-" (2*8alphanum))
+ my $extension = qr{(?: $extensionSub (?: $s$extensionSub )* )}x ; # singleton 1*("-" (2*8alphanum))
+
+ my $privateuse = qr{(?: $x (?: $s$alphanum{1,8} )+ )}x ; # ("x"/"X") 1*("-" (1*8alphanum))
+
+ # Define certain grandfathered codes, since otherwise the regex is pretty useless.
+ # Since these are limited, this is safe even later changes to the registry --
+ # the only oddity is that it might change the type of the tag, and thus
+ # the results from the capturing groups.
+ # http://www.iana.org/assignments/language-subtag-registry
+ # Note that these have to be compared case insensitively, requiring (?i) below.
+
+ my $grandfathered = qr{(?: (?i)
+ en $s GB $s oed
+ | i $s (?: ami | bnn | default | enochian | hak | klingon | lux | mingo | navajo | pwn | tao | tay | tsu )
+ | sgn $s (?: BE $s fr | BE $s nl | CH $s de)
+)}x;
+
+ # For well-formedness, we don't need the ones that would otherwise pass, so they are commented out here
+
+ # | art $s lojban
+ # | cel $s gaulish
+ # | en $s (?: boont | GB $s oed | scouse )
+ # | no $s (?: bok | nyn)
+ # | zh $s (?: cmn | cmn $s Hans | cmn $s Hant | gan | guoyu | hakka | min | min $s nan | wuu | xiang | yue)
+
+ # Here is the final breakdown, with capturing groups for each of these components
+ # The language, variants, extensions, grandfathered, and private-use may have interior '-'
+
+ #my $root = qr{(?: ($language) (?: $s ($script) )? 40% (?: $s ($region) )? 40% (?: $s ($variant) )? 10% (?: $s ($extension) )? 5% (?: $s ($privateuse) )? 5% ) 90% | ($grandfathered) 5% | ($privateuse) 5% };
+
+ $string =~ qr{^ (?:($language)) (?:$s($script))? (?:$s($region))? (?:$s($variant))? (?:$s($extension))? (?:$s($privateuse))? $}xi; # |($grandfathered) | ($privateuse) $}xi;
+ my %subtag = (
+ 'rfc4646_subtag' => $string,
+ 'language' => $1,
+ 'script' => $2,
+ 'region' => $3,
+ 'variant' => $4,
+ 'extension' => $5,
+ 'privateuse' => $6,
+ );
+ return \%subtag;
+}
+
+# Script Direction Resources:
+# http://www.w3.org/International/questions/qa-scripts
+sub get_bidi {
+ my ($language_script)= @_;
+ my $dbh = C4::Context->dbh;
+ my $bidi;
+ my $sth = $dbh->prepare('SELECT bidi FROM language_script_bidi WHERE rfc4646_subtag=?');
+ $sth->execute($language_script);
+ while (my $result = $sth->fetchrow_hashref) {
+ $bidi = $result->{'bidi'};
+ }
+ return $bidi;
+};
+
+sub accept_language {
+ # referenced http://search.cpan.org/src/CGILMORE/I18N-AcceptLanguage-1.04/lib/I18N/AcceptLanguage.pm
+ my ($clientPreferences,$supportedLanguages) = @_;
+ my @languages = ();
+ if ($clientPreferences) {
+ # There should be no whitespace anways, but a cleanliness/sanity check
+ $clientPreferences =~ s/\s//g;
+ # Prepare the list of client-acceptable 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) });
+ }
+ } else {
+ carp "accept_language(x,y) called with no clientPreferences (x).";
+ }
+ # Prepare the list of server-supported languages
+ my %supportedLanguages = ();
+ my %secondaryLanguages = ();
+ foreach my $language (@$supportedLanguages) {
+ # warn "Language supported: " . $language->{language};
+ my $subtag = $language->{rfc4646_subtag};
+ $supportedLanguages{lc($subtag)} = $subtag;
+ if ( $subtag =~ /^([^-]+)-?/ ) {
+ $secondaryLanguages{lc($1)} = $subtag;
+ }
+ }
+
+ # 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 '') {
+ # FIXME: We just checked the exact same conditional!
+ # 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;
+ return undef; # else, we got nothing.
+}