| File | C4/Languages.pm | Statements Executed | 635 | Total Time | 0.00971400000000001 seconds |
| Calls | Inclusive Time | Subroutine | |
|---|---|---|---|
| 1 | 0.01548 | C4::Languages:: | getTranslatedLanguages |
| 1 | 0.01275 | C4::Languages:: | getAllLanguages |
| 1 | 0.00202 | C4::Languages:: | _build_languages_arrayref |
| 2 | 0.00050 | C4::Languages:: | regex_lang_subtags |
| 1 | 0.00027 | C4::Languages:: | _get_language_dirs |
| 0 | 0 | C4::Languages:: | BEGIN |
| 0 | 0 | C4::Languages:: | _get_themes |
| 0 | 0 | C4::Languages:: | accept_language |
| 0 | 0 | C4::Languages:: | getFrameworkLanguages |
| 0 | 0 | C4::Languages:: | get_bidi |
| 0 | 0 | C4::Languages:: | language_get_description |
| Line | Stmts. | Exclusive Time | Avg. | Code |
|---|---|---|---|---|
| 1 | package C4::Languages; | |||
| 2 | ||||
| 3 | # Copyright 2006 (C) LibLime | |||
| 4 | # Joshua Ferraro <jmf@liblime.com> | |||
| 5 | # | |||
| 6 | # This file is part of Koha. | |||
| 7 | # | |||
| 8 | # Koha is free software; you can redistribute it and/or modify it under the | |||
| 9 | # terms of the GNU General Public License as published by the Free Software | |||
| 10 | # Foundation; either version 2 of the License, or (at your option) any later | |||
| 11 | # version. | |||
| 12 | # | |||
| 13 | # Koha is distributed in the hope that it will be useful, but WITHOUT ANY | |||
| 14 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | |||
| 15 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |||
| 16 | # | |||
| 17 | # You should have received a copy of the GNU General Public License along with | |||
| 18 | # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, | |||
| 19 | # Suite 330, Boston, MA 02111-1307 USA | |||
| 20 | ||||
| 21 | ||||
| 22 | use strict; | |||
| 23 | #use warnings; #FIXME: turn off warnings before release | |||
| 24 | use Carp; | |||
| 25 | use C4::Context; | |||
| 26 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $DEBUG); | |||
| 27 | ||||
| 28 | BEGIN { | |||
| 29 | $VERSION = 3.00; | |||
| 30 | require Exporter; | |||
| 31 | @ISA = qw(Exporter); | |||
| 32 | @EXPORT = qw( | |||
| 33 | &getFrameworkLanguages | |||
| 34 | &getTranslatedLanguages | |||
| 35 | &getAllLanguages | |||
| 36 | ); | |||
| 37 | @EXPORT_OK = qw(getFrameworkLanguages getTranslatedLanguages getAllLanguages get_bidi regex_lang_subtags language_get_description accept_language); | |||
| 38 | $DEBUG = 0; | |||
| 39 | } | |||
| 40 | ||||
| 41 | =head1 NAME | |||
| 42 | ||||
| 43 | C4::Languages - Perl Module containing language list functions for Koha | |||
| 44 | ||||
| 45 | =head1 SYNOPSIS | |||
| 46 | ||||
| 47 | use C4::Languages; | |||
| 48 | ||||
| 49 | =head1 DESCRIPTION | |||
| 50 | ||||
| 51 | =head1 FUNCTIONS | |||
| 52 | ||||
| 53 | =head2 getFrameworkLanguages | |||
| 54 | ||||
| 55 | Returns a reference to an array of hashes: | |||
| 56 | ||||
| 57 | my $languages = getFrameworkLanguages(); | |||
| 58 | for my $language(@$languages) { | |||
| 59 | print "$language->{language_code}\n"; # language code in iso 639-2 | |||
| 60 | print "$language->{language_name}\n"; # language name in native script | |||
| 61 | print "$language->{language_locale_name}\n"; # language name in current locale | |||
| 62 | } | |||
| 63 | ||||
| 64 | =cut | |||
| 65 | our $memd; | |||
| 66 | if (C4::Context->preference('usecache')){ | |||
| 67 | require Cache::Memcached; | |||
| 68 | Cache::Memcached->import(); | |||
| 69 | $memd = new Cache::Memcached( | |||
| 70 | 'servers'=>['127.0.0.1:11211'], | |||
| 71 | ); | |||
| 72 | } | |||
| 73 | ||||
| 74 | ||||
| 75 | ||||
| 76 | sub getFrameworkLanguages { | |||
| 77 | # get a hash with all language codes, names, and locale names | |||
| 78 | my $all_languages = getAllLanguages(); | |||
| 79 | my @languages; | |||
| 80 | ||||
| 81 | # find the available directory names | |||
| 82 | my $dir=C4::Context->config('intranetdir')."/installer/data/"; | |||
| 83 | opendir (MYDIR,$dir); | |||
| 84 | my @listdir= grep { !/^\.|CVS/ && -d "$dir/$_"} readdir(MYDIR); | |||
| 85 | closedir MYDIR; | |||
| 86 | ||||
| 87 | # pull out all data for the dir names that exist | |||
| 88 | for my $dirname (@listdir) { | |||
| 89 | for my $language_set (@$all_languages) { | |||
| 90 | ||||
| 91 | if ($dirname eq $language_set->{language_code}) { | |||
| 92 | push @languages, { | |||
| 93 | 'language_code'=>$dirname, | |||
| 94 | 'language_description'=>$language_set->{language_description}, | |||
| 95 | 'native_descrition'=>$language_set->{language_native_description} } | |||
| 96 | } | |||
| 97 | } | |||
| 98 | } | |||
| 99 | return \@languages; | |||
| 100 | } | |||
| 101 | ||||
| 102 | =head2 getTranslatedLanguages | |||
| 103 | ||||
| 104 | Returns a reference to an array of hashes: | |||
| 105 | ||||
| 106 | my $languages = getTranslatedLanguages(); | |||
| 107 | print "Available translated languages:\n"; | |||
| 108 | for my $language(@$trlanguages) { | |||
| 109 | print "$language->{language_code}\n"; # language code in iso 639-2 | |||
| 110 | print "$language->{language_name}\n"; # language name in native script | |||
| 111 | print "$language->{language_locale_name}\n"; # language name in current locale | |||
| 112 | } | |||
| 113 | ||||
| 114 | =cut | |||
| 115 | ||||
| 116 | # spent 0.01548s within C4::Languages::getTranslatedLanguages which was called:
# 1 times (0.01548s) by C4::Output::gettemplate at line 117 of C4/Output.pm sub getTranslatedLanguages { | |||
| 117 | 7 | 0.00002 | 2e-06 | my ($interface, $theme, $current_language, $which) = @_; |
| 118 | my $htdocs; | |||
| 119 | my $all_languages = getAllLanguages(); # spent 0.01275s making 1 calls to C4::Languages::getAllLanguages | |||
| 120 | my @languages; | |||
| 121 | my $lang; | |||
| 122 | my @enabled_languages; | |||
| 123 | ||||
| 124 | 3 | 0.00005 | 0.00002 | if ($interface && $interface eq 'opac' ) { |
| 125 | @enabled_languages = split ",", C4::Context->preference('opaclanguages'); # spent 0.00030s making 1 calls to C4::Context::preference | |||
| 126 | $htdocs = C4::Context->config('opachtdocs'); # spent 0.00002s making 1 calls to C4::Context::config | |||
| 127 | 2 | 0.00006 | 0.00003 | if ( $theme and -d "$htdocs/$theme" ) { |
| 128 | (@languages) = _get_language_dirs($htdocs,$theme); # spent 0.00027s making 1 calls to C4::Languages::_get_language_dirs | |||
| 129 | return _build_languages_arrayref($all_languages,\@languages,$current_language,\@enabled_languages); # spent 0.00202s making 1 calls to C4::Languages::_build_languages_arrayref | |||
| 130 | } | |||
| 131 | else { | |||
| 132 | for my $theme ( _get_themes('opac') ) { | |||
| 133 | push @languages, _get_language_dirs($htdocs,$theme); | |||
| 134 | } | |||
| 135 | return _build_languages_arrayref($all_languages,\@languages,$current_language,\@enabled_languages); | |||
| 136 | } | |||
| 137 | } | |||
| 138 | elsif ($interface && $interface eq 'intranet' ) { | |||
| 139 | @enabled_languages = split ",", C4::Context->preference('language'); | |||
| 140 | $htdocs = C4::Context->config('intrahtdocs'); | |||
| 141 | if ( $theme and -d "$htdocs/$theme" ) { | |||
| 142 | @languages = _get_language_dirs($htdocs,$theme); | |||
| 143 | return _build_languages_arrayref($all_languages,\@languages,$current_language,\@enabled_languages); | |||
| 144 | } | |||
| 145 | else { | |||
| 146 | foreach my $theme ( _get_themes('intranet') ) { | |||
| 147 | push @languages, _get_language_dirs($htdocs,$theme); | |||
| 148 | } | |||
| 149 | return _build_languages_arrayref($all_languages,\@languages,$current_language,\@enabled_languages); | |||
| 150 | } | |||
| 151 | } | |||
| 152 | else { | |||
| 153 | @enabled_languages = split ",", C4::Context->preference('opaclanguages'); | |||
| 154 | my $htdocs = C4::Context->config('intrahtdocs'); | |||
| 155 | foreach my $theme ( _get_themes('intranet') ) { | |||
| 156 | push @languages, _get_language_dirs($htdocs,$theme); | |||
| 157 | } | |||
| 158 | $htdocs = C4::Context->config('opachtdocs'); | |||
| 159 | foreach my $theme ( _get_themes('opac') ) { | |||
| 160 | push @languages, _get_language_dirs($htdocs,$theme); | |||
| 161 | } | |||
| 162 | my %seen; | |||
| 163 | $seen{$_}++ for @languages; | |||
| 164 | @languages = keys %seen; | |||
| 165 | return _build_languages_arrayref($all_languages,\@languages,$current_language,\@enabled_languages); | |||
| 166 | } | |||
| 167 | } | |||
| 168 | ||||
| 169 | =head2 getAllLanguages | |||
| 170 | ||||
| 171 | Returns a reference to an array of hashes: | |||
| 172 | ||||
| 173 | my $alllanguages = getAllLanguages(); | |||
| 174 | print "Available translated languages:\n"; | |||
| 175 | for my $language(@$alllanguages) { | |||
| 176 | print "$language->{language_code}\n"; | |||
| 177 | print "$language->{language_name}\n"; | |||
| 178 | print "$language->{language_locale_name}\n"; | |||
| 179 | } | |||
| 180 | ||||
| 181 | =cut | |||
| 182 | ||||
| 183 | # spent 0.01275s within C4::Languages::getAllLanguages which was called:
# 1 times (0.01275s) by C4::Languages::getTranslatedLanguages at line 119 of C4/Languages.pm sub getAllLanguages { | |||
| 184 | 7 | 0.00017 | 0.00002 | my @languages_loop; |
| 185 | my $dbh=C4::Context->dbh; # spent 0.00015s making 1 calls to C4::Context::dbh | |||
| 186 | my $current_language = shift || 'en'; | |||
| 187 | 1 | 6e-06 | 6e-06 | my $sth = $dbh->prepare('SELECT * FROM language_subtag_registry WHERE type=\'language\''); # spent 0.00006s making 1 calls to DBI::db::prepare |
| 188 | $sth->execute(); # spent 0.00006s making 1 calls to DBI::st::execute | |||
| 189 | while (my $language_subtag_registry = $sth->fetchrow_hashref) { # spent 0.00008s making 1 calls to DBI::st::fetchrow_hashref
# spent 0.00004s making 1 calls to DBI::common::FETCH
# spent 0.00001s making 1 calls to DBI::st::fetch | |||
| 190 | ||||
| 191 | # pull out all the script descriptions for each language | |||
| 192 | 150 | 0.00456 | 0.00003 | my $sth2= $dbh->prepare("SELECT * FROM language_descriptions LEFT JOIN language_rfc4646_to_iso639 on language_rfc4646_to_iso639.rfc4646_subtag = language_descriptions.subtag WHERE type='language' AND subtag =? AND language_descriptions.lang = ?"); # spent 0.00170s making 30 calls to DBI::db::prepare, avg 0.00006s/call |
| 193 | $sth2->execute($language_subtag_registry->{subtag},$current_language); # spent 0.00188s making 30 calls to DBI::st::execute, avg 0.00006s/call | |||
| 194 | ||||
| 195 | 1 | 0.00011 | 0.00011 | my $sth3 = $dbh->prepare("SELECT description FROM language_descriptions WHERE type='language' AND subtag=? AND lang=?"); # spent 0.00168s making 30 calls to DBI::db::prepare, avg 0.00006s/call |
| 196 | ||||
| 197 | # add the correct description info | |||
| 198 | while (my $language_descriptions = $sth2->fetchrow_hashref) { # spent 0.00089s making 30 calls to DBI::st::fetchrow_hashref, avg 0.00003s/call
# spent 0.00026s making 30 calls to DBI::common::FETCH, avg 9e-06s/call
# spent 0.00025s making 30 calls to DBI::st::fetch, avg 8e-06s/call | |||
| 199 | 150 | 0.00333 | 0.00002 | $sth3->execute($language_subtag_registry->{subtag},$language_subtag_registry->{subtag}); # spent 0.00163s making 30 calls to DBI::st::execute, avg 0.00005s/call |
| 200 | my $native_description; | |||
| 201 | while (my $description = $sth3->fetchrow_hashref) { # spent 0.00079s making 30 calls to DBI::st::fetchrow_hashref, avg 0.00003s/call
# spent 0.00022s making 30 calls to DBI::common::FETCH, avg 7e-06s/call
# spent 0.00021s making 30 calls to DBI::st::fetch, avg 7e-06s/call | |||
| 202 | 30 | 0.00072 | 0.00002 | $native_description = $description->{description}; # spent 0.00063s making 30 calls to DBI::st::fetchrow_hashref, avg 0.00002s/call
# spent 0.00021s making 30 calls to DBI::st::fetch, avg 7e-06s/call
# spent 0.00018s making 30 calls to DBI::common::FETCH, avg 6e-06s/call |
| 203 | } | |||
| 204 | ||||
| 205 | # fill in the ISO6329 code | |||
| 206 | $language_subtag_registry->{iso639_2_code} = $language_descriptions->{iso639_2_code}; | |||
| 207 | # fill in the native description of the language, as well as the current language's translation of that if it exists | |||
| 208 | 60 | 0.00009 | 1e-06 | if ($native_description) { # spent 0.00061s making 30 calls to DBI::st::fetchrow_hashref, avg 0.00002s/call
# spent 0.00019s making 30 calls to DBI::st::fetch, avg 6e-06s/call
# spent 0.00017s making 30 calls to DBI::common::FETCH, avg 6e-06s/call |
| 209 | $language_subtag_registry->{language_description} = $native_description; | |||
| 210 | $language_subtag_registry->{language_description}.=" ($language_descriptions->{description})" if $language_descriptions->{description}; | |||
| 211 | } | |||
| 212 | else { | |||
| 213 | $language_subtag_registry->{language_description} = $language_descriptions->{description}; | |||
| 214 | } | |||
| 215 | } | |||
| 216 | push @languages_loop, $language_subtag_registry; # spent 0.00066s making 30 calls to DBI::st::fetchrow_hashref, avg 0.00002s/call
# spent 0.00019s making 30 calls to DBI::st::fetch, avg 6e-06s/call
# spent 0.00017s making 30 calls to DBI::common::FETCH, avg 6e-06s/call | |||
| 217 | } | |||
| 218 | return \@languages_loop; | |||
| 219 | } | |||
| 220 | ||||
| 221 | =head2 _get_themes | |||
| 222 | ||||
| 223 | Internal function, returns an array of all available themes. | |||
| 224 | ||||
| 225 | (@themes) = &_get_themes('opac'); | |||
| 226 | (@themes) = &_get_themes('intranet'); | |||
| 227 | ||||
| 228 | =cut | |||
| 229 | ||||
| 230 | sub _get_themes { | |||
| 231 | my $interface = shift; | |||
| 232 | my $htdocs; | |||
| 233 | my @themes; | |||
| 234 | if ( $interface eq 'intranet' ) { | |||
| 235 | $htdocs = C4::Context->config('intrahtdocs'); | |||
| 236 | } | |||
| 237 | else { | |||
| 238 | $htdocs = C4::Context->config('opachtdocs'); | |||
| 239 | } | |||
| 240 | opendir D, "$htdocs"; | |||
| 241 | my @dirlist = readdir D; | |||
| 242 | foreach my $directory (@dirlist) { | |||
| 243 | # if there's an en dir, it's a valid theme | |||
| 244 | -d "$htdocs/$directory/en" and push @themes, $directory; | |||
| 245 | } | |||
| 246 | return @themes; | |||
| 247 | } | |||
| 248 | ||||
| 249 | =head2 _get_language_dirs | |||
| 250 | ||||
| 251 | Internal function, returns an array of directory names, excluding non-language directories | |||
| 252 | ||||
| 253 | =cut | |||
| 254 | ||||
| 255 | # spent 0.00027s within C4::Languages::_get_language_dirs which was called:
# 1 times (0.00027s) by C4::Languages::getTranslatedLanguages at line 128 of C4/Languages.pm sub _get_language_dirs { | |||
| 256 | 5 | 0.00006 | 0.00001 | my ($htdocs,$theme) = @_; |
| 257 | my @lang_strings; | |||
| 258 | opendir D, "$htdocs/$theme"; | |||
| 259 | for my $lang_string ( readdir D ) { | |||
| 260 | 175 | 0.00009 | 5e-07 | next if $lang_string =~/^\./; |
| 261 | next if $lang_string eq 'all'; | |||
| 262 | next if $lang_string =~/png$/; | |||
| 263 | next if $lang_string =~/css$/; | |||
| 264 | next if $lang_string =~/CVS$/; | |||
| 265 | next if $lang_string =~/\.txt$/i; #Don't read the readme.txt ! | |||
| 266 | next if $lang_string =~/img|images|famfam/; | |||
| 267 | push @lang_strings, $lang_string; | |||
| 268 | } | |||
| 269 | return (@lang_strings); | |||
| 270 | } | |||
| 271 | ||||
| 272 | =head2 _build_languages_arrayref | |||
| 273 | ||||
| 274 | Internal function for building the ref to array of hashes | |||
| 275 | ||||
| 276 | FIXME: this could be rewritten and simplified using map | |||
| 277 | ||||
| 278 | =cut | |||
| 279 | ||||
| 280 | # spent 0.00202s within C4::Languages::_build_languages_arrayref which was called:
# 1 times (0.00202s) by C4::Languages::getTranslatedLanguages at line 129 of C4/Languages.pm sub _build_languages_arrayref { | |||
| 281 | 2 | 9e-06 | 5e-06 | my ($all_languages,$translated_languages,$current_language,$enabled_languages) = @_; |
| 282 | 2 | 0.00001 | 7e-06 | if (C4::Context->preference('usecache')){ # spent 0.00028s making 1 calls to C4::Context::preference |
| 283 | my $languages = $memd->get("koha::C4::Languages::languages"); # spent 0.00172s making 1 calls to Cache::Memcached::get | |||
| 284 | if ($languages) { | |||
| 285 | return $languages; | |||
| 286 | } | |||
| 287 | } | |||
| 288 | my @translated_languages = @$translated_languages; | |||
| 289 | my @languages_loop; # the final reference to an array of hashrefs | |||
| 290 | my @enabled_languages = @$enabled_languages; | |||
| 291 | # how many languages are enabled, if one, take note, some contexts won't need to display it | |||
| 292 | my $one_language_enabled = 1 unless @enabled_languages > 1; | |||
| 293 | my %seen_languages; # the language tags we've seen | |||
| 294 | my %found_languages; | |||
| 295 | my $language_groups; | |||
| 296 | my $track_language_groups; | |||
| 297 | my $current_language_regex = regex_lang_subtags($current_language); | |||
| 298 | # Loop through the translated languages | |||
| 299 | for my $translated_language (@translated_languages) { | |||
| 300 | # separate the language string into its subtag types | |||
| 301 | my $language_subtags_hashref = regex_lang_subtags($translated_language); | |||
| 302 | ||||
| 303 | # is this language string 'enabled'? | |||
| 304 | for my $enabled_language (@enabled_languages) { | |||
| 305 | #warn "Checking out if $translated_language eq $enabled_language"; | |||
| 306 | $language_subtags_hashref->{'enabled'} = 1 if $translated_language eq $enabled_language; | |||
| 307 | } | |||
| 308 | ||||
| 309 | # group this language, key by langtag | |||
| 310 | $language_subtags_hashref->{'sublanguage_current'} = 1 if $translated_language eq $current_language; | |||
| 311 | $language_subtags_hashref->{'rfc4646_subtag'} = $translated_language; | |||
| 312 | $language_subtags_hashref->{'native_description'} = language_get_description($language_subtags_hashref->{language},$language_subtags_hashref->{language},'language'); | |||
| 313 | $language_subtags_hashref->{'script_description'} = language_get_description($language_subtags_hashref->{script},$language_subtags_hashref->{'language'},'script'); | |||
| 314 | $language_subtags_hashref->{'region_description'} = language_get_description($language_subtags_hashref->{region},$language_subtags_hashref->{'language'},'region'); | |||
| 315 | $language_subtags_hashref->{'variant_description'} = language_get_description($language_subtags_hashref->{variant},$language_subtags_hashref->{'language'},'variant'); | |||
| 316 | $track_language_groups->{$language_subtags_hashref->{'language'}}++; | |||
| 317 | push ( @{ $language_groups->{$language_subtags_hashref->{language}} }, $language_subtags_hashref ); | |||
| 318 | } | |||
| 319 | # $key is a language subtag like 'en' | |||
| 320 | while( my ($key, $value) = each %$language_groups) { | |||
| 321 | ||||
| 322 | # is this language group enabled? are any of the languages within it enabled? | |||
| 323 | my $enabled; | |||
| 324 | for my $enabled_language (@enabled_languages) { | |||
| 325 | my $regex_enabled_language = regex_lang_subtags($enabled_language); | |||
| 326 | $enabled = 1 if $key eq $regex_enabled_language->{language}; | |||
| 327 | } | |||
| 328 | push @languages_loop, { | |||
| 329 | # this is only use if there is one | |||
| 330 | rfc4646_subtag => @$value[0]->{rfc4646_subtag}, | |||
| 331 | native_description => language_get_description($key,$key,'language'), | |||
| 332 | language => $key, | |||
| 333 | sublanguages_loop => $value, | |||
| 334 | plural => $track_language_groups->{$key} >1 ? 1 : 0, | |||
| 335 | current => $current_language_regex->{language} eq $key ? 1 : 0, | |||
| 336 | group_enabled => $enabled, | |||
| 337 | one_language_enabled => $one_language_enabled, | |||
| 338 | }; | |||
| 339 | } | |||
| 340 | if (C4::Context->preference('usecache')){ | |||
| 341 | $memd->set("koha::C4::Languages::languages",\@languages_loop); | |||
| 342 | } | |||
| 343 | return \@languages_loop; | |||
| 344 | } | |||
| 345 | ||||
| 346 | sub language_get_description { | |||
| 347 | my ($script,$lang,$type) = @_; | |||
| 348 | my $dbh = C4::Context->dbh; | |||
| 349 | my $desc; | |||
| 350 | my $sth = $dbh->prepare("SELECT description FROM language_descriptions WHERE subtag=? AND lang=? AND type=?"); | |||
| 351 | #warn "QUERY: SELECT description FROM language_descriptions WHERE subtag=$script AND lang=$lang AND type=$type"; | |||
| 352 | $sth->execute($script,$lang,$type); | |||
| 353 | while (my $descriptions = $sth->fetchrow_hashref) { | |||
| 354 | $desc = $descriptions->{'description'}; | |||
| 355 | } | |||
| 356 | unless ($desc) { | |||
| 357 | $sth = $dbh->prepare("SELECT description FROM language_descriptions WHERE subtag=? AND lang=? AND type=?"); | |||
| 358 | $sth->execute($script,'en',$type); | |||
| 359 | while (my $descriptions = $sth->fetchrow_hashref) { | |||
| 360 | $desc = $descriptions->{'description'}; | |||
| 361 | } | |||
| 362 | } | |||
| 363 | return $desc; | |||
| 364 | } | |||
| 365 | =head2 regex_lang_subtags | |||
| 366 | ||||
| 367 | This internal sub takes a string composed according to RFC 4646 as | |||
| 368 | an input and returns a reference to a hash containing keys and values | |||
| 369 | for ( language, script, region, variant, extension, privateuse ) | |||
| 370 | ||||
| 371 | =cut | |||
| 372 | ||||
| 373 | sub regex_lang_subtags { | |||
| 374 | 40 | 0.00041 | 0.00001 | my $string = shift; |
| 375 | ||||
| 376 | # Regex for recognizing RFC 4646 well-formed tags | |||
| 377 | # http://www.rfc-editor.org/rfc/rfc4646.txt | |||
| 378 | ||||
| 379 | # regexes based on : http://unicode.org/cldr/data/tools/java/org/unicode/cldr/util/data/langtagRegex.txt | |||
| 380 | # The structure requires no forward references, so it reverses the order. | |||
| 381 | # The uppercase comments are fragments copied from RFC 4646 | |||
| 382 | # | |||
| 383 | # Note: the tool requires that any real "=" or "#" or ";" in the regex be escaped. | |||
| 384 | ||||
| 385 | my $alpha = qr/[a-zA-Z]/ ; # ALPHA | |||
| 386 | my $digit = qr/[0-9]/ ; # DIGIT | |||
| 387 | my $alphanum = qr/[a-zA-Z0-9]/ ; # ALPHA / DIGIT | |||
| 388 | my $x = qr/[xX]/ ; # private use singleton | |||
| 389 | my $singleton = qr/[a-w y-z A-W Y-Z]/ ; # other singleton | |||
| 390 | my $s = qr/[-]/ ; # separator -- lenient parsers will use [-_] | |||
| 391 | ||||
| 392 | # Now do the components. The structure is slightly different to allow for capturing the right components. | |||
| 393 | # The notation (?:....) is a non-capturing version of (...): so the "?:" can be deleted if someone doesn't care about capturing. | |||
| 394 | ||||
| 395 | my $extlang = qr{(?: $s $alpha{3} )}x ; # *3("-" 3ALPHA) | |||
| 396 | my $language = qr{(?: $alpha{2,3} | $alpha{4,8} )}x ; | |||
| 397 | #my $language = qr{(?: $alpha{2,3}$extlang{0,3} | $alpha{4,8} )}x ; # (2*3ALPHA [ extlang ]) / 4ALPHA / 5*8ALPHA | |||
| 398 | ||||
| 399 | my $script = qr{(?: $alpha{4} )}x ; # 4ALPHA | |||
| 400 | ||||
| 401 | my $region = qr{(?: $alpha{2} | $digit{3} )}x ; # 2ALPHA / 3DIGIT | |||
| 402 | ||||
| 403 | my $variantSub = qr{(?: $digit$alphanum{3} | $alphanum{5,8} )}x ; # *("-" variant), 5*8alphanum / (DIGIT 3alphanum) | |||
| 404 | my $variant = qr{(?: $variantSub (?: $s$variantSub )* )}x ; # *("-" variant), 5*8alphanum / (DIGIT 3alphanum) | |||
| 405 | ||||
| 406 | my $extensionSub = qr{(?: $singleton (?: $s$alphanum{2,8} )+ )}x ; # singleton 1*("-" (2*8alphanum)) | |||
| 407 | my $extension = qr{(?: $extensionSub (?: $s$extensionSub )* )}x ; # singleton 1*("-" (2*8alphanum)) | |||
| 408 | ||||
| 409 | my $privateuse = qr{(?: $x (?: $s$alphanum{1,8} )+ )}x ; # ("x"/"X") 1*("-" (1*8alphanum)) | |||
| 410 | ||||
| 411 | # Define certain grandfathered codes, since otherwise the regex is pretty useless. | |||
| 412 | # Since these are limited, this is safe even later changes to the registry -- | |||
| 413 | # the only oddity is that it might change the type of the tag, and thus | |||
| 414 | # the results from the capturing groups. | |||
| 415 | # http://www.iana.org/assignments/language-subtag-registry | |||
| 416 | # Note that these have to be compared case insensitively, requiring (?i) below. | |||
| 417 | ||||
| 418 | my $grandfathered = qr{(?: (?i) | |||
| 419 | en $s GB $s oed | |||
| 420 | | i $s (?: ami | bnn | default | enochian | hak | klingon | lux | mingo | navajo | pwn | tao | tay | tsu ) | |||
| 421 | | sgn $s (?: BE $s fr | BE $s nl | CH $s de) | |||
| 422 | )}x; | |||
| 423 | ||||
| 424 | # For well-formedness, we don't need the ones that would otherwise pass, so they are commented out here | |||
| 425 | ||||
| 426 | # | art $s lojban | |||
| 427 | # | cel $s gaulish | |||
| 428 | # | en $s (?: boont | GB $s oed | scouse ) | |||
| 429 | # | no $s (?: bok | nyn) | |||
| 430 | # | zh $s (?: cmn | cmn $s Hans | cmn $s Hant | gan | guoyu | hakka | min | min $s nan | wuu | xiang | yue) | |||
| 431 | ||||
| 432 | # Here is the final breakdown, with capturing groups for each of these components | |||
| 433 | # The language, variants, extensions, grandfathered, and private-use may have interior '-' | |||
| 434 | ||||
| 435 | #my $root = qr{(?: ($language) (?: $s ($script) )? 40% (?: $s ($region) )? 40% (?: $s ($variant) )? 10% (?: $s ($extension) )? 5% (?: $s ($privateuse) )? 5% ) 90% | ($grandfathered) 5% | ($privateuse) 5% }; | |||
| 436 | ||||
| 437 | $string =~ qr{^ (?:($language)) (?:$s($script))? (?:$s($region))? (?:$s($variant))? (?:$s($extension))? (?:$s($privateuse))? $}xi; # |($grandfathered) | ($privateuse) $}xi; | |||
| 438 | my %subtag = ( | |||
| 439 | 'rfc4646_subtag' => $string, | |||
| 440 | 'language' => $1, | |||
| 441 | 'script' => $2, | |||
| 442 | 'region' => $3, | |||
| 443 | 'variant' => $4, | |||
| 444 | 'extension' => $5, | |||
| 445 | 'privateuse' => $6, | |||
| 446 | ); | |||
| 447 | return \%subtag; | |||
| 448 | } | |||
| 449 | ||||
| 450 | # Script Direction Resources: | |||
| 451 | # http://www.w3.org/International/questions/qa-scripts | |||
| 452 | sub get_bidi { | |||
| 453 | my ($language_script)= @_; | |||
| 454 | my $dbh = C4::Context->dbh; | |||
| 455 | my $bidi; | |||
| 456 | my $sth = $dbh->prepare('SELECT bidi FROM language_script_bidi WHERE rfc4646_subtag=?'); | |||
| 457 | $sth->execute($language_script); | |||
| 458 | while (my $result = $sth->fetchrow_hashref) { | |||
| 459 | $bidi = $result->{'bidi'}; | |||
| 460 | } | |||
| 461 | return $bidi; | |||
| 462 | }; | |||
| 463 | ||||
| 464 | sub accept_language { | |||
| 465 | # referenced http://search.cpan.org/src/CGILMORE/I18N-AcceptLanguage-1.04/lib/I18N/AcceptLanguage.pm | |||
| 466 | # FIXME: since this is only used in Output.pm as of Jan 8 2008, maybe it should be IN Output.pm | |||
| 467 | my ($clientPreferences,$supportedLanguages) = @_; | |||
| 468 | my @languages = (); | |||
| 469 | if ($clientPreferences) { | |||
| 470 | # There should be no whitespace anways, but a cleanliness/sanity check | |||
| 471 | $clientPreferences =~ s/\s//g; | |||
| 472 | ||||
| 473 | # Prepare the list of client-acceptable languages | |||
| 474 | foreach my $tag (split(/,/, $clientPreferences)) { | |||
| 475 | my ($language, $quality) = split(/\;/, $tag); | |||
| 476 | $quality =~ s/^q=//i if $quality; | |||
| 477 | $quality = 1 unless $quality; | |||
| 478 | next if $quality <= 0; | |||
| 479 | # We want to force the wildcard to be last | |||
| 480 | $quality = 0 if ($language eq '*'); | |||
| 481 | # Pushing lowercase language here saves processing later | |||
| 482 | push(@languages, { quality => $quality, | |||
| 483 | language => $language, | |||
| 484 | lclanguage => lc($language) }); | |||
| 485 | } | |||
| 486 | } else { | |||
| 487 | carp "accept_language(x,y) called with no clientPreferences (x)."; | |||
| 488 | } | |||
| 489 | # Prepare the list of server-supported languages | |||
| 490 | my %supportedLanguages = (); | |||
| 491 | my %secondaryLanguages = (); | |||
| 492 | foreach my $language (@$supportedLanguages) { | |||
| 493 | # warn "Language supported: " . $language->{language_code}; | |||
| 494 | $supportedLanguages{lc($language->{language_code})} = $language->{language_code}; | |||
| 495 | if ($language->{language_code} =~ /^([^-]+)-?/) { | |||
| 496 | $secondaryLanguages{lc($1)} = $language->{language_code}; | |||
| 497 | } | |||
| 498 | } | |||
| 499 | ||||
| 500 | # Reverse sort the list, making best quality at the front of the array | |||
| 501 | @languages = sort { $b->{quality} <=> $a->{quality} } @languages; | |||
| 502 | my $secondaryMatch = ''; | |||
| 503 | foreach my $tag (@languages) { | |||
| 504 | if (exists($supportedLanguages{$tag->{lclanguage}})) { | |||
| 505 | # Client en-us eq server en-us | |||
| 506 | return $supportedLanguages{$tag->{language}} if exists($supportedLanguages{$tag->{language}}); | |||
| 507 | return $supportedLanguages{$tag->{lclanguage}}; | |||
| 508 | } elsif (exists($secondaryLanguages{$tag->{lclanguage}})) { | |||
| 509 | # Client en eq server en-us | |||
| 510 | return $secondaryLanguages{$tag->{language}} if exists($secondaryLanguages{$tag->{language}}); | |||
| 511 | return $supportedLanguages{$tag->{lclanguage}}; | |||
| 512 | } elsif ($tag->{lclanguage} =~ /^([^-]+)-/ && exists($secondaryLanguages{$1}) && $secondaryMatch eq '') { | |||
| 513 | # Client en-gb eq server en-us | |||
| 514 | $secondaryMatch = $secondaryLanguages{$1}; | |||
| 515 | } elsif ($tag->{lclanguage} =~ /^([^-]+)-/ && exists($secondaryLanguages{$1}) && $secondaryMatch eq '') { | |||
| 516 | # FIXME: We just checked the exact same conditional! | |||
| 517 | # Client en-us eq server en | |||
| 518 | $secondaryMatch = $supportedLanguages{$1}; | |||
| 519 | } elsif ($tag->{lclanguage} eq '*') { | |||
| 520 | # * matches every language not already specified. | |||
| 521 | # It doesn't care which we pick, so let's pick the default, | |||
| 522 | # if available, then the first in the array. | |||
| 523 | #return $acceptor->defaultLanguage() if $acceptor->defaultLanguage(); | |||
| 524 | return $supportedLanguages->[0]; | |||
| 525 | } | |||
| 526 | } | |||
| 527 | # No primary matches. Secondary? (ie, en-us requested and en supported) | |||
| 528 | return $secondaryMatch if $secondaryMatch; | |||
| 529 | return undef; # else, we got nothing. | |||
| 530 | } | |||
| 531 | 1; | |||
| 532 | ||||
| 533 | __END__ | |||
| 534 | ||||
| 535 | =head1 AUTHOR | |||
| 536 | ||||
| 537 | Joshua Ferraro | |||
| 538 | ||||
| 539 | =cut |