Created
April 1, 2016 21:51
-
-
Save pzp1997/5de44543471254dabd9ee0e05162aec3 to your computer and use it in GitHub Desktop.
Gets the dictionary of languages to language codes that Yandex supports.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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