Google 的翻译越来越好了,官方的Golang SDK 已经很完美,这里介绍的是几个非官方发布的有特色的库。

kyai/google-translate-tk

tkktokentkktoken
1
2
3
tkk, _ := gt.GetTKK()
tk := gt.GetTK("hello", tkk)
tk = gt.GetTK("girl", tkk)

bregydoc/gtranslate

tkkottojs
1
2
gtranslate.Translate("I'm alive", language.English, language.Spanish)
gtranslate.TranslateWithParams("I'm alive", gtranslate.TranslateWithParams{From: "en", To: "es"})

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
    "fmt"

    "github.com/bregydoc/gtranslate"
)

func main() {
    text := "Hello World"
    translated, err := gtranslate.TranslateWithParams(
        text,
        gtranslate.TranslationParams{
            From: "en",
            To:   "ja",
        },
    )
    if err != nil {
        panic(err)
    }

    fmt.Printf("en: %s | ja: %s \n", text, translated)
    // en: Hello World | ja: こんにちは世界
}

nubunto/tts

URL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package main

import (
    "io/ioutil"
    "log"
    "os"

    "github.com/nubunto/tts"
)

func main() {
    s, err := tts.Speak(tts.Config{
        Speak:    "olá!",
        Language: "pt-BR",
    })
    if err != nil {
        log.Fatal(err)
    }

    err = ioutil.WriteFile("output.mp3", s.Bytes(), os.ModePerm)
    if err != nil {
        log.Fatal(err)
    }

}

yanzay/googlespeak

nubunto/tts
1
2
3
4
5
6
7
8
9
10
package main

import (
  "github.com/yanzay/googlespeak"
)

func main() {
  googlespeak.Say("hi") // English by default
  googlespeak.Say("привет", "ru")
}

本文网址: https://golangnote.com/topic/266.html 转摘请注明来源