npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flawcra/translate-tools-core

v0.2.8

Published

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

Downloads

6

Readme

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 name | Platforms | API key | Status | | -------------------- | --------------- | ------------ | ------------------------------------------------------------------ | | GoogleTranslator | browser, nodejs | not required | ready to use | | YandexTranslator | browser | not required | ready to use | | BingTranslatorPublic | browser | not required | unstable, ready to use for translate short text with low frequency | | ReversoTranslator | browser | not required | unstable |

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