From b996f2f722e069d479b6f948c4a7e271beab166a Mon Sep 17 00:00:00 2001 From: "gerv%gerv.net" <> Date: Fri, 24 Jan 2003 07:34:00 +0000 Subject: Bug 126955 - Bugzilla should support translated/localized templates. Patch by burnus; r=gerv, a=justdave. --- Bugzilla/Template.pm | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) (limited to 'Bugzilla/Template.pm') diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index abcfd3ff6..434785332 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -22,6 +22,8 @@ # Jacob Steenhagen # Bradley Baetz # Christopher Aillon +# Tobias Burnus + package Bugzilla::Template; @@ -35,6 +37,65 @@ use Date::Format (); use base qw(Template); +my $template_include_path; + +# Make an ordered list out of a HTTP Accept-Language header see RFC 2616, 14.4 +# We ignore '*' and ;q=0 +# For languages with the same priority q the order remains unchanged. +sub sortAcceptLanguage { + sub sortQvalue { $b->{'qvalue'} <=> $a->{'qvalue'} } + my $accept_language = $_[0]; + + # clean up string. + $accept_language =~ s/[^A-Za-z;q=0-9\.\-,]//g; + my @qlanguages; + my @languages; + foreach(split /,/, $accept_language) { + if (m/([A-Za-z\-]+)(?:;q=(\d(?:\.\d+)))?/) { + my $lang = $1; + my $qvalue = $2; + $qvalue = 1 if not defined $qvalue; + next if $qvalue == 0; + $qvalue = 1 if $qvalue > 1; + push(@qlanguages, {'qvalue' => $qvalue, 'language' => $lang}); + } + } + + return map($_->{'language'}, (sort sortQvalue @qlanguages)); +} + +# Returns the path to the templates based on the Accept-Language +# settings of the user and of the available languages +# If no Accept-Language is present it uses the defined default +sub getTemplateIncludePath () { + # Return cached value if available + if ($template_include_path) { + return $template_include_path; + } + my $languages = trim(Param('languages')); + if (not ($languages =~ /,/)) { + return $template_include_path = + ["template/$languages/custom", "template/$languages/default"]; + } + my @languages = sortAcceptLanguage($languages); + my @accept_language = sortAcceptLanguage($ENV{'HTTP_ACCEPT_LANGUAGE'} || "" ); + my @usedlanguages; + foreach my $lang (@accept_language) { + # Per RFC 1766 and RFC 2616 any language tag matches also its + # primary tag. That is 'en' (accept lanuage) matches 'en-us', + # 'en-uk' etc. but not the otherway round. (This is unfortunally + # not very clearly stated in those RFC; see comment just over 14.5 + # in http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4) + if(my @found = grep /^$lang(-.+)?$/i, @languages) { + push (@usedlanguages, @found); + } + } + push(@usedlanguages, Param('defaultlanguage')); + return $template_include_path = + [map(("template/$_/custom", "template/$_/default"), @usedlanguages)]; +} + + ############################################################################### # Templatization Code @@ -100,7 +161,7 @@ sub create { return $class->new({ # Colon-separated list of directories containing templates. - INCLUDE_PATH => "template/en/custom:template/en/default", + INCLUDE_PATH => [\&getTemplateIncludePath], # Remove white-space before template directives (PRE_CHOMP) and at the # beginning and end of templates and template blocks (TRIM) for better -- cgit v1.2.3-24-g4f1b