Skip to content

Instantly share code, notes, and snippets.

@AshKyd
Created July 26, 2014 15:01
Show Gist options
  • Save AshKyd/6c0cef0ebe03a1847408 to your computer and use it in GitHub Desktop.
Save AshKyd/6c0cef0ebe03a1847408 to your computer and use it in GitHub Desktop.
Create a list of countries and languages spoken from the "List of official languages by state" page on Wikipedia https://en.wikipedia.org/wiki/List_of_official_languages_by_state
var countries = [];
// These states have no official languages, so override them with these ones.
var override = {
"United Kingdom": ['English'],
"United States": ['English'],
"Australia": ['English'],
"Mexico": ['Spanish'],
"Chile": ['Spanish'],
}
function sanitiseName(name) {
return $.trim(name.match(/([^\(\[]*)/)[0]);
}
$('#mw-content-text > ul > li').each(function() {
var country = sanitiseName($('b:eq(0)', this).text());
var languages = [];
if (override[country]) {
languages = override[country];
} else {
$(this).find('>ul>li').each(function() {
languages.push(sanitiseName($(this).text()));
});
}
countries.push({
country: country,
languages: languages
});
});
console.log(JSON.stringify(countries));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment