Skip to content

Commit

Permalink
i8n, language select dropdown, ai built localizations into ES / DE / FR
Browse files Browse the repository at this point in the history
  • Loading branch information
dwot committed Jan 3, 2025
1 parent 22a0d18 commit 2f5cc32
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func main() {

// Public routes
r.GET("/login", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/login.html", gin.H{
"lcl": translations,
Expand Down Expand Up @@ -208,7 +208,7 @@ func main() {
protected.Use(ForcePasswordChangeMiddleware())

protected.GET("/change-password", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/change-password.html", gin.H{
"lcl": translations,
Expand Down
14 changes: 7 additions & 7 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func AddBasicRoutes(r *gin.RouterGroup, version string) {
r.GET("/", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/index.html", gin.H{
"title": "Dashboard",
Expand All @@ -26,7 +26,7 @@ func AddBasicRoutes(r *gin.RouterGroup, version string) {
})

r.GET("/plants", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/plants.html", gin.H{
"title": "Plants",
Expand All @@ -45,7 +45,7 @@ func AddBasicRoutes(r *gin.RouterGroup, version string) {
})

r.GET("/strains", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/strains.html", gin.H{
"title": "Strains",
Expand All @@ -62,7 +62,7 @@ func AddBasicRoutes(r *gin.RouterGroup, version string) {
})

r.GET("/graph/:id", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/graph.html", gin.H{
"title": "Sensor Graphs",
Expand All @@ -78,7 +78,7 @@ func AddBasicRoutes(r *gin.RouterGroup, version string) {
})

r.GET("/plant/:id", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/plant.html", gin.H{
"title": "Plant Details",
Expand Down Expand Up @@ -166,7 +166,7 @@ func AddProtectedApiRoutes(r *gin.RouterGroup) {

func AddProtectedRotues(r *gin.RouterGroup, version string) {
r.GET("/settings", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/settings.html", gin.H{
"title": "Settings",
Expand All @@ -185,7 +185,7 @@ func AddProtectedRotues(r *gin.RouterGroup, version string) {
})

r.GET("/sensors", func(c *gin.Context) {
lang := c.DefaultQuery("lang", "en")
lang := utils.GetLanguage(c)
translations := utils.TranslationService.GetTranslations(lang)
c.HTML(http.StatusOK, "views/sensors.html", gin.H{
"title": "Sensors",
Expand Down
17 changes: 17 additions & 0 deletions utils/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package utils

import (
"embed"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -350,3 +352,18 @@ func (i *I18nManager) GetTranslations(lang string) map[string]string {
}
return translations
}

func GetLanguage(c *gin.Context) string {
lang := "en"
queryLang := c.Query("lang")
sessionLang := sessions.Default(c).Get("lang")
if sessionLang != nil {
lang = sessionLang.(string)
}
if queryLang != "" {
lang = queryLang
sessions.Default(c).Set("lang", lang)
sessions.Default(c).Save()
}
return lang
}
4 changes: 2 additions & 2 deletions web/templates/pages/strains.html
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ <h5 class="modal-title" id="editStrainModalLabel">{{ .lcl.edit_strain }}</h5>
<thead class="table-dark">
<tr>
<th scope="col" data-key="name" data-type="text" class="sortable ${currentSort.key === "name" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.title_strain }} <i class="fa-solid fa-sort"></i></th>
<th scope="col" data-key="breeder" data-type="text" class="sortable ${currentSort.key === "breeder" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.title_breeder }} <i class="fa-solid fa-sort"></i></th>
<th scope="col" data-key="indica" data-type="numeric" class="sortable ${currentSort.key === "indica" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.title_i_s }} <i class="fa-solid fa-sort"></i></th>
<th scope="col" data-key="breeder" data-type="text" class="sortable ${currentSort.key === "breeder" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.breeder }} <i class="fa-solid fa-sort"></i></th>
<th scope="col" data-key="indica" data-type="numeric" class="sortable ${currentSort.key === "indica" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.title_is }} <i class="fa-solid fa-sort"></i></th>
<th scope="col" data-key="autoflower" data-type="text" class="sortable ${currentSort.key === "autoflower" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.title_auto }} <i class="fa-solid fa-sort"></i></th>
<th scope="col" data-key="seed_count" data-type="numeric" class="sortable ${currentSort.key === "seed_count" ? (currentSort.isAscending ? "asc" : "desc") : ""}">{{ .lcl.seed_count }} <i class="fa-solid fa-sort"></i></th>
<th scope="col">Edit </th>
Expand Down

0 comments on commit 2f5cc32

Please sign in to comment.