1.0.5 • Published 3 years ago

translator-promise v1.0.5

Weekly downloads
1
License
ISC
Repository
github
Last release
3 years ago

Translate Node Module

A node module to translate. There is power by Google Translate.

Installation

npm install translator-promise

Usage

const translate = require('translator-promise');
// async/await. The second parameter can be a language name (ISO 639-1)
const result = await translate('Hello world', 'ja');
console.log(result); 
// Output:
// {
//     word: 'Hello world', 
//     text: 'こんにちは世界', 
//     candidate: [ 'こんにちは世界', 'こんにちは' ] 
// }
 
// Promises with .then(). The third parameter is the source language.
translate('こんにちは世界', 'cn', 'ja').then(result => {
  console.log(result);  
  // Output:
  // { 
  //     word: 'こんにちは世界', 
  //     text: '你好,世界!', 
  //     candidate: [ '你好', '您好', '打招呼', '个招呼', '喂' ] 
  // }
});

Parameters

function translate(word: string, to: string, from: string): object
parameterdescription
wordThe word want to translate.
toThe target language. Default is Chinese(cn). (Optional)
fromThe source language. Default is recognized automatically(auto). (Optional)

Note: If there is no source language, the language will be recognized automatically. When there is about 33.3% Chinese, the source language will be changed to Chinese.

Return Object

keydescription
wordThe word want to translate.
textThe most match result.
candidateOther translate result.

Language Code

You can check it in here (ISO 639-1).

Example

translate('word', 'ja');

// {
//     "word": "word",
//     "text": "語",
//     "candidate": [
//         "ワード",
//         "単語",
//         "語",
//         "言葉",
//         "語句",
//         "伝言",
//         "一言半句",
//         "口舌",
//         "一言"
//     ]
// }

translate('中文', 'ko', 'zh');

// {
//     "word": "中文",
//     "text": "중국어",
//     "candidate": [
//         "중국어"
//     ]
// }

translate('中文');

// {
//     "word": "中文",
//     "text": "Chinese",
//     "candidate": [
//         "Chinese"
//     ]
// }

translate('用 Google 翻译一下这条句子。');

// { 
//     word: '用 Google 翻译一下这条句子。',
//     text: 'Translate this sentence with Google.',
//     candidate:
//     [ 
//         'Translate this sentence with Google.',
//         'By Google translate this sentence.' 
//     ] 
// }