https://github.com/spectatornan/go-zero-i18n
https://github.com/spectatornan/go-zero-i18n
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/spectatornan/go-zero-i18n
- Owner: SpectatorNan
- License: mit
- Created: 2022-04-26T08:39:49.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2025-12-27T16:06:16.000Z (6 months ago)
- Last Synced: 2025-12-29T12:55:16.187Z (6 months ago)
- Language: Go
- Size: 56.6 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goi18nx
## Usage
### Interceptor
#### Go zero api server
```go
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
```go
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
```go
// 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
```go
// 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)
}
```