An open API service indexing awesome lists of open source software.

https://github.com/yxw007/translate

🎉一个简单的翻译库,支持多翻译引擎。A simple translation library that supports multiple translation engines
https://github.com/yxw007/translate

amazon azure baidu browser cjs deepl ems google i18n nodejs translate translator typescript

Last synced: about 2 months ago
JSON representation

🎉一个简单的翻译库,支持多翻译引擎。A simple translation library that supports multiple translation engines

Awesome Lists containing this project

README

          

# Translate

English | [简体中文](./README_zh-CN.md)

![GitHub top language](https://img.shields.io/github/languages/top/yxw007/translate)
![GitHub License](https://img.shields.io/github/license/yxw007/translate)
![NPM Version](https://img.shields.io/npm/v/%40yxw007%2Ftranslate)
![Codecov](https://codecov.io/gh/yxw007/translate/branch/master/graph/badge.svg)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/yxw007/translate/ci.yml)

## ❓ Why do I need translate?

1. a lot of translation tool libraries on the market, basically not very well-maintained
2. not written by ts, not friendly enough when using the prompts
3. single function, does not support batch translation Or only support a translation engine
4. ...

> Note: Translate helps you to solve all the above problems, and will expand more in the future!

## ✨ Features

- 🌐 **Multi-environment support**: Node environment, browser environment
- ✨ **Easy to use**: provides a concise API, you can easily help you to translate
- 🌍 **Multi-translation engine support**: Google, Azure Translate, Amazon Translate, Deepl, Baidu, OpenAI, etc. (will expand more in the future)
- 🛠️ **typescript**: friendlier code hints and quality assurance
- 📦 **Batch translation**: one api request, translate more content, reduce http requests to improve translation efficiency
- 🔓 **completely open source**.

> **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**

> Browser bundles exclude Node-only helpers such as filesystem utilities. Keep browser usage focused on engines that are safe to expose client-side.

## 💻Translation engines, integration cases

| Name | Support | Description |
| ---------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| google | ✔ | 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 |
| tencent | ✔ | 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) |

## 🛠 Development

This project now uses `rolldown`, `oxlint`, and `oxfmt` instead of `rollup`, `eslint`, and `prettier`.

```bash
pnpm build
pnpm lint
pnpm lint:fix
pnpm format
pnpm format:check
pnpm test
```

- `pnpm build`: builds Node and browser bundles with `rolldown`, then emits `.d.ts` files with TypeScript.
- `pnpm lint`: runs `oxlint` and `oxfmt --check`.
- `pnpm lint:fix`: applies `oxlint --fix` and formats files with `oxfmt`.
- `pnpm format`: formats the repository with `oxfmt`.
- `pnpm test`: runs stable offline-safe tests.

Integration tests that require real network access and third-party credentials are disabled by default. To run them manually, set `RUN_INTEGRATION_TESTS=true` before running `pnpm test`.

- Bash

```bash
RUN_INTEGRATION_TESTS=true pnpm test
```

- PowerShell

```powershell
$env:RUN_INTEGRATION_TESTS = "true"
pnpm test
```

## 🚀 Install

- npm

```bash
npm install @yxw007/translate
```

- yarn

```bash
yarn add @yxw007/translate
```

- pnpm

```bash
pnpm i @yxw007/translate
```

## 📖 Usage

### Node

- ESM

```typescript
import { translator, engines } from "@yxw007/translate";
```

- Commonjs

```typescript
const { translator, engines } = required("@yxw007/translate");
```

- Translation examples

```typescript
translator.addEngine(engines.google);
const res1 = await translator.translate("hello", { from: "en", to: "zh" });
console.log(res1);

const res2 = await translator.translate(["hello", "good"], { from: "en", to: "zh", engine: "google" });
console.log(res2);
```

Output results

```bash
['你好']
["你好", "好的"]
```

- Language detection examples

```typescript
translator.addEngine(engines.google);
const res1 = await translator.checkLanguage("hello", { engine: "google" });
console.log(res1);
```

Output results

```bash
en
```

### Browser

use jsDelivr CDN

- `development`

```html

```

- `production`

```html

```

- example

```html

...


...



(async () => {
const { engines, translator } = translate;
translator.addEngine(engines.google);
const res = await translator.translate("hello", { from: "en", to: "zh" });
console.log(res);
})();