Socket
Socket
Sign inDemoInstall

@flawcra/translate-tools-core

Package Overview
Dependencies
19
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @flawcra/translate-tools-core

A free and unlimited API for Google Translate, Yandex, Bing


Version published
Weekly downloads
0
Maintainers
1
Created
Weekly downloads
 

Readme

Source

The translate tools core kit contains a interfaces and default implementations of basic translation entities.

This package is part of translate tools project.

Main purpose of core package it's standardization a translator entities.

Translators info

This package contains few translators and its have different status and support different platforms.

Translator namePlatformsAPI keyStatus
GoogleTranslatorbrowser, nodejsnot requiredready to use
YandexTranslatorbrowsernot requiredready to use
BingTranslatorPublicbrowsernot requiredunstable, ready to use for translate short text with low frequency
ReversoTranslatorbrowsernot requiredunstable

Usage

Install package npm install @translate-tools/core

WARNING: this library is still under construction, so types is not obey semver and may be change unpredictable. To avoid problems with builds, fix a version of package on minor part.

example: ~0.2.0

Code example

import { GoogleTranslator } from '@translate-tools/core/translators/GoogleTranslator';

const translator = new GoogleTranslator();

// Translate single string
translator
	.translate('Hello world', 'en', 'de')
	.then((translate) => console.log('Translate result', translate));

NOTE: For use with nodejs you should specify user agent

In most cases for nodejs, translator will work incorrectly without User-Agent

import { GoogleTranslator } from '@translate-tools/core/translators/GoogleTranslator';

const translator = new GoogleTranslator({
	headers: {
		'User-Agent':
			'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
	},
});

translator
	.translate('Hello world', 'en', 'de')
	.then((translate) => console.log('Translate result', translate));

For use with browser you should specify CORS proxy in most cases when translator is not work

import { GoogleTranslator } from '@translate-tools/core/translators/GoogleTranslator';

// Use some CORS proxy service address as prefix
const translator = new GoogleTranslator({
	corsProxy: 'https://crossorigin.me/',
});

translator
	.translate('Hello world', 'en', 'de')
	.then((translate) => console.log('Translate result', translate));

// Or use your own transform function
const translator = new GoogleTranslator({
	corsProxy(url) {
		return `https://my-cors-proxy/${url}/some-postfix`;
	},
});

translator
	.translate('Hello world', 'en', 'de')
	.then((translate) => console.log('Translate result', translate));

Package contents

Translator

Translator it's basic entity for translate text.

Abstraction

Namespace types/Translator

  • Interface ITranslator with basic structure of translator object
  • Abstract class Translator which implement ITranslator and define static members

Implementation

Namespace translators

Contains a translators which extends Translator and use API of popular translate services.

  • GoogleTranslator
  • YandexTranslator
  • BingTranslator

Also contains FakeTranslator for mock and tests

Examples

import { GoogleTranslator } from '@translate-tools/core/translators/GoogleTranslator';

const translator = new GoogleTranslator();

// Translate single string
translator
	.translate('Hello world', 'en', 'de')
	.then((translate) => console.log('Single translate', translate));

// Translate multiple string
translator
	.translateBatch(['Translator can translate few strings', 'at one time'], 'en', 'de')
	.then((translate) => console.log('Batch translate', translate));

Translate scheduler

Translate scheduler it's task manager which try fit many translate requests to one request to Translator.

It's very useful for cases when you have many requests to translate short text but your Translator have limits for API requests.

Abstraction

Namespace util/Scheduler

Interface ITranslateScheduler

Implementation

  • Scheduler
  • SchedulerWithCache

Examples

import { Scheduler } from '@translate-tools/core/util/Scheduler';
import { GoogleTranslator } from '@translate-tools/core/translators/GoogleTranslator';

const translator = new GoogleTranslator();
const scheduler = new Scheduler(translator);

// Scheduler will join this requests and execute it as one request
// Scheduler may implement it any way, it may group requests by languages or other way,
// it may call `translate` method or `translateBatch`, etc

scheduler
	.translate('My first translation request', 'en', 'de')
	.then((translate) => console.log('Request #1', translate));

scheduler
	.translate('My second translation request', 'en', 'de')
	.then((translate) => console.log('Request #2', translate));

API

You can specify options in constructor for each Translator class.

Not all modules is use all keys.

apiKey

type: string

Access key for requests to API

useMultiplexing

type: boolean

Union text array to 1 request (or more, but less than usually anyway).

Option for reduce the number of requests, but it may generate artifacts in translated text.

headers

type: Record<string, string>

Additional headers for requests

corsProxy

type: string | ((url: string) => string)

Proxy prefix or transform function which return url with CORS proxy

CORS proxy useful to avoid CORS error in browser or to mask server requests as browser requests.

All requests will send through this proxy server and this server will modify headers

Keywords

FAQs

Last updated on 10 May 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc