Fix Bug 1058265 - Re-enable the Translation Bar using navigator.languages

This commit is contained in:
Kohei Yoshino 2014-08-25 17:32:50 -04:00
Родитель 2b0b6fab5b
Коммит 8b139390cf
5 изменённых файлов: 9 добавлений и 46 удалений

Просмотреть файл

@ -1027,7 +1027,7 @@ FIREFOX_TWITTER_ACCOUNTS = {
# Mapbox token for spaces and communities pages # Mapbox token for spaces and communities pages
MAPBOX_TOKEN = 'examples.map-i86nkdio' MAPBOX_TOKEN = 'examples.map-i86nkdio'
# TABZILLA_INFOBAR_OPTIONS = 'translation' TABZILLA_INFOBAR_OPTIONS = 'translation'
# Optimize.ly project code for base template JS snippet # Optimize.ly project code for base template JS snippet
OPTIMIZELY_PROJECT_ID = None OPTIMIZELY_PROJECT_ID = None

Просмотреть файл

@ -193,22 +193,6 @@ var Tabzilla = (function (Tabzilla) {
var removeCompactModeEvents = function () { var removeCompactModeEvents = function () {
nav.off('.submenu'); nav.off('.submenu');
}; };
Tabzilla.user = {};
Tabzilla.detectUserLang = function (callback) {
// Expose the user's accept languages in the HTTP Accept-Language
// request header, because the navigator.language property is just the
// application's locale on some browsers and the new navigator.languages
// property is not widely implemented yet.
$.ajax({ url: '/en-US/tabzilla/userlang.jsonp',
cache: true, crossDomain: true, dataType: 'jsonp',
jsonpCallback: "_", success: function (data) {
var lang = Tabzilla.user.languages = data.languages;
if (typeof callback === 'function') {
callback(lang);
}
}});
};
Tabzilla.open = function () { Tabzilla.open = function () {
opened = true; opened = true;
panel.toggleClass('open'); panel.toggleClass('open');
@ -392,7 +376,12 @@ var Tabzilla = (function (Tabzilla) {
Infobar.prototype.oncancel = {}; Infobar.prototype.oncancel = {};
Tabzilla.setupTransbar = function (userLangs, pageLang) { Tabzilla.setupTransbar = function (userLangs, pageLang) {
var transbar = new Infobar('transbar', 'Translation Bar'); var transbar = new Infobar('transbar', 'Translation Bar');
userLangs = userLangs || Tabzilla.user.languages;
// Note that navigator.language doesn't always work because it's just
// the application's locale on some browsers. navigator.languages has
// not been widely implemented yet, but the new property provides an
// array of the user's accept languages that we'd like to see.
userLangs = userLangs || navigator.languages;
pageLang = pageLang || document.documentElement.lang; pageLang = pageLang || document.documentElement.lang;
if (transbar.disabled || !userLangs || !pageLang) { if (transbar.disabled || !userLangs || !pageLang) {
@ -614,9 +603,7 @@ var Tabzilla = (function (Tabzilla) {
// Information Bars in order of priority // Information Bars in order of priority
var infobars = { var infobars = {
translation: function () { translation: Tabzilla.setupTransbar
Tabzilla.detectUserLang(Tabzilla.setupTransbar);
}
}; };
$.each((tab.data('infobar') || '').split(' '), function (index, value) { $.each((tab.data('infobar') || '').split(' '), function (index, value) {
var setup = infobars[value]; var setup = infobars[value];

Просмотреть файл

@ -10,5 +10,4 @@ import views
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^tabzilla\.js$', views.tabzilla_js, name='tabzilla'), url(r'^tabzilla\.js$', views.tabzilla_js, name='tabzilla'),
url(r'^transbar\.jsonp$', views.transbar_jsonp, name='transbar'), url(r'^transbar\.jsonp$', views.transbar_jsonp, name='transbar'),
url(r'^userlang\.jsonp$', views.userlang_jsonp, name='userlang'),
) )

Просмотреть файл

@ -2,15 +2,13 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os.path import os.path
from datetime import datetime from datetime import datetime
from django.conf import settings from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponseRedirect
from django.template import loader from django.template import loader
from django.views.decorators.http import last_modified from django.views.decorators.http import last_modified
from django.views.decorators.vary import vary_on_headers
from lib import l10n_utils from lib import l10n_utils
@ -62,16 +60,3 @@ def transbar_jsonp(request):
resp = _resp(request, 'tabzilla/transbar.jsonp', 'application/javascript') resp = _resp(request, 'tabzilla/transbar.jsonp', 'application/javascript')
resp['Access-Control-Allow-Origin'] = '*' resp['Access-Control-Allow-Origin'] = '*'
return resp return resp
@cache_control_expires(24 * 30)
@vary_on_headers('Accept-Language')
def userlang_jsonp(request):
data = {
'languages': l10n_utils.get_accept_languages(request)
}
resp = HttpResponse('_({0});'.format(json.dumps(data)),
content_type='application/javascript')
resp['Access-Control-Allow-Origin'] = '*'
return resp

Просмотреть файл

@ -79,12 +79,4 @@ Adding the Translation Bar extension to Tabzilla requires:
<a href="https://www.mozilla.org/" id="tabzilla" data-infobar="translation">mozilla</a> <a href="https://www.mozilla.org/" id="tabzilla" data-infobar="translation">mozilla</a>
The Translation Bar relies on ``Tabzilla.user.languages`` and this data can be used for other purposes. In your JavaScript code:
if (Tabzilla.user.languages) {
doSomething(Tabzilla.user.languages);
} else {
Tabzilla.detectUserLang(doSomething);
}
.. note:: Though the Translation Bar is currently implemented as an extension of Tabzilla, it might be moved to a standalone language utility in the future. .. note:: Though the Translation Bar is currently implemented as an extension of Tabzilla, it might be moved to a standalone language utility in the future.