DEV Community

Cover image for GoogletransX update!
Yurii Zinets
Yurii Zinets

Posted on • Updated on

GoogletransX update!

https://github.com/yuriizinets/googletransx

Hi guys! :)
A bit earlier, I published my extended version of Googletrans (check here). And now, I'd like to introduce to you some changes

Interface translation

From now, with TranslateInterface you can translate your structured data directly! Without handling fields getting and setting by yourself. It supports all the features, that github.com/stretchr/objx provides (nested interfaces, slices, etc)

Example is here:

package main

import (
    "github.com/yuriizinets/googletransx"
)

func main() {
    input := map[string]interface{}{
        "A": map[string]interface{}{
            "B": "I'm a test",
            "D": []string{"Example", "Example"},
        },
        "C": "Example",
    }
    params := TranslateParams{
        Src:  "en",
        Dest: "ru",
    }
    fields := []TranslateField{
        {
            Src:    "A.B",
            Dest:   "A.B_ru",
            Params: params,
        },
        {
            Src:    "A.D",
            Dest:   "A.D_ru",
            Params: params,
        },
    }
    output, err := TranslateInterface(input, fields)
    if err != nil {
        t.Fatal(err)
    }
    fmt.Println(output) // Will be map[A:map[B:I'm a test B_ru:Я тест D:[Example Example] D_ru:[пример пример]] C:Example]
}
Enter fullscreen mode Exit fullscreen mode

Ordered results from bulk

It wasn't in the plans, but TranslateInterface requires correctly ordered results from BulkTranslate. So, UID field is added for TranslateParams struct. It's optional, you can just leave it as-is, BulkTranslate will put UIDs for you.

Summary

I hope, that changes will be useful for you.
Thank you for your attention, and good luck!

P.S.

Don't forget to check my another articles and packages!

Top comments (0)