Advertisement
cymplecy

Trans Orig

Sep 6th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     key: \"getTranslate\",
  2.    value: function getTranslate(args) {
  3.      // Don't remake the request if we already have the value.
  4.      if (this._lastTextTranslated === args.WORDS && this._lastLangTranslated === args.LANGUAGE) {
  5.        return this._translateResult;
  6.      }
  7.  
  8.      var lang = this.getLanguageCodeFromArg(args.LANGUAGE);
  9.      var urlBase = \"\".concat(serverURL, \"translate?language=\");
  10.      urlBase += lang;
  11.      urlBase += '&text=';
  12.      urlBase += encodeURIComponent(args.WORDS);
  13.      var tempThis = this;
  14.      var translatePromise = new Promise(function (resolve) {
  15.        nets({
  16.          url: urlBase,
  17.          timeout: serverTimeoutMs
  18.        }, function (err, res, body) {
  19.          if (err) {
  20.            log.warn(\"error fetching translate result! \".concat(res));
  21.            resolve('');
  22.            return '';
  23.          }
  24.  
  25.          var translated = JSON.parse(body).result;
  26.          tempThis._translateResult = translated; // Cache what we just translated so we don't keep making the
  27.          // same call over and over.
  28.  
  29.          tempThis._lastTextTranslated = args.WORDS;
  30.          tempThis._lastLangTranslated = args.LANGUAGE;
  31.          resolve(translated);
  32.          return translated;
  33.        });
  34.      });
  35.      translatePromise.then(function (translatedText) {
  36.        return translatedText;
  37.      });
  38.      return translatePromise;
  39.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement