Internationalization

The Connector’s LoadResString method loads and localizes strings stored in a static resource named text.yaml. LoadResString extracts the request’s locale from the Accept-Language header embedded in the context’s frame, and returns the string in the language best matching it.

func (svc *Service) Localization(w http.ResponseWriter, r *http.Request) (err error) {
	ctx := r.Context()
	localizedStr, _ := svc.LoadResString(ctx, "stringKey")
	w.Write([]byte(localizedStr))
	return nil
}

text.yaml is expected to have the following format:

stringKey:
  default: Localized
  en: Localized
  en-GB: Localised
  fr: Localisée

If a default is not provided, English (en) is used as the fallback language. String keys and locale names are case sensitive.

See Also

  • Hello example/localized reads the Hello key from resources/text.yaml and returns the translation best matching the request’s Accept-Language header (en, fr, es, it, de, pt, da, nl, pl, no, tr, sv).