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

@rosostolato/translate

v1.1.0

Published

Translate text to different languages on node.js and the browser

Downloads

3

Readme

Translate

Fork of translate package. I needed to fork because it was throwing errors for node-fetch. That means it will only work on browsers.

Convert text to different languages on Node.js and the browser. Flexible package and powerful back-end using Google (default) or Yandex:

// async/await. Options can be a language name (ISO 639)
const text = await translate('Hello world', 'es');
console.log(text);  // Hola mundo

// Promises with .then(). Options can also be an object
translate('こんにちは世界', { from: 'ja', to: 'es' }).then(text => {
  console.log(text);  // Hola mundo
});

Getting started

This package can be used in Node.js and on the browser. For the browser we are using fetch, so you might want to polyfill it depending on the browsers you support.

To use it in node.js first install it:

npm install translate

Then import it to use it:

const translate = require('translate'); // Old school
import translate from 'translate';      // New wave

To use it in the browser download the main translate.min.js file and include it:

<script src="translate.min.js"></script>

Or use the awesome Jsdelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/translate@1/translate.min.js"></script>

After including translate the usage is similar for both Node.js and the browser.

Options

The first parameter is the string that you want to translate. Right now only a single string of text is accepted.

The second parameter is the options. It accepts either a String of the language to translate to or a simple Object with these options:

  • to: the string of the language to translate to. It can be in any of the two ISO 639 (1 or 2) or the full name in English like Spanish. Defaults to en.
  • from: the string of the language to translate to. It can be in any of the two ISO 639 (1 or 2) or the full name in English like Spanish. Also defaults to en.
  • cache: a Number with the milliseconds that each translation should be cached. Leave it undefined to cache it indefinitely (until a server/browser restart).
  • engine: a String containing the name of the engine to use for translation. Right now it defaults to google. Read more in the engine section.
  • key: the API Key for the engine of your choice. Read more in the engine section.

Examples:

// Translate from English (default) to Spanish (specified)
const foo = await translate('Hello world', 'es');

// Same as this:
const bar = await translate('Hello world', { to: 'es' });

On both to and from defaulting to en: while I am Spanish and was quite tempted to set this as one of those, English is the main language of the Internet and the main secondary language for those who have a different native language. This is why most of the translations will happen either to or from English.

Default options

You can change the default options for anything by calling the root library and the option name:

translate.from = 'es';

This can be applied to any of the options enumerated above.

Engines

Several translating engines are available to translate your text:

To get the API Key you will be signing some contract with these services; it's your responsibility to follow these and we are not liable if you don't as explained in our MIT License.

Once you get the API key and if you are only going to be using one engine (very likely), we recommend setting this globally for your whole project:

// ... include translate

translate.engine = 'google';
translate.key = 'YOUR-KEY-HERE';

// ... use translate()

If you are in Node.js, this likely comes from an environment variable:

// ... include translate

translate.engine = 'google';
translate.key = process.env.TRANSLATE_KEY;

// ... use translate()

To pass it per-translation, you can add it to your arguments:

translate('Hello world', { to: 'en', engine: 'google', key: 'YOUR-KEY-HERE' });

Promises

Working with Promises and specially with async/await reduces Callback Hell. To see it in action, first you'll need an async function. Then put your await calls inside:

// Browser; jQuery for demonstration purposes
$('#translate').submit(async e => {
  e.preventDefault();
  const text = $('.text').text();
  const spanish = await translate(text, { to: 'es' });
  alert(spanish);
});

// Node.js; serverjs.io example for demonstration purposes
const route = async ctx => {
  const spanish = await translate(ctx.body, { to: 'es' });
  return send(spanish);
};

Authors and thanks

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