egg-google-translate

egg plugin for google translation

Usage no npm install needed!

<script type="module">
  import eggGoogleTranslate from 'https://cdn.skypack.dev/egg-google-translate';
</script>

README

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