https://github.com/spacetab-io/i18n-gin-go
A gin wrappers for a i18n-go package
https://github.com/spacetab-io/i18n-gin-go
Last synced: about 1 month ago
JSON representation
A gin wrappers for a i18n-go package
- Host: GitHub
- URL: https://github.com/spacetab-io/i18n-gin-go
- Owner: spacetab-io
- License: gpl-3.0
- Created: 2020-05-19T06:48:25.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-01T20:35:22.000Z (almost 2 years ago)
- Last Synced: 2025-02-14T22:13:52.821Z (3 months ago)
- Language: Go
- Homepage:
- Size: 40 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# i18n-gin-go
A gin wrappers for a i18n-go package# Translation
## Usage
Basic usage a gin middleware for processing HTTP's requests
```go
package packimport (
"encoding/json"
"net/http""github.com/gin-gonic/gin"
translation_gin "github.com/spacetab-io/i18n-gin-go/translation"
"github.com/spacetab-io/i18n-go/translation"
)var (
// a configuration of default properties of translation context
// it could be loaded from json or yaml configuration file
Conf = translation.Conf{
Display: "ru",
Fallback: "en",
Second: "en",
TranslationList: false,
}
)// A model definition
type Record struct {
Id int `json:"id"`
Name translation.String `json:"name"`
}func (o *Record) ApplyTranslationCtx(ctx translation.Context) {
o.Name.ApplyTranslationCtx(ctx)
}// A router definition
func Router() http.Handler {
router := gin.New()router.Use(translation_gin.Header(&Conf))
router.GET("/record", Handler)
return router
}// A handler of an http request
func Handler(c *gin.Context) {
rec := &Record{
Id: 10,
Name: translation.String {
Translate: map[string]string {
"en": "Hello world!",
"ru": "Здравствуй мир!",
},
},
}
rec.ApplyTranslationCtx(translation_gin.ContextFromGin(&Conf, c))
c.JSON(http.StatusOK, rec)
}
```