@fwqaaq/translator
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Google Translation API for Deno🛠️

Features

  • Automatic language detection with default options.
  • Option to specify source and target languages.
  • Choice of translation models and clients for different response data.
  • Customizable Google Translate URL.
  • Error handling for common translation issues.
  • Modern ESM module instead of CommonJS.

Usage

[!NOTE] Only ESM module is supported.

Import

Deno:

import { Translator } from "https://deno.land/x/translator/mod.ts";

ESM:

import { Translator } from 'https://esm.sh/@fwqaaq/translator'

or:

pnpm add @fwqaaq/translator

Example

const options = {
  source: "zh-CN",
  model: 't',
  client: 'gtx',
  url: 'https://translate.googleapis.com/translate_a/'
};

async function main() {
  const translator = new Translator(options);
  const response = await translator.translate("你好");
  console.log(response) // { lang: 'zh-CN', text: 'Hello' }
}

main()

API Reference

  • Constructor
    • options: TranslateOptions - (Optional) The options to use when translating.
  • Methods
    • translate(_text: string, isRaw: boolean): Promise<ResponseData | Array<any>> - Translates the text. If isRaw is true, returns raw response data. Default is false.
    • audio(_text: string, isOriginal: boolean): string - Returns google audio link. If isOriginal is true, using the original text to generate the audio, and set source to your target language. Default is false, read the translated text.
  • TranslateOptions Interface
    • source: string - Source language (default: 'auto').
    • target: string - Target language (default: 'en').
    • model: 't' | 'single' - Translation model (default: 't').
    • client: 'gtx' | 'dict-chrome-ex' - Client for translation (default: 'gtx').
    • url: string - Google Translate URL (default: 'https://clients5.google.com/translate_a/').
  • ResponseData Interface
    • lang: string - The language of the source text.
    • text: string - The translated text.

Links

Package Sidebar

Install

npm i @fwqaaq/translator

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

11 kB

Total Files

5

Last publish

Collaborators

  • fwqaaq