Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Service ¶
type Service interface {
Translate(ctx context.Context, text, sourceLang, targetLang string) (string, error)
}
Service is a translation service (e.g. Google Translate / DeepL).
type TranslateOption ¶
type TranslateOption func(*translateConfig)
A TranslateOption configures the translation behavior.
func Parallel ¶
func Parallel(n int) TranslateOption
Parallel sets the maximum number of parallel translation requests.
func Preserve ¶
func Preserve(expr *regexp.Regexp) TranslateOption
Preserve (prevent translation of) strings that match the given expr.
A typical use case are placeholder variables. Example:
r, err := t.Translate( context.TODO(), "Hello, {firstName}!", "en", "de", dragoman.Preserve(regexp.MustCompile(`{[a-zA-Z0-9]+?}`)), ) // r: "Hallo, {firstName}!"
type Translator ¶
type Translator struct {
// contains filtered or unexported fields
}
Translator is a structured-text translator.
func (*Translator) Translate ¶
func (t *Translator) Translate( ctx context.Context, input io.Reader, sourceLang, targetLang string, ranger text.Ranger, opts ...TranslateOption, ) ([]byte, error)
Translate the contents of input from sourceLang to targetLang.
Example (Html) ¶
package main import ( "context" "fmt" "log" "os" "strings" "github.com/bounoable/dragoman" "github.com/bounoable/dragoman/format/html" "github.com/bounoable/dragoman/service/deepl" ) func main() { svc := deepl.New(os.Getenv("DEEPL_AUTH_KEY")) dm := dragoman.New(svc) res, err := dm.Translate( context.TODO(), strings.NewReader(`<p>This is an example.</p>`), "en", "de", html.Ranger(), ) if err != nil { log.Fatal(err) } fmt.Println(res) }
Output:
Example (HtmlWithAttributes) ¶
package main import ( "context" "fmt" "log" "os" "strings" "github.com/bounoable/dragoman" "github.com/bounoable/dragoman/format/html" "github.com/bounoable/dragoman/service/deepl" ) func main() { svc := deepl.New(os.Getenv("DEEPL_AUTH_KEY")) dm := dragoman.New(svc) res, err := dm.Translate( context.TODO(), strings.NewReader(`<p title="A title tag.">Hello, here is an <img src="someimage.jpeg" alt="An alternate description."></p>`), "en", "de", html.Ranger( html.WithAttribute("title", "alt"), // allow all `title` and `alt` attributes to be translated // OR html.MustAttributePath("p.title", "img.alt"), // allow `title` attribute of `p` tags and `alt` attribute of `img` tags to be translated ), ) if err != nil { log.Fatal(err) } fmt.Println(res) }
Output:
Example (HtmlWithPlaceholder) ¶
package main import ( "context" "fmt" "log" "os" "regexp" "strings" "github.com/bounoable/dragoman" "github.com/bounoable/dragoman/format/html" "github.com/bounoable/dragoman/service/deepl" ) func main() { svc := deepl.New(os.Getenv("DEEPL_AUTH_KEY")) dm := dragoman.New(svc) res, err := dm.Translate( context.TODO(), strings.NewReader(`<p>Hello, {firstName}, this is an example.</p>`), "en", "de", html.Ranger(), dragoman.Preserve(regexp.MustCompile(`{[a-zA-Z]+?}`)), ) if err != nil { log.Fatal(err) } fmt.Println(res) }
Output:
Example (HtmlWithPlaceholderAndAttributes) ¶
package main import ( "context" "fmt" "log" "os" "regexp" "strings" "github.com/bounoable/dragoman" "github.com/bounoable/dragoman/format/html" "github.com/bounoable/dragoman/service/deepl" ) func main() { svc := deepl.New(os.Getenv("DEEPL_AUTH_KEY")) dm := dragoman.New(svc) res, err := dm.Translate( context.TODO(), strings.NewReader(`<p title="A title tag.">Hello, {firstName}, here is an <img src="someimage.jpeg" alt="An alternate description."></p>`), "en", "de", html.Ranger( html.MustAttributePath("p.title", "img.alt"), // allow `title` attribute of `p` tags and `alt` attribute of `img` tags to be translated ), dragoman.Preserve(regexp.MustCompile(`{[a-zA-Z]+?}`)), ) if err != nil { log.Fatal(err) } fmt.Println(res) }
Output:
Example (Json) ¶
package main import ( "context" "fmt" "log" "os" "strings" "github.com/bounoable/dragoman" "github.com/bounoable/dragoman/format/json" "github.com/bounoable/dragoman/service/deepl" ) func main() { svc := deepl.New(os.Getenv("DEEPL_AUTH_KEY")) dm := dragoman.New(svc) res, err := dm.Translate( context.TODO(), strings.NewReader(`{"title": "This is a title."}`), "en", "de", json.Ranger(), ) if err != nil { log.Fatal(err) } fmt.Println(string(res)) }
Output:
Example (JsonWithPlaceholder) ¶
package main import ( "context" "fmt" "log" "os" "regexp" "strings" "github.com/bounoable/dragoman" "github.com/bounoable/dragoman/format/json" "github.com/bounoable/dragoman/service/deepl" ) func main() { svc := deepl.New(os.Getenv("DEEPL_AUTH_KEY")) dm := dragoman.New(svc) res, err := dm.Translate( context.TODO(), strings.NewReader(`{"greeting": "Hello, {firstName}!"}`), "en", "de", json.Ranger(), dragoman.Preserve(regexp.MustCompile(`{[a-zA-Z]+?}`)), ) if err != nil { log.Fatal(err) } fmt.Println(res) }
Output:
Directories
¶
Path | Synopsis |
---|---|
cmd
|
|
Package directory can be used to translate whole directories.
|
Package directory can be used to translate whole directories. |
mocks
Package mock_directory is a generated GoMock package.
|
Package mock_directory is a generated GoMock package. |
format
|
|
html
Package html provides translation of HTML files.
|
Package html provides translation of HTML files. |
json
Package json provides translation of JSON files.
|
Package json provides translation of JSON files. |
json/internal/lex
Package lex provides a very basic JSON lexer.
|
Package lex provides a very basic JSON lexer. |
Package mock_dragoman is a generated GoMock package.
|
Package mock_dragoman is a generated GoMock package. |
service
|
|
deepl
Package deepl provides the DeepL-backed translation service.
|
Package deepl provides the DeepL-backed translation service. |
deepl/mocks
Package mock_deepl is a generated GoMock package.
|
Package mock_deepl is a generated GoMock package. |
gcloud/mocks
Package mock_gcloud is a generated GoMock package.
|
Package mock_gcloud is a generated GoMock package. |
mocks
Package mock_text is a generated GoMock package.
|
Package mock_text is a generated GoMock package. |
preserve
Package preserve cuts substrings out of strings with the ability to reinsert them at a later time.
|
Package preserve cuts substrings out of strings with the ability to reinsert them at a later time. |
Click to show internal directories.
Click to hide internal directories.