1.0.9 • Published 12 months ago

@sckt/translate v1.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

translate

an opinionated API for translator engines (fork of github:franciscop/translate)

Original description

Convert text to different languages on Node.js and the browser. Flexible package that allows you to use Google (default), Yandex, Libre or DeepL

What are the differences of this fork and upstream?

Getting started

Install:

npm install @sckt/translate
pnpm install @sckt/translate
...

Import:

import translate from '@sckt/translate'

Usage:

import translate from 'translate'

const text = await translate('Hello world', {
  engine: 'deepl',
  key: process.env.DEEPL_KEY,
  to: 'es',
})

console.log(text) // "Hola mundo"

Options

export interface Options<
  Engines extends Record<string, Engine> = Record<never, Engine>,
  EngineName extends LiteralUnion<keyof typeof defaultEngines, keyof Engines> = LiteralUnion<
    keyof typeof defaultEngines,
    keyof Engines
  >,
> {
  /** source language - default: 'en' */
  from?: LiteralUnion<LanguageCode, string>
  /** target language - default: 'en' */
  to?: LiteralUnion<LanguageCode, string>

  /** api key */
  key?: string

  /** translation engine name - default: 'google' */
  engine?: EngineName
  /** custom engines definition */
  engines?: Engines

  /** custom url for specific engines */
  url?: string

  /** override for url params for engines that supports it */
  overrideParams?: Record<string, string>

  /** cache expiration time, default: never */
  cache?: number
}

Engines

Default engines (use by setting the engine option):

  • deepl: (demo): A rapidly growing popular translation engine built with Machine Learning.
  • google: (demo | docs): Google Translate.
  • google_batchexecute: (demo): Google Translate (used for the Translate webapp, doesn't work if Referer or Origin header is set to something else other than translate.google.com?)
  • google_dict_chrome_ex: Google Translate (Google Dictionary browser extension)
  • google_cloud: (docs) Google Cloud Translation REST API.
  • libre: (demo): An independent translation engine. You can use the official website or install it on your own server.
  • lingva: (demo): Alternative front-end for Google Translate, serving as a Free and Open Source translator with over a hundred languages available.
  • simplytranslate: (demo): A privacy friendly frontend for multiple Translation Websites.
  • yandex: (demo | docs | API Key): Yandex Translate

Custom engines:

import translate, { Engine } from '@sckt/translate'

/**
 * imagine in your head that we have a translation engine hosted at `localhost`,
 *
 * endpoint is `localhost/v1/translate`
 *
 * takes a body of shape: {
 *   from: string,
 *   to: string,
 *   text: string,
 * }
 *
 * then responds with: {
 *   translation?: string;
 *   error?: string;
 * }
 *
 * with this info, we may now create our own engine implementation:
 **/
const localhost: Engine = {
  needkey: false,
  fetch: ({ from, text, to }) => [
    new URL('api/v1/translate', 'http://127.0.0.1'),
    {
      method: 'POST',
      body: JSON.stringify({
        from,
        to,
        text,
      }),
    },
  ],
  extraSourceLanguages: ['auto'],
  parse: (res) =>
    res.json().then((_) => {
      // tfw we can't cast unknown to something else in func args
      const body = _ as { translation?: string; error?: string }

      if (!body?.translation) throw new Error('no response')
      if (body.error) throw new Error(body.error)

      return body.translation
    }),
}

// we may now use the engine impl we created
await translate('test', { engines: { localhost }, engine: 'localhost', from: 'auto' })
  .then(console.log)
  .catch(console.error)

Acknowledgement

  • libmozhi for its Google Translate frontend implementation

Acknowledgement (upstream)

Current package and development: Francisco Presencia

Original package and idea: Andrew Lunny (alunny), Marak Squires, Google

Testing in Internet Explorer supported by BrowserStack:

BrowserStack logo

1.0.9

12 months ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago