DEV Community

Cover image for GoogletransX update!
Yurii Zinets
Yurii Zinets

Posted on • Edited on

2 2

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!

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay