/
geoip-locale.js
133 lines (120 loc) · 4.88 KB
/
geoip-locale.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import "sumo/js/libs/jquery.cookie";
import trackEvent from "sumo/js/analytics";
/* Please do not directly use this code or SUMO key. */
/* Contact MLS team for your own credentials. */
/* https://location.services.mozilla.com/contact */
(function() {
var GeoIPUrl = 'https://location.services.mozilla.com/v1/country?key=fa6d7fc9-e091-4be1-b6c1-5ada5815ae9d';
var countryData = {
'country_name': $.cookie('geoip_country_name'),
'country_code': $.cookie('geoip_country_code')
};
if (!countryData.country_name) {
$.ajax({
method: 'GET',
url: GeoIPUrl,
beforeSend: function() {} // don't send X-CSRFToken header
})
.done(function(data) {
$.cookie('geoip_country_name', data.country_name, { path: '/' });
$.cookie('geoip_country_code', data.country_code, { path: '/' });
countryData = data;
})
.fail(function(error) {
console.warn('Error retrieving geoip data');
$('#announce-geoip-suggestion').remove();
})
.always(function() {
handleLocale(countryData.country_name);
});
} else {
handleLocale(countryData.country_name);
}
})();
function handleLocale(countryName) {
// Mapping of {currentLocale: {country_name: suggested_locale}}
var languageSuggestions = {
'en-US': {
Indonesia: 'id',
Bangladesh: 'bn',
},
};
var currentLocale = $('html').attr('lang');
var suggestedLocale = (languageSuggestions[currentLocale] || {})[countryName];
var $announceBar = $('#announce-geoip-suggestion');
if (suggestedLocale) {
/* Now get the localized versions of the prompt and response texts from
* the server. This can't be done with the normal localization route,
* because the localization the server gave won't have the suggested locale
* in it. The non-suggested text could be translated locally, but the code
* is cleaner when it is also translated on the server.
*/
$.ajax({
method: 'GET',
url: '/geoip-suggestion',
data: {
locales: [currentLocale, suggestedLocale],
}
})
.done(function(data) {
/* Get the translated strings in both the current language and the
* new (suggested) language. Show a prompt to switch to the new
* language in both languages, but only if the prompts are different.
* If the prompt is not localized, it will only show in the current
* language, instead of repeating the same message twice.
*/
var languageInCurrentLocale = data.locales[suggestedLocale][0];
var languageInNativeLocale = data.locales[suggestedLocale][1];
var currentLocaleSuggestion = interpolate(
data[currentLocale].suggestion,
{language: languageInCurrentLocale},
true);
var suggestedLocaleSuggestion = interpolate(
data[suggestedLocale].suggestion,
{language: languageInNativeLocale},
true);
$announceBar.show();
var $message = $announceBar.find('p');
$message.text(currentLocaleSuggestion);
$message.append($('<button class="sumo-button button-sm confirm" />').text(data[currentLocale].confirm));
$message.append($('<button class="sumo-button button-sm cancel" />').text(data[currentLocale].cancel));
if (data[currentLocale].suggestion !== data[suggestedLocale].suggestion) {
var $localisedMessage = $('<p />').appendTo($announceBar);
$localisedMessage.text(suggestedLocaleSuggestion);
$localisedMessage.append($('<button class="sumo-button button-sm confirm" />').text(data[suggestedLocale].confirm));
$localisedMessage.append($('<button class="sumo-button button-sm cancel" />').text(data[suggestedLocale].cancel));
}
trackEvent('geoip_targeting_banner_show');
})
.error(function(err) {
console.error('GeoIP suggestion error', err);
});
$announceBar.on('click', 'p button', function(ev) {
/* If the user clicks "yes", close the bar (so it remembers) and navigate
* to the new locale. If the user clicks "no", just close the bar.
* Either way, the announce bar UI code (in ui.js) will remember this action
* in local storage, so the bar won't ever show up again.
*/
var $this = $(this);
$announceBar.find('.close-button').trigger("click");
if ($this.hasClass('confirm')) {
trackEvent('geoip_targeting_banner_accept');
// Delay the click navigation by 250ms to ensure the event is tracked.
setTimeout(function() {
var newQsVar = 'lang=' + suggestedLocale;
if (window.location.search.length === 0) {
newQsVar = '?' + newQsVar;
} else {
newQsVar = '&' + newQsVar;
}
window.location.search += newQsVar;
}, 250);
} else {
trackEvent('geoip_targeting_banner_reject');
}
});
} else {
// If no locale should be suggested, the bar might still display, so remove it.
$announceBar.remove();
}
}