Saturday, April 27, 2024
HomeGolangPassing handle - however worth just isn't up to date (go chi...

Passing handle – however worth just isn’t up to date (go chi middleware) – Getting Assist


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

	lang := []string{"en-US", "fr"}

	for i := 0; i < len(lang); i++ {
		bundle.LoadMessageFile(fmt.Sprintf("json/%s.json", lang[I]))
	}
	localizer := i18n.NewLocalizer(bundle, "en-US")
	appConfig := handler.AppConfig{T: t, Localizer: localizer}

	//frequent.ChangeString(&str)
	r.Use(middlewares.I18n(bundle, &appConfig))

middleware

func I18n(bundle *i18n.Bundle, appConfig *handler.AppConfig) func(subsequent http.Handler) http.Handler {
	return func(subsequent http.Handler) http.Handler {
		fn := func(w http.ResponseWriter, r *http.Request) {

			lang := r.URL.Question().Get("lang")

			if lang == "" {
				lang = "en-US"
				url, _ := frequent.FullUrl(r)
				fmt.Println(url)

				q := url.Question()
				q.Add("lang", lang)
				url.RawQuery = q.Encode()

				http.Redirect(w, r, url.String(), http.StatusPermanentRedirect)
			}

			settle for := r.Header.Get("Settle for-Language")
			fmt.Println("settle for", settle for)
			localizer := i18n.NewLocalizer(bundle, lang, settle for)
			appConfig.Localizer = localizer
			appConfig.Lang = lang
			subsequent.ServeHTTP(w, r)
		}
		return http.HandlerFunc(fn)
	}
}

inside some handlers...
fmt.Println(l.config.Lang) //it's empty - what's flawed right here?

Hello,

might you add some description beside the code?
Which packages are you utilizing?

the place these packages come from ?

i18n => “github.com/nicksnyder/go-i18n/v2/i18n
handler => customized bundle which accommodates the struct to carry the config

However it appears, through the use of middleware, it’s not potential to vary the worth. As a result of the whole lot compiled and I get the outdated information solely. I don’t know precisely what occurs.

Proper now, I’ve created a brand new bundle to deal with i18n (native bundle can be named as i18n. I’ve to assume and rename it)

I’ve carried out the translate methodology for that bundle

func (i18 *I18n) Translate(textual content string) string {
	fmt.Println("Translate..", i18)
	localizer := i18n.NewLocalizer(i18.Bundle, i18.Lang)
	str, err := localizer.Localize(&i18n.LocalizeConfig{MessageID: textual content})
	if err != nil {
		return err.Error()
	}

	return str
}

I go the i18n struct to all my templates. Contained in the templates, I name the perform `translate

{{translate "login"}} //translated to Login
I made it working by some means.

//if you really want the code, I can create a easy go chi middleware and share the code.like altering the worth of a string. I attempted that, That can be not working. (even after passing the handle) Previous worth is printed

Hello,
questioning if a greater strategy could be that the middleware provides the localizer to request’s Context as a substitute of modifying the worldwide appConfig.
I believe thos could be a greater strategy to keep away from that a number of requests change the worldwide appConfigg in concurrent means

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments