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

deepl-translator

v1.2.1

Published

This module provides promised methods for translating text using DeepL Translator (https://www.deepl.com/translator) undocumented API.

Downloads

143

Readme

deepl-translator

Coverage Status Build Status Known Vulnerabilities npm version styled with prettier

This unofficial node module provides promised methods for detecting language and translating text using DeepL Translator (https://www.deepl.com/translator) undocumented API.

DeepL has done a great job with their deep learning translation model which outperforms the competition by a wide margin. An excerpt from their page on that topic:

Blind test

100 sentences were translated by DeepL Translator, Google Translate, Microsoft Translator, and Facebook. Professional translators assessed the translations, without knowing which system produced which results. The translators preferred our translations over the competition's by a factor of 3:1. Here are the results of a test run in August 2017:

Stats

Supported languages:

| Language code | Language |:--------------------|:--------------------------------------------------------------- | EN | English | DE | German | FR | French | ES | Spanish | IT | Italian | NL | Dutch | PL | Polish

Install

yarn add deepl-translator

This package can also be used on the client since it provides an XHR shim for the HTTP request helper implementation. The shim is defined as a mapping in the browser property of the package.json so it should be picked up automatically by most of the popular bundlers.

Usage

const { translate, detectLanguage, wordAlternatives, translateWithAlternatives } = require('deepl-translator');

// Translate text with explicit source and target languages
translate('Die Übersetzungsqualität von deepl ist erstaunlich!', 'EN', 'DE')
  .then(res => console.log(`Translation: ${res.translation}`))
  .catch(console.error);

// Translate short text with this method to get a few translation alternatives
translateWithAlternatives(
  'Die Übersetzungsqualität von deepl ist erstaunlich!',
  'EN'
)
  .then(res =>
    console.log(
      `Translation alternatives: ${res.translationAlternatives.join(', ')}`
    )
  )
  .catch(console.error);

// Leave out the source language or specify 'auto' to autodetect the input
translate('This is a representative chunk of text in english.', 'DE')
  .then(res => console.log(`Translation: ${res.translation}`))
  .catch(console.error);

// Detect the text language without giving back the translation
detectLanguage('Deepl también puede detectar un idioma. ¿Qué idioma es este?')
  .then(res => console.log(`Detected language: ${res.languageName}`))
  .catch(console.error);

// Multi-line translations work as well, even with different source languages mixed in
translate(
  `Das ist der erste Satz... Das der zweite.

  C'est la troisième phrase.


  Y ese es el cuarto.
  I piąta.`,
  'EN'
)
  .then(res => console.log(`Translation: ${res.translation}, Resolved languages: ${res.resolvedSourceLanguage}`))
  .catch(console.error);

  // This method allows for tweaking the translation. By providing a beginning, we define a word or a phrase
  // for which we want alternative translations within the context of a larger body of text (a sentence).
  // One of the returned alternatives can then be selected and passed into translateWithAlternatives
  // as an overriding word/phrase for the beginning of the whole translation.
  {
    const text = 'Die Übersetzungsqualität von deepl ist erstaunlich!';
    // Translates to: 'The translation quality of deepl is amazing!'
    wordAlternatives(text, 'EN', 'DE', 'The translation')
      .then(res => {
        console.log(`3 Alternative beginnings:`);
        res.alternatives.slice(0, 3).forEach(alt => console.log(alt));
        // Choose the third alternetive
        return res.alternatives[2];
      })
      .then(beginning => {
        // Request translation with selected alternative beginning
        return translateWithAlternatives(text, 'EN', 'DE', beginning);
      })
      .then(res => console.log(`Alternative: ${res.translation}`));
  }

API

translate(text, targetLanguage, sourceLanguage) -> object

This method translates the input text into a specified target language. Source language can be autodetected. Multi-line text translation is possible with line breaks preserved.

text (string) Input text to be translated

targetLanguage (string) Language code of the language to translate to

sourceLanguage (string) Language code of the input text language. Can be left out or set to auto for automatic language detection.

Returns

{
  targetLanguage: 'XY', // Language code(s) of the language that was translate to
  resolvedSourceLanguage: 'YZ', // Language code(s) of the input language (resolved automatically for autodetect)
  translation: 'Translated text' // Translated text
}

translateWithAlternatives(text, targetLanguage, sourceLanguage, beginning) -> object

This method translates the input text into a specified target language. Source language can be autodetected. Optionally, a sentence beginning override can be given (should be used in conjunction with wordAlternatives which gives contextual phrase/word translations).

text (string) Input text to be translated

targetLanguage (string) Language code of the language to translate to

sourceLanguage (string) Language code of the input text language. Can be left out or set to auto for automatic language detection.

beginning (string) Override of the translation beginning

Returns

{
  targetLanguage: 'XY', // Language code of the language that was translate to
  resolvedSourceLanguage: 'YZ', // Language code of the input language (resolved automatically for autodetect)
  translation: 'Translated text', // Translated text
  translationAlternatives: ['First translated alternative', 'Second translated alternative']
}

detectLanguage(text) -> object

This method detects the language of the input text.

text (string) Input text to detect the language on

Returns

{
  languageCode: 'XY', // Language code of the input text
  languageName: 'Some language', // Language name (in English) of the input text
}

wordAlternatives(text, targetLanguage, sourceLanguage, beginning) -> object

This method suggests alternative wordings for a subset of the beginning input text. This means we get alternative translations for the given word or a phrase within a context of the larger body of text (the input text). Languages cannot be autodetected.

text (string) Input text to be translated

targetLanguage (string) Language code of the language to translate to

sourceLanguage (string) Language code of the input text language

beginning (string) Subset of the translation of text for which the contextual alternatives will be provided

Returns

{
  targetLanguage: 'XY', // Language code of the language that was translate to
  resolvedSourceLanguage: 'YZ', // Language code of the input language
  alternatives: ['An alternative', 'Yet another alternative'], // Array of alternative sentence beginnings
}

License

Apache License 2.0