@lzy1960/google-translate

Translate api for Google Translate


Keywords
google, translate, translator, translation, language, api, rollup, tdd, typescript, vitest
License
MIT
Install
npm install @lzy1960/google-translate@0.2.7

Documentation

google-translate

一款谷歌翻译api的插件

NPM version NPM downloads codecov Maintainability Language grade: JavaScript

OSCS Status

感谢开源社区 OSCS 的关注!

项目说明

  • TDD 开发流程
  • 使用 ts 编写
  • 集成 vitest 测试

使用方式

  1. 安装依赖包

    npm i @lzy1960/google-translate
    // OR
    yarn add @lzy1960/google-translate
    // OR
    pnpm i @lzy1960/google-translate
  2. 调用

    // default options
    const options = {
     from: 'auto',
     to: 'en',
     tld: 'cn',
     type: 'default'
    }
    const res = await translate('你好', options)
    console.log(res) // { "from": "zh-CN", "pronunciation": "Nǐ hǎo", "text": "Hello" }
  3. 入参说明

    translate接收两个参数:text 和 options

    options 默认为:

    {
     from: 'auto', // 源语言
     to: 'en', // 目标语言
     tld: 'cn', // 服务地址
     type: 'default', // 类型 'default' / 'word'
     isMobile: false // TODO:是否是移动端(移动端和pc端的返回值不一样)
    }
  4. 返回结果说明

    // default
    {
     from: "zh-CN", // 源语言
     pronunciation: "Nǐ hǎo", // 读音
     text: "Hello" // 目标语言结果
    }
    // word
    {
    text: '你好!',
       common: [
         {
           type: '感叹词',
           words: [
             {
               word: 'Hello!',
               explains: ['你好!', '喂!'],
               frequency: 1,
             },
             {
               word: 'Hi!',
               explains: ['嗨!', '你好!'],
               frequency: 1,
             },
             {
               word: 'Hallo!',
               explains: ['你好!'],
               frequency: 3,
             },
           ],
         },
       ],
    }

options 参数说明

参数 说明 类型 默认值
from 源语言 string 'auto'
to 目标语言 string 'en'
tld 地区 string 'cn'
type 类型 default | word 'default'