You've already forked golang-base-project
Added multilanguage support
This commit is contained in:
@ -3,7 +3,9 @@ package routes
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"github.com/uberswe/golang-base-project/config"
|
||||
"github.com/uberswe/golang-base-project/lang"
|
||||
"github.com/uberswe/golang-base-project/middleware"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@ -12,13 +14,15 @@ import (
|
||||
type Controller struct {
|
||||
db *gorm.DB
|
||||
config config.Config
|
||||
bundle *i18n.Bundle
|
||||
}
|
||||
|
||||
// New creates a new instance of the routes.Controller
|
||||
func New(db *gorm.DB, c config.Config) Controller {
|
||||
func New(db *gorm.DB, c config.Config, bundle *i18n.Bundle) Controller {
|
||||
return Controller{
|
||||
db: db,
|
||||
config: c,
|
||||
bundle: bundle,
|
||||
}
|
||||
}
|
||||
|
||||
@ -28,6 +32,7 @@ type PageData struct {
|
||||
Messages []Message
|
||||
IsAuthenticated bool
|
||||
CacheParameter string
|
||||
Trans func(s string) string
|
||||
}
|
||||
|
||||
// Message holds a message which can be rendered as responses on HTML pages
|
||||
@ -41,3 +46,14 @@ func isAuthenticated(c *gin.Context) bool {
|
||||
_, exists := c.Get(middleware.UserIDKey)
|
||||
return exists
|
||||
}
|
||||
|
||||
func (controller Controller) DefaultPageData(c *gin.Context) PageData {
|
||||
langService := lang.New(c, controller.bundle)
|
||||
return PageData{
|
||||
Title: "Home",
|
||||
Messages: nil,
|
||||
IsAuthenticated: isAuthenticated(c),
|
||||
CacheParameter: controller.config.CacheParameter,
|
||||
Trans: langService.Trans,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user