Code Monkey home page Code Monkey logo

lost-in-translation's Introduction

LostInTranslation.js

You know that thing where people were translating lyrics and such through multiple translations and then singing the lyrics back in English? Yeah that's what I was thinking of when I wrote this. A Node.js module that allows you to translate a string multiple times through multiple languages. I've had some fun with it, and maybe you will to.

greyscale

Installation

To install:

npm install lost-in-translation 

Usage

Setup:

You must pass two objects to the constructor. The first is your translation parameters, and the second contains authentication information for the Google Cloud Translation API.

const Translator = require('lost-in-translation');

var translator = new Translator({
    languageSteps: 3,
    targetLanguage: 'en',
    startingLanguage: 'en',
    inputString: 'This is my totally rad input string'
}, {
    projectId: 'your projectId',
    keyFilename: 'path to keyfile'
})

The first object passed to the constructor should have the following structure:

//All parameters are required.
{
    //languageSteps can be either a number or an array of languages e.g. ['az','eu','be','bn']
    languageSteps: 3,
    //the final language returned
    targetLanguage: 'en',
    //the language we are starting with
    startingLanguage: 'en',
    //the string to be translated
    inputString: 'This is my totally rad input string'
}

getTranslation:

translator.getTranslation().then((data)=>{
    console.log("The text became: " + data[data.length - 1].translation);
})

// Logged: The text became: This is the absolute line input line.

The getTranslation method returns a promise that resolves to an array of objects, each representing a translation step. For example, if you specify 3 languages to translate through then the returned object will have 5 elements. The element at data[0] is the first step, so it is the input and input language. And the last item in the array is the completed translation and output language.

The objects in the array have the followin signature:

{
    code: 'This will be the 2 letter language code',
    translation: 'This will be the inputString translated into the language represented in the code parameter'
}

setTranslationParams

This function lets you provide new translation parameters to the translator.

translator.setTranslationParams({
    languageSteps: ['az','eu','be','bn'],
    targetLanguage: 'en',
    startingLanguage: 'en',
    inputString: 'This is a new string that is totally more awesome than the last one.'
})
.then(()=>{
    return translator.getTranslation();
})
.then((data)=>{
    console.log("The text became: " + data[data.length - 1].translation);
})

//Logged: The text became: Secondly, more beautiful than circuits is not completely new.

setLanguageList

LostInTranslation.js includes an internal array of supported language codes. This is done so that you don't have to rely on language detection at the expense of perhaps not being up to date at all times. If you find yourself needing to alter the list of language codes, you may replace the existing array using setLanguageList.

var list = ['ps', 'fa', 'pl'];
translator.setLanguageList(list);

//Now the internal list will only contain the languages specified: 'ps', 'fa', 'pl'

lost-in-translation's People

Contributors

joshdenz avatar

Watchers

 avatar

lost-in-translation's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.