Gathering detailed insights and metrics for @yxw007/translate
Gathering detailed insights and metrics for @yxw007/translate
Gathering detailed insights and metrics for @yxw007/translate
Gathering detailed insights and metrics for @yxw007/translate
🎉一个简单的翻译库,支持多翻译引擎。A simple translation library that supports multiple translation engines
npm install @yxw007/translate
Typescript
Module System
Node Version
NPM Version
TypeScript (91.98%)
Vue (3.84%)
CSS (1.83%)
HTML (1.31%)
JavaScript (1.04%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
17 Stars
124 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jun 04, 2025
Latest Version
0.2.2
Package Id
@yxw007/translate@0.2.2
Unpacked Size
1.31 MB
Size
280.25 kB
File Count
23
NPM Version
10.8.2
Node Version
20.19.2
Published on
Jun 01, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
3
29
English | 简体中文
Note: Translate helps you to solve all the above problems, and will expand more in the future!
Special reminder: although the library has supported the use of the browser environment, but please only use the google engine translation (google does not need key), the use of other translation engine need to configure the key, the use of the front-end will lead to key leakage, do not do it
Name | Support | Description |
---|---|---|
√ | Commissioned and ready for use | |
azure translate | √ | Commissioned and ready for use |
amazon translate | √ | Commissioned and ready for use |
baidu | √ | Commissioned and ready for use |
deepl | √ | Commissioned and ready for use |
openai | √ | Commissioned and ready for use |
yandex | I have not tuned in as I do not have a bank account supported by the platform (help from those who are in a position to do so is welcome and appreciated) |
npm
1npm install @yxw007/translate
yarn
1yarn add @yxw007/translate
pnpm
1pnpm i @yxw007/translate
ESM
1import { translator, engines } from "@yxw007/translate"
Commonjs
1const { translator, engines } = required("@yxw007/translate")
example
1translator.addEngine(engines.google()); 2const res1 = await translator.translate("hello", { from: "en", to: "zh" }); 3console.log(res1); 4 5const res2 = await translator.translate(["hello", "good"], { from: "en", to: "zh", engine: "google" }); 6console.log(res2);
输出结果
1['你好'] 2["你好", "好的"]
use jsDelivr CDN
development
1<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script>
production
1<script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.min.js"></script>
example
1<!DOCTYPE html> 2... 3 4<head> 5 ... 6 <script src="https://cdn.jsdelivr.net/npm/@yxw007/translate@0.0.7/dist/browser/index.umd.js"></script> 7</head> 8 9<body> 10 <script> 11 (async () => { 12 const { engines, translator } = translate; 13 translator.addEngine(engines.google()); 14 const res = await translator.translate("hello", { from: "en", to: "zh" }); 15 console.log(res); 16 })(); 17 </script> 18</body> 19 20</html> 21
1class Translator { 2 private engines: Map<string, Engine>; 3 constructor() { 4 this.engines = new Map<string, Engine>(); 5 } 6 /** 7 * This method is obsolete, please use the addEngine method 8 * @param engine {@link Engine} instance 9 * @deprecated Use {@link addEngine} instead. 10 */ 11 use(engine: Engine) { 12 this.addEngine(engine); 13 } 14 addEngine(engine: Engine) { 15 ... 16 } 17 removeEngine(engineName: string) { 18 ... 19 } 20 translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) { 21 ... 22 } 23}
use
Add a translation engine to transitorion engine to translator
1type Engine = { 2 name: string; 3 translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>) { 4};
translate
You can pass a text or pass a text array, which will return a translated Promise<string[]>
1translate<T extends Engines>(text: string | string[], options: TranslateOptions<T>)
1export interface TranslateOptions { 2 from?: FromLanguage<T>; 3 to: ToLanguage<T>; 4 engine?: Engines; 5 /** 6 * Cache time in milliseconds 7 */ 8 cache_time?: number; 9 /** 10 * Domain to use for translation 11 */ 12 domain?: string; 13}
Note: To learn more about the support of each engine language, go to the following directory to view the corresponding configurations
1interface BaseEngineOption {}
1interface AzureEngineOption extends BaseEngineOption { 2 key: string; 3 region: string; 4}
Note: Option Param, please get it from the corresponding platform
1interface AmazonEngineOption extends BaseEngineOption{ 2 region: string; 3 accessKeyId: string; 4 secretAccessKey: string; 5}
Note: Option Param, please get it from the corresponding platform
1export interface BaiduEngineOption extends BaseEngineOption { 2 appId: string; 3 secretKey: string; 4}
Note: Option Param, please get it from the corresponding platform
1export interface DeeplEngineOption { 2 key: string; 3}
Note: Option Param, please get it from the corresponding platform
1export interface OpenAIEngineOption { 2 apiKey: string; 3 model: OpenAIModel; 4} 5 6export const OPEN_AI_MODELS = [ 7 "o1-preview", 8 "o1-preview-2024-09-12", 9 "o1-mini-2024-09-12", 10 "o1-mini", 11 "dall-e-2", 12 "gpt-3.5-turbo", 13 "gpt-3.5-turbo-0125", 14 "babbage-002", 15 "davinci-002", 16 "dall-e-3", 17 "text-embedding-3-large", 18 "gpt-3.5-turbo-16k", 19 "tts-1-hd-1106", 20 "text-embedding-ada-002", 21 "text-embedding-3-small", 22 "tts-1-hd", 23 "whisper-1", 24 "gpt-3.5-turbo-1106", 25 "gpt-3.5-turbo-instruct", 26 "gpt-4o-mini-2024-07-18", 27 "gpt-4o-mini", 28 "tts-1", 29 "tts-1-1106", 30 "gpt-3.5-turbo-instruct-0914", 31] as const; 32 33export type OpenAIModel = (typeof OPEN_AI_MODELS)[number];
Description:option param Please get it from the corresponding platform.
Special attention: Please create a new branch based on the master, develop on the new branch, and create PR to Master after development.
Installation dependence
1pnpm install
Add new Engine
Add a new platform ENGINE plugin
1export interface XXEngineOption extends BaseEngineOption { 2 key: string; 3} 4 5export function xx(options: XXEngineOption): Engine { 6 const { key } = options; 7 const base = "https://translate.yandex.net/api/v1.5/tr.json/translate"; 8 return { 9 name: "yandex", 10 async translate<T extends Engines>(text: string | string[], opts: EngineTranslateOptions<T>) { 11 const { from, to } = opts; 12 if (!Array.isArray(text)) { 13 text = [text]; 14 } 15 //TODO: Call the platform translation APIplatform translation API 16 const translations: string[] = []; 17 //TODO: Analyze the corresponding results of the platform API, and resolve the results to the translations back 18 for (const translation of body.text) { 19 if (translation) { 20 translations.push(translation); 21 } 22 } 23 return translations; 24 }, 25 }; 26}
Add the plugin to Engines(Location:/src/engines/index.ts
)
1import { xx } from "./xx"; 2export const engines = { 3 google, 4 azure, 5 amazon, 6 baidu, 7 deepl, 8 openai, 9 xx 10} as const;
Add the origin language configuration supported by the engine
1//Note: If the origin and target languages are the same, you can directly use the target language to configure them, otherwise please configure them separately 2//src/language/origin/index.ts 3import azure from "../target/azure"; 4... 5import xxx from "../target/xxx" 6 7export const originLanguages = { 8 azure: azure, 9 ... 10 xxx: xxx, 11} as const; 12 13export type originLanguageMapNames = { 14 amazon: keyof typeof amazon; 15 ... 16 xxx: keyof typeof xxx; 17}; 18 19export type originLanguageMapValues = { 20 amazon: ValuesOf<typeof amazon>; 21 ... 22 xxx: ValuesOf<typeof xxx>; 23}; 24
Add the target language that is supported by the engine
1//src/language/target/index.ts 2import azure from "./azure"; 3... 4import xxx from "./amazon"; 5 6export const targetLanguages = { 7 azure: azure, 8 ... 9 xxx: xxx, 10} as const; 11 12export type targetLanguageMapNames = { 13 amazon: keyof typeof amazon; 14 ... 15 xxx: keyof typeof xxx; 16}; 17 18export type targetLanguageMapValues = { 19 amazon: ValuesOf<typeof amazon>; 20 ... 21 xxx: ValuesOf<typeof xxx>; 22}; 23
Build
1pnpm build
Test
1pnpm test
Tips: At present, the library can be used normally. Welcome everyone to experience. If you have any questions and suggestions, you can mention the feedback to me.If you are interested, you are welcome to join, let us improve this tool together. Help to click star ⭐, let more people know this tool, thank you for everyone🙏
Note:Thanks to franciscop/translate for giving me ideas for a quick implementation of this library, and also indirectly some of his code. Much appreciated.🙏
Translate is released under the MIT license. See the LICENSE
file.
No vulnerabilities found.
No security vulnerabilities found.