libretranslate

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2021 License: MIT Imports: 10 Imported by: 5

README

LibreTranslate in golang

LibreTranslate is an Open Source Machine Translation
API Docs | Self-Hosted

Install:
go get github.com/snakesel/libretranslate
Example usage:
package main

import (
    "fmt"
    tr "github.com/snakesel/libretranslate"
)

func main() {
    translate := tr.New(tr.Config{
        Url:   "https://libretranslate.com",
        Key:   "XXX",
    })

    // you can use "auto" for source language
    // so, translator will detect language
    trtext, err := translate.Translate("Hello, World!", "auto", "ru")
    if err == nil {
        fmt.Println(trtext)
    } else {
        fmt.Println(err.Error())
    }

    // Detect the language of the text
    conf, lang, err = translate.Detect("Nächster Stil")
    if err == nil {
        fmt.Printf("%s (%f)", lang, conf)
    } else {
        fmt.Println(err.Error())
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Url string
	Key string
	// Debug is an optional writer which will be used for debug output.
	Debug io.Writer
}

Config is the configuration struct you should pass to New().

type Translation

type Translation struct {
	Config
	// contains filtered or unexported fields
}

func New

func New(conf Config) *Translation

New returns a new Translation.

func (*Translation) Detect

func (tr *Translation) Detect(text string) (float32, string, error)

Detect the language of the text Return: confidence, language, error

func (*Translation) Translate

func (tr *Translation) Translate(source, sourceLang, targetLang string) (string, error)

Translate text from a language to another

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL