Skip to content

Commit

Permalink
[Bug 1248594] Use MLS for GeoIP detection
Browse files Browse the repository at this point in the history
  • Loading branch information
brittanydionigi committed Feb 29, 2016
1 parent 1c61b5f commit f53907a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
61 changes: 25 additions & 36 deletions kitsune/sumo/static/sumo/js/geoip-locale.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,34 @@
/* globals $:false, interpolate:false, _gaq:false */
/* geo.mozilla.org will inject a couple functions that look like this, though
* with the correct country code/name for the current client.
*
* function geoip_country_code() { return 'US'; }
* function geoip_country_name() { return 'United States'; }
*/

(function() {
var cookieCountryName = $.cookie('geoip_country_name');
var cookieCountryCode = $.cookie('geoip_country_code');

if (cookieCountryName) {
window.geoip_country_name = function() {
return cookieCountryName;
};
} else if (window.geoip_country_name) {
// Cookie expires after 30 days.
$.cookie('geoip_country_name', window.geoip_country_name(), {expires: 30});
} else {
window.geoip_country_name = function() {
return 'Unknown';
};
}
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 (cookieCountryCode) {
window.geoip_country_code = function() {
return cookieCountryCode;
};
} else if (window.geoip_country_code) {
// Cookie expires after 30 days.
$.cookie('geoip_country_code', window.geoip_country_code(), {expires: 30});
if (!countryData.country_name) {
$.ajax({
method: 'GET',
url: GeoIPUrl
})
.done(function(data) {
$.cookie('geoip_country_name', data.country_name);
$.cookie('geoip_country_code', data.country_code);
countryData = data;
})
.fail(function(error) {
throw new Error('Error retrieving geoip data');
})
.always(function() {
handleLocale(countryData.country_name);
});
} else {
window.geoip_country_code = function() {
return '??';
};
handleLocale(countryData.country_name);
}
})();

(function() {
function handleLocale(countryName) {
// Mapping of {currentLocale: {country_name: suggested_locale}}
var languageSuggestions = {
'en-US': {
Expand All @@ -47,8 +38,7 @@
};

var currentLocale = $('html').attr('lang');
var currentCountry = window.geoip_country_name();
var suggestedLocale = (languageSuggestions[currentLocale] || {})[window.geoip_country_name()];
var suggestedLocale = (languageSuggestions[currentLocale] || {})[countryName];
var $announceBar = $('#announce-geoip-suggestion');

if (suggestedLocale) {
Expand Down Expand Up @@ -133,5 +123,4 @@
// If no locale should be suggested, the bar might still display, so remove it.
$announceBar.remove();
}

})();
}
4 changes: 0 additions & 4 deletions kitsune/sumo/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,6 @@
{% endif %}
{# End Optimizely #}

{% if include_geoip %}
<script src="//geo.mozilla.org/country.js" type="text/javascript"></script>
{% endif %}

{% javascript 'common' %}
<!--[if (gte IE 6)&(lte IE 8)]>
{% javascript 'ie6-8' %}
Expand Down

0 comments on commit f53907a

Please sign in to comment.