1.1.1 • Published 5 years ago

egg-google-translate v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

egg-google-translate

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Egg plugin for google translation.

Install

$ npm i egg-google-translate --save

Usage

// {app_root}/config/plugin.js
exports.googleTranslate = {
  enable: true,
  package: 'egg-google-translate',
};

Configuration

// {app_root}/config/config.default.js
exports.googleTranslate = {
  key: {YOUR_API_KEY}, // Google Translation API
  hacked: boolean, // 是否开启Hacked,开启则翻译接口可以免费试用,忽略Key
};

see config/config.default.js for more detail.

API

this.app.translation.translate(text, target)
this.app.translation.detect(text)
this.app.translation.getLanguages()

Example

'use strict'

const { Controller } = require('@ali/nut')

class TranslationController extends Controller {
  translate () {
    const {
      text,
      target
    } = this.ctx.request.body
    const results = await this.app.translation.translate(text, target)
    if (results) {
      this.ctx.body = results[0]
    }
  }
  detect () {
    const {
      text
    } = this.ctx.request.body
    const results = await this.app.translation.detect(text)
    if (results) {
      this.ctx.body = results[0]
    }
  }
  languages () {
    const results = await this.app.translation.getLanguages()
    if (results) {
      this.ctx.body = results[0]
    }
  }
}

module.exports = TranslationController

Questions & Suggestions

Please open an issue here.

License

MIT