$(document).on 'click', '.suggested-translation-link', () -> form = $(this).parent().parent().parent().parent() # Get form # Get target language, and also check if there is textare or just input if $(this).parent().parent().children('.translation-input').children('input').attr('name') targetLanguage = $(this).parent().parent().children('.translation-input').children('input').attr('name') else targetLanguage = $(this).parent().parent().children('.translation-input').children('textarea').attr('name') numberOfLanguages = form.children('.form-group').length # Get number of all languages # Will be used if there is no translation for the key in other languages but there is translation for the key in this language hasTranslation = false # Get source language and translatable string (first language with translation which is not target language) for i in [0...numberOfLanguages] formGroup = form.children('.form-group').eq(i) # Determines if there ir textarea or input field if formGroup.children().eq(0).children('.translation-input').children('textarea').val() value = formGroup.children().eq(0).children('.translation-input').children('textarea').val() language = formGroup.children().eq(0).children('.translation-input').children('textarea').attr('name') else value = formGroup.children().eq(0).children('.translation-input').children('input').val() language = formGroup.children().eq(0).children('.translation-input').children('input').attr('name') # If there is translation for key in language that is not target language then it will be translatable string if value && language != targetLanguage sourceLanguage = language translatableString = value break # In case if there is no other language with translation and this language has translation then this will be displayed translation if value && language == targetLanguage hasTranslation = true result = value # Alert user if there is nothing to get translation from alert 'There is no word to translate. Please fill at least one translation.' unless translatableString || hasTranslation # Check for internet connection, if there is no connection then alert user unless navigator.onLine alert 'Unable to connect google translate service. Please make sure that you are connected to the internet!' else if translatableString # Regular expression for searching punctuation marks regex = /[^\.!\?]+[\.!\?]+/g # If translatable string includes many sentences then every sentence should be translated independently if regex.test(translatableString) sentences = translatableString.match( regex ) # Array for translation storage translation = [] # Translate every sentence independently for sentence in sentences # Google translate API url url = "https://translate.googleapis.com/translate_a/single?client=gtx" url += "&sl=" + sourceLanguage url += "&tl=" + targetLanguage url += "&dt=t&q=" + encodeURI(sentence) # HTTP GET request for translation xmlHttp = new XMLHttpRequest() xmlHttp.open( "GET", url, false ) xmlHttp.send( null ) # Parse API response to JSON response = JSON.parse(xmlHttp.responseText) # Get suggested translation translation.push response[0][0][0] result = translation.join(' ') # If translatable string is just one sentence then it can be translated directly else # Google translate API url url = "https://translate.googleapis.com/translate_a/single?client=gtx" url += "&sl=" + sourceLanguage url += "&tl=" + targetLanguage url += "&dt=t&q=" + encodeURI(translatableString) # HTTP GET request for translation xmlHttp = new XMLHttpRequest() xmlHttp.open( "GET", url, false ) xmlHttp.send( null ) # Get translation response = JSON.parse(xmlHttp.responseText) result = response[0][0][0] # Show suggested translation to user $(this).children('.suggested-translation').children('p').text result