Skip to content

Instantly share code, notes, and snippets.

@pzp1997
Created April 1, 2016 21:51
Show Gist options
  • Save pzp1997/5de44543471254dabd9ee0e05162aec3 to your computer and use it in GitHub Desktop.
Save pzp1997/5de44543471254dabd9ee0e05162aec3 to your computer and use it in GitHub Desktop.
Gets the dictionary of languages to language codes that Yandex supports.
var KEY = '';
var getRequestSync = function(url) {
var xhr = new XMLHttpRequest();
var responseText;
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE)
responseText = xhr.status === 200 ? xhr.responseText : null;
};
xhr.open('GET', url, false);
xhr.send();
return responseText;
};
var swapKeyValues = function(dict) {
var d = {};
for (var key in dict) {
d[dict[key]] = key;
}
return d;
};
var baseUrl = 'https://translate.yandex.net/api/v1.5/tr.json/getLangs';
var url = baseUrl + '?key=' + KEY + '&ui=en';
var langCodes = swapKeyValues(JSON.parse(getRequestSync(url)).langs);
console.log(langCodes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment