Skip to content

SpectatorNan/go-zero-i18n

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

goi18nx

Usage

Interceptor

Go zero api server

import "github.com/SpectatorNan/go-zero-i18n/goi18nx"

    ......
	var c config.Config
	conf.MustLoad(*configFile, &c)

	ctx := svc.NewServiceContext(c)
	server := rest.MustNewServer(c.RestConf, errorx.RouteMethodNotAllow())
 
    // LocalizationFiles is a slice of toml files path
	// register i18n
	server.Use(goi18nx.NewI18nMiddleware([]language.Tag{
        language.English,
        language.Chinese,
    }, []string{".../active.en.toml",".../active.zh.toml"}).Handle)
	
	......

Go zero rpc server

import "github.com/SpectatorNan/go-zero-i18n/goi18nx"

    ......

    i18n := goi18nx.NewI18nGrpcInterceptor([]language.Tag{
            language.English,
            language.Chinese,
        }, c.LocalizationFiles)
    s.AddUnaryInterceptors(i18n.Interceptor)
	
    ......
	

Format string

    // check context has i18n translator
    goi18nx.IsHasI18n(ctx)

    // context.Context
	// msgKey: localization string's key
	// defaultMsg: default string (if not found in localization file)
    goi18nx.FormatText(ctx, serr.MsgKey, serr.DefaultMsg)

    
    

Support From DB

// Shop is a struct from db
func (s *Shop) Name(ctx context.Context) string {
	langMap := map[language.Tag]string{
		language.English: s.NameEn,
		language.Chinese: s.NameCn,
		// more language mapping
	}
	return goi18nx.LocalizedString(ctx, s.NameCn, langMap)
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages