From 3606f75dd1d18e5a3419c0b679ba3288fd203cf5 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Wed, 18 Nov 2009 07:06:45 +0000 Subject: Bug 421265: Let the user easily override the language used to display HTML pages Patch by Jacques Supcik r=mkanat, a=mkanat --- Bugzilla/Install/Util.pm | 35 ++++++++++++++++++++++--- Bugzilla/Template.pm | 8 ++++++ js/global.js | 9 +++++++ skins/standard/global.css | 12 +++++++++ template/en/default/bug/create/create.html.tmpl | 2 +- template/en/default/global/header.html.tmpl | 34 ++++++++++++++++++++++-- 6 files changed, 94 insertions(+), 6 deletions(-) diff --git a/Bugzilla/Install/Util.pm b/Bugzilla/Install/Util.pm index e53164d25..effb39ee8 100644 --- a/Bugzilla/Install/Util.pm +++ b/Bugzilla/Install/Util.pm @@ -127,15 +127,28 @@ sub install_string { } sub include_languages { + # If we are in CGI mode (not in checksetup.pl) and if the function has + # been called without any parameter, then we cache the result of this + # function in Bugzilla->request_cache. This is done to improve the + # performance of the template processing. + my $to_be_cached = 0; + if (exists $ENV{'SERVER_SOFTWARE'} and not @_) { + my $cache = Bugzilla->request_cache; + if (exists $cache->{include_languages}) { + return @{$cache->{include_languages}} + } + $to_be_cached = 1; + } my ($params) = @_; $params ||= {}; # Basically, the way this works is that we have a list of languages # that we *want*, and a list of languages that Bugzilla actually # supports. The caller tells us what languages they want, by setting - # $ENV{HTTP_ACCEPT_LANGUAGE} or $params->{only_language}. The languages - # we support are those specified in $params->{use_languages}. Otherwise - # we support every language installed in the template/ directory. + # $ENV{HTTP_ACCEPT_LANGUAGE}, using the "LANG" cookie or setting + # $params->{only_language}. The languages we support are those + # specified in $params->{use_languages}. Otherwise we support every + # language installed in the template/ directory. my @wanted; if ($params->{only_language}) { @@ -143,6 +156,15 @@ sub include_languages { } else { @wanted = _sort_accept_language($ENV{'HTTP_ACCEPT_LANGUAGE'} || ''); + # Don't use the cookie if we are in "checksetup.pl". The test + # with $ENV{'SERVER_SOFTWARE'} is the same as in + # Bugzilla:Util::i_am_cgi. + if (exists $ENV{'SERVER_SOFTWARE'}) { + my $cgi = Bugzilla->cgi; + if (defined (my $lang = $cgi->cookie('LANG'))) { + unshift @wanted, $lang; + } + } } my @supported; @@ -175,6 +197,13 @@ sub include_languages { push(@usedlanguages, 'en'); } + # Cache the result if we are in CGI mode and called without parameter + # (see the comment at the top of this function). + if ($to_be_cached) { + my $cache = Bugzilla->request_cache; + $cache->{include_languages} = \@usedlanguages; + } + return @usedlanguages; } diff --git a/Bugzilla/Template.pm b/Bugzilla/Template.pm index fa7247243..ba0a035bb 100644 --- a/Bugzilla/Template.pm +++ b/Bugzilla/Template.pm @@ -729,6 +729,14 @@ sub create { # Currently logged in user, if any # If an sudo session is in progress, this is the user we're faking 'user' => sub { return Bugzilla->user; }, + + # Currenly active language + # XXX Eventually this should probably be replaced with something + # like Bugzilla->language. + 'current_language' => sub { + my ($language) = include_languages(); + return $language; + }, # If an sudo session is in progress, this is the user who # started the session. diff --git a/js/global.js b/js/global.js index 77e40d4c8..2d3974150 100644 --- a/js/global.js +++ b/js/global.js @@ -110,3 +110,12 @@ function check_mini_login_fields( suffix ) { window.alert( mini_login_constants.warning ); return false; } + +function set_language( value ) { + YAHOO.util.Cookie.set('LANG', value, + { + expires: new Date('January 1, 2038'), + path: BUGZILLA.param.cookie_path + }); + window.location.reload() +} diff --git a/skins/standard/global.css b/skins/standard/global.css index 64c73c3d5..8eb7cc3b1 100644 --- a/skins/standard/global.css +++ b/skins/standard/global.css @@ -61,6 +61,18 @@ padding: 0.5em; } + #lang_links_container { + float: right; + } + #lang_links_container .links { + border: none; + padding: .5em; + } + + .lang_current { + font-weight: bold; + } + #message { border: 1px solid red; margin: 0.3em 0em; diff --git a/template/en/default/bug/create/create.html.tmpl b/template/en/default/bug/create/create.html.tmpl index f466f7704..21f7959a2 100644 --- a/template/en/default/bug/create/create.html.tmpl +++ b/template/en/default/bug/create/create.html.tmpl @@ -33,7 +33,7 @@ style_urls = [ 'skins/standard/create_attachment.css', 'skins/standard/yui/calendar.css' ] javascript_urls = [ "js/attachment.js", "js/util.js", "js/yui/calendar.js", - "js/field.js", "js/yui/cookie.js", "js/TUI.js" ] + "js/field.js", "js/TUI.js" ] onload = 'set_assign_to();' %] diff --git a/template/en/default/global/header.html.tmpl b/template/en/default/global/header.html.tmpl index f8044976d..904a89d45 100644 --- a/template/en/default/global/header.html.tmpl +++ b/template/en/default/global/header.html.tmpl @@ -191,6 +191,7 @@ +