Skip to content

The Hyperscale translation library provides a simple translation manager over x/text package.

License

Notifications You must be signed in to change notification settings

hyperscale-stack/translation

Repository files navigation

Hyperscale Translation Last release Documentation

Go Report Card

Branch Status Coverage
master Build Status Coveralls

The Hyperscale translation library provides a simple translation manager over x/text package.

Example

package main

import (
    "fmt"

    "github.com/hyperscale-stack/locale"
    "github.com/hyperscale-stack/translation"
    "golang.org/x/text/language"
    "golang.org/x/text/message/catalog"
)

func main() {
    // plase use golang.org/x/text/cmd/gotext for build catalog
    c := catalog.NewBuilder(catalog.Fallback(language.English))
    c.SetString(language.English, "Hello World", "Hello World")
    c.SetString(language.French, "Hello World", "Bonjour le monde")

    // the default language is always language.English, for override use translation.WithDefaultLocale()
    trans := translation.New(
        []language.Tag{
            language.English,
            language.French,
        },
        translation.WithCatalog(c),
    )

    ctx := context.Background()

    ctx = locale.ToContext(ctx, language.French)

    trans.Translate(ctx, "Hello World") // return Bonjour le monde
}

License

Hyperscale Translation is licensed under the MIT license.