Coder Social home page Coder Social logo

Comments (1)

brutalzinn avatar brutalzinn commented on June 8, 2024

Thanks for this. Saved me a lot of time. A small contribute to take more advantage of distribuition of wrapper between modules:


type Localizer struct {
	bundle *i18n.Bundle
}

var defaultLang = "pt"

var localizer Localizer

func NewLocalizer(languages ...string) Localizer {
	path, err := os.Getwd()
	if err != nil {
		panic("error on get Getwd")
	}

	bundle := i18n.NewBundle(language.English)
	bundle.RegisterUnmarshalFunc("json", json.Unmarshal)

	for _, lang := range languages {
		bundle.MustLoadMessageFile(fmt.Sprintf("%s/translations/%s.json", path, lang))
	}

	localizer = Localizer{bundle: bundle}
	return localizer
}

func Get(id string) string {
	return localizer.get(defaultLang, id)
}

func GetWithData(id string, data map[string]any) string {
	return localizer.getWithData(defaultLang, id, data)
}

func (l Localizer) get(lang string, id string) string {
	localizer := i18n.NewLocalizer(l.bundle, lang)

	cfg := &i18n.LocalizeConfig{
		DefaultMessage: &i18n.Message{
			ID:    id,
			Other: id,
			One:   id,
		},
	}

	str, err := localizer.Localize(cfg)
	if err != nil {
		return id
	}
	return str
}

func (l Localizer) getWithData(lang, id string, data map[string]any) string {
	localizer := i18n.NewLocalizer(l.bundle, lang)

	cfg := &i18n.LocalizeConfig{
		DefaultMessage: &i18n.Message{
			ID:    id,
			Other: id,
		},
		TemplateData: data,
	}
	str, err := localizer.Localize(cfg)
	if err != nil {
		return id
	}

	return str
}

from go-i18n.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.