func init() { // TO DO : find another way maybe with assets, go bind-data out1, _ := Asset("en-us.all.yaml") out2, _ := Asset("fr-be.all.yaml") i18n.ParseTranslationFileBytes("en-us.all.yaml", out1) i18n.ParseTranslationFileBytes("fr-be.all.yaml", out2) Tr, _ = i18n.Tfunc("fr-be") }
func loadFromAsset(locale string) (err error) { assetName := locale + ".all.json" assetKey := filepath.Join(resourcePath, assetName) bytes, err := Asset(assetKey) if err != nil { return } err = goi18n.ParseTranslationFileBytes(assetName, bytes) return }
func loadAsset(assetName string) { assetBytes, err := resources.Asset(assetName) if err != nil { panic(fmt.Sprintf("Could not load asset '%s': %s", assetName, err.Error())) } err = go_i18n.ParseTranslationFileBytes(assetName, assetBytes) if err != nil { panic(fmt.Sprintf("Could not load translations '%s': %s", assetName, err.Error())) } }
func loadAsset(assetName string) error { assetBytes, err := resources.Asset(assetName) if err != nil { return fmt.Errorf("Could not load asset '%s': %s", assetName, err.Error()) } err = i18n.ParseTranslationFileBytes(assetName, assetBytes) if err != nil { return fmt.Errorf("Could not load translations '%s': %s", assetName, err.Error()) } return nil }
func Init(config core_config.Reader) go_i18n.TranslateFunc { sources := []string{ config.Locale(), os.Getenv(lcAll), os.Getenv(lang), defaultLocale, } assetNames := resources.AssetNames() for _, source := range sources { if source == "" { continue } for _, l := range language.Parse(source) { if l.Tag == zhTW || l.Tag == zhHK { l.Tag = zhHant } for _, assetName := range assetNames { assetLocale := strings.ToLower(strings.Replace(path.Base(assetName), underscore, hyphen, -1)) if strings.HasPrefix(assetLocale, l.Tag) { assetBytes, err := resources.Asset(assetName) if err != nil { panic(fmt.Sprintf("Could not load asset '%s': %s", assetName, err.Error())) } err = go_i18n.ParseTranslationFileBytes(assetName, assetBytes) if err != nil { panic(fmt.Sprintf("Could not load translations '%s': %s", assetName, err.Error())) } T, err := go_i18n.Tfunc(source) if err == nil { return T } } } } } panic("Unable to find suitable translation") }
func loadLanguage(assets *AssetBin, langFile string) { fp := filepath.Join(l10n, langFile) if err := i18n.ParseTranslationFileBytes(fp, assets.MustLoad(fp)); err != nil { panic("Can't load language '" + langFile + "'; " + err.Error()) } }
func (imw I18N) Handler(ph http.Handler, c context.Context) http.Handler { for _, l := range imw.Languages { file, err := fs.DefaultFS.OpenRoot(imw.Dir, l+".all.json") if err == nil { var b []byte if b, err = ioutil.ReadAll(file); err == nil { err = i18n.ParseTranslationFileBytes(l+".all.json", b) } } if err != nil { panic(fmt.Sprintf("Error opening locale file '%s.all.json': %v\n", l, err)) } } renderer := webfw.GetRenderer(c) renderer.Funcs(imw.TemplateFuncMap()) handler := func(w http.ResponseWriter, r *http.Request) { c.Set(r, context.BaseCtxKey("langs"), imw.Languages) if len(imw.Languages) == 0 { c.Set(r, context.BaseCtxKey("lang"), "") ph.ServeHTTP(w, r) return } found := false uriParts := strings.SplitN(r.RequestURI, "?", 2) if uriParts[0] == "" { uriParts[0] = r.URL.Path } for _, prefix := range imw.IgnoreURLPrefix { if prefix[0] == '/' { prefix = prefix[1:] } if strings.HasPrefix(uriParts[0], imw.Pattern+prefix+"/") { found = true break } if uriParts[0] == imw.Pattern+prefix { found = true break } } if !found { for _, language := range imw.Languages { if uriParts[0] == imw.Pattern+language { url := uriParts[0] + "/" if len(uriParts) > 1 && uriParts[1] != "" { url += "?" + uriParts[1] } http.Redirect(w, r, url, http.StatusFound) return } if strings.HasPrefix(uriParts[0], imw.Pattern+language+"/") { r.URL.Path = imw.Pattern + r.URL.Path[len(imw.Pattern+language+"/"):] uriParts[0] = imw.Pattern + uriParts[0][len(imw.Pattern+language+"/"):] r.RequestURI = strings.Join(uriParts, "?") c.Set(r, context.BaseCtxKey("lang"), language) found = true s := webfw.GetSession(c, r) s.Set("language", language) break } } } if !found { fallback := webfw.GetFallbackLanguage(c, r, imw.FallbackLanguage) index := strings.Index(fallback, "-") short := fallback if index > -1 { short = fallback[:index] } foundShort := false for _, language := range imw.Languages { if language == fallback { found = true break } if language == short { foundShort = true } } var language string if found { language = fallback } else if foundShort { language = short } else { language = imw.FallbackLanguage } url := imw.Pattern + language + uriParts[0][len(imw.Pattern)-1:] if len(uriParts) > 1 && uriParts[1] != "" { url += "?" + uriParts[1] } http.Redirect(w, r, url, http.StatusFound) return } ph.ServeHTTP(w, r) } return http.HandlerFunc(handler) }
// SupressTestingErrorMessages loads a minimal en-US locale for testing purposes only. Should be called in init() func SupressTestingErrorMessages() { _ = i18n.ParseTranslationFileBytes("en-US.json", []byte(`[{"id":"test", "translation":"message"}]`)) }