From 8bcce3023a5e4ae22413398d81cd685f53a33a5b Mon Sep 17 00:00:00 2001 From: Alpha5959 <109584578+Alpha5959@users.noreply.github.com> Date: Sun, 3 Dec 2023 00:24:45 +0600 Subject: [PATCH] Optimize translation module, integrate franc for language detection. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code optimization encompassed modularization, having unique functions for translation and API queries, frequent usage of ‘async/await’ for improved readability, and thorough exception handling. By switching from ’langdetect’ to franc, language detection became smoother and more accurate. These adjustments are made to make this translation module more practical and sustainable. --- src/index.js | 65 +++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/src/index.js b/src/index.js index d89e6c2..10a4ecf 100644 --- a/src/index.js +++ b/src/index.js @@ -1,65 +1,62 @@ -const axios = require('axios'); -const translate = require('translate-google'); -const langdetect = require('langdetect'); +const axios = require("axios"); +const translate = require("translate-google"); +const franc = require("franc"); -let config = {}; +let args = {}; -function setConfig(newConfig) { - config = { ...config, ...newConfig }; +function setConfig(config) { + args = config; } -function getVersion() { - return require('./package.json').version; +function version() { + return require("./package.json").version; } -async function translateText(text, fromLang, toLang) { +async function translateText(text, toLanguage) { try { - const translatedText = await translate(text, { from: fromLang, to: toLang }); - return translatedText; + return await translate(text, { to: toLanguage }); } catch (error) { - throw new Error(`Translation error: ${error.message}`); + throw new Error(`[TRANSLATE] Translation failed: ${error.message}`); } } -async function requestApi(prompt) { +async function makeApiRequest(message) { try { - const translatedPrompt = await translateText(prompt, 'auto', 'en'); - const options = { - method: 'GET', - url: 'https://google-bard1.p.rapidapi.com/', + method: "GET", + url: "https://google-bard1.p.rapidapi.com/", headers: { - userid: config.userid, - message: translatedPrompt, - key: config.key, - 'X-RapidAPI-Key': config.apikey, - 'X-RapidAPI-Host': 'google-bard1.p.rapidapi.com', + userid: args.userid, + message, + key: args.key, + "X-RapidAPI-Key": args.apikey, + "X-RapidAPI-Host": "google-bard1.p.rapidapi.com", }, }; const response = await axios.request(options); - const detectedLang = langdetect.detectOne(prompt); - const translatedResponse = await translateText(response.data.response, 'en', detectedLang); - - return translatedResponse; + return response.data.response; } catch (error) { - throw new Error(`API request error: ${error.message}`); + throw new Error(`[API] Request failed: ${error.message}`); } } async function createText(prompt) { try { - const response = await requestApi(prompt); - return response; - } catch (error) { - throw new Error(`Text creation error: ${error.message}`); + const detectedLangCode = franc(prompt); + const detectedLang = translate.languages[detectedLangCode]; + const translatedPrompt = await translateText(prompt, "en"); + const apiResponse = await makeApiRequest(translatedPrompt); + const translatedResponse = await translateText(apiResponse, detectedLang); + + return translatedResponse; + } catch (err) { + throw new Error(err); } } module.exports = { setConfig, - getVersion, + version, createText, - translateText, // Expose translateText for external use. (Optional) - requestApi, // Expose requestApi for external use. (Optional) };