UNPKG

i18n-pro

Version:

An out-of-the-box, lightweight JavaScript i18n auto-translation solution

103 lines (102 loc) 3.42 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.translateByGooglex = exports.translateImpl = exports.setGooglexConfig = exports.proxyGoogleXTranslate = void 0; const GoogleXTranslate = __importStar(require("google-translate-api-x")); const utils_1 = require("./utils"); const node_fetch_1 = __importDefault(require("node-fetch")); exports.proxyGoogleXTranslate = { ...GoogleXTranslate, }; const config = { from: '', to: [], }; const ERROR_CODE_TIP_MAP = {}; const EXIT_ERROR_CODES = Object.keys(ERROR_CODE_TIP_MAP); function setGooglexConfig(configProp) { Object.entries(configProp).forEach(([key, value]) => { config[key] = value; }); } exports.setGooglexConfig = setGooglexConfig; async function translateImpl(props) { const { texts, from, to, proxy } = props; let agent = null; global.fetch = node_fetch_1.default; if (proxy) { const createHttpsProxyAgent = require('https-proxy-agent'); agent = createHttpsProxyAgent(proxy); } return exports.proxyGoogleXTranslate.translate(texts, { from, to, requestOptions: { agent, }, }); } exports.translateImpl = translateImpl; async function translateByGooglex(props) { const { texts, from, to } = props; const TRANSLATOR_NAME = (0, utils_1.getTranslatorName)('googlex'); const success = {}; const error = {}; const textErrorMsg = {}; let errorCode; try { console.log(t('翻译中...')); const res = await translateImpl({ ...props, proxy: config.proxy, }); const srcDistMap = res.reduce?.((res, item, index) => { res[texts[index]] = item.text; return res; }, {}); (0, utils_1.collectRes)({ from, to, texts, srcDistMap, success, error, textErrorMsg, translatorName: TRANSLATOR_NAME, }); } catch (e) { (0, utils_1.handleTranslateFail)(e, errorCode, EXIT_ERROR_CODES, texts, error); } return { success, error, textErrorMsg, }; } exports.translateByGooglex = translateByGooglex;