JAVASCRIPT   49
Translator
Guest on 14th September 2023 12:14:06 AM


  1.  
  2. // Muaz Khan      - www.MuazKhan.com
  3. // MIT License    - www.WebRTC-Experiment.com/licence
  4.  
  5. // Source Code    - github.com/muaz-khan/Translator
  6. // Demo           - www.webrtc-experiment.com/Translator
  7.  
  8. function Translator() {
  9.     this.voiceToText = function(callback, language) {
  10.         initTranscript(callback, language);
  11.     };
  12.  
  13.     this.translateLanguage = function(text, config) {
  14.         config = config || { };
  15.         var api_key = config.api_key;
  16.  
  17.         var newScript = document.createElement('script');
  18.         newScript.type = 'text/javascript';
  19.  
  20.         var sourceText = encodeURIComponent(text); // escape
  21.  
  22.         var randomNumber = 'method' + (Math.random() * new Date().getTime()).toString(36).replace( /\./g , '');
  23.         window[randomNumber] = function(response) {
  24.             if (response.data && response.data.translations[0] && config.callback) {
  25.                 config.callback(response.data.translations[0].translatedText);
  26.                 return;
  27.             }
  28.  
  29.             if(response.error && response.error.message == 'Daily Limit Exceeded') {
  30.                 config.callback('Google says, "Daily Limit Exceeded". Please try this experiment a few hours later.');
  31.                 return;
  32.             }
  33.  
  34.             if (response.error) {
  35.                 console.error(response.error.message);
  36.                 return;
  37.             }
  38.  
  39.             console.error(response);
  40.         };
  41.  
  42.         var source = 'https://www.googleapis.com/language/translate/v2?key=' + api_key + '&target=' + (config.to || 'en-US') + '&callback=window.' + randomNumber + '&q=' + sourceText;
  43.         newScript.src = source;
  44.         document.getElementsByTagName('head')[0].appendChild(newScript);
  45.     };
  46.    
  47.     var recognition;
  48.  
  49.     function initTranscript(callback, language) {
  50.         if (recognition) recognition.stop();
  51.  
  52.         window.SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
  53.  
  54.         recognition = new SpeechRecognition();
  55.  
  56.         recognition.lang = language || 'en-US';
  57.  
  58.         console.log('SpeechRecognition Language', recognition.lang);
  59.  
  60.         recognition.continuous = true;
  61.         recognition.interimResults = true;
  62.  
  63.         recognition.onresult = function(event) {
  64.             for (var i = event.resultIndex; i < event.results.length; ++i) {
  65.                 if (event.results[i].isFinal) {
  66.                     callback(event.results[i][0].transcript);
  67.                 }
  68.             }
  69.         };
  70.  
  71.         recognition.onend = function() {
  72.             if(recognition.dontReTry === true) {
  73.                 return;
  74.             }
  75.  
  76.             initTranscript(callback, language);
  77.         };
  78.  
  79.         recognition.onerror = function(e) {
  80.             if(e.error === 'audio-capture') {
  81.                 recognition.dontReTry = true;
  82.                 alert('Failed capturing audio i.e. microphone. Please check console-logs for hints to fix this issue.');
  83.                 console.error('No microphone was found. Ensure that a microphone is installed and that microphone settings are configured correctly. https://support.google.com/chrome/bin/answer.py?hl=en&answer=1407892');
  84.                 console.error('Original', e.type, e.message.length || e);
  85.                 return;
  86.             }
  87.  
  88.             console.error(e.type, e.error, e.message);
  89.         };
  90.  
  91.         recognition.start();
  92.     }
  93. }

Raw Paste

Login or Register to edit or fork this paste. It's free.