1.1.0 • Published 3 years ago

google-translate-genurl v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Install

npm install google-translate-genurl

or

yarn add google-translate-genurl
生成谷歌翻译最终的调用链接, 具体的请求、跨域问题,调用方处理~

注意案例中的请求所需要处理的跨域问题需要您自己解决

import {simple, batch} from "google-translate-genurl"

// 单段文本翻译
const q = "Five Star 2-Pocket Folders, 4 Pack, Plastic Folders with Stay-Put Tabs and Prong Fasteners, Includes Writable Label, Holds 11\" x 8-1/2\", Assorted Colors (38048)"
const url = await simple.translate(q, {to: "zh-CN"});
// 伪调用
axios.get(url).then(res => {
    console.log(simple.fmt(res.data))
})

// 多文本翻译
const batchText = [
    "Casual Dresses",
    "High Quality Midi Dress",
]
const {url, data} = await batch.translate(batchText, {to: "zh-CN"});
// 注意:这里的 data 是返回的格式化完成的params字符串,在您的业务中可能不需要它
// 伪调用 1
axios.post(url, {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;'
    },
    params: {
        q: batchText
    },
    paramsSerializer: function (params) {
        return qs.stringify(params, {arrayFormat: 'repeat'}); // !!! repeat 很重要
    },
}).then(res => {
    console.log(batch.fmt(batchText, res.data))
    // {
    //     "0": {
    //         "before": "Casual Dresses",
    //         "after": "休闲装",
    //         "beforeLan": "en"
    //     },
    //     "1": {
    //         "before": "High Quality Midi Dress",
    //         "after": "高品质中长连衣裙",
    //         "beforeLan": "en"
    //     }
    // }
})
// 伪调用 2
axios.post(url, {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;'
    },
    params: data,
    paramsSerializer: function (params) {
        return params
    },
}).then(res => {
    // 同 1
})
1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago