capacitor-bing-translator
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

capacitor-bing-translator

NPM version Auto Test NPM Downloads License

A simple and free API for Bing Translator for Capacitor.js.

Install

npm install capacitor-bing-translator

Platforms

All Capacitor native platforms are supported (Android & iOS).

Web platform is not supported, because of CORS; Unless you disable CORS in your browser (for example with this extention or using CLI flags)

Usage

From auto-detected language to English:

import { translate } from 'bing-translate-api'

translate('你好', null, 'en').then(res => {
  console.log(res.translation)
}).catch(err => {
  console.error(err)
})

Translation result

{
  // original text
  "text": "你好",
  // user-specified language code
  "userLang": "auto-detect",
  // translated text
  "translation": "Hello",
  // `correctedText` is returned only when `correct` is set as `true`
  // supported since v1.1.0
  "correctedText": "",
  // detected language
  "language": {
    // language code of translated text
    "to": "en",
    // detected language code of original text
    "from": "zh-Hans",
    // score of language detection
    // supported since v1.1.0
    "score": 1
  }
}

API

translate(text, [from], [to], [correct], [raw], [userAgent])

text

Type: string

The text to be translated, can't be blank. The maximum text length is 1000.

from

Type: string Default: auto-detect

The language code of source text.

MUST be auto-detect or one of the codes/names (not case sensitive) contained in lang.json

to

Type: string Default: en

The language in which the text should be translated.

MUST be one of the codes/names (not case sensitive) contained in lang.json.

correct

Type: boolean Default: false Since: v1.1.0

Whether to correct the input text.

Note that:

  1. There is currently a limit of 50 characters for correction service.
  2. Only the languages in the list are supported to be corrected.
raw

Type: boolean Default: false

Whether the translation result contains raw response from Bing API.

userAgent

Type: string

The header value of user-agent used in API requests.

Default:

navigator.userAgent

License

MIT © 2021-2023 plainheart.

Thanks

Great thanks to Bing Translator for providing so excellent translation service.

Bing translate api for Node.js

Bing Translate API - A free Bing translation api for using in Node.js. Capacitor Bing Translator is a fork of that.

Google translate api for Capacitor

If you want to use google translate API instead of Bing, use the google-translate-api-x package. and use a custom requestFunction like this:

const result = await translate('سلام دنیا', {
  to: 'en',
  autoCorrect: true,
  forceBatch: false,
  fallbackBatch: false,
  async requestFunction(url: string, options: RequestInit) {
    const result = await CapacitorHttp.request({
      url,
      headers: {
        ...options.headers,
        'user-agent': navigator.userAgent,
      },
      method: options.method,
      data: options.body || null,
      webFetchExtra: {
        credentials: options.credentials,
      },
    })

    return new Response(
      JSON.stringify(result.data),
      {
        headers: result.headers,
        status: result.status,
      },
    )
  },
})

Package Sidebar

Install

npm i capacitor-bing-translator

Weekly Downloads

11

Version

0.1.1

License

MIT

Unpacked Size

26.1 kB

Total Files

9

Last publish

Collaborators

  • mojtaba74