Browse Source

Make Language set in the Context, not by the template

Morgan Bazalgette 1 year ago
parent
commit
95a6e39d94
Signed by: Morgan Bazalgette <the@howl.moe> GPG Key ID: 40D328300D245DA5
4 changed files with 15 additions and 11 deletions
  1. 3
    2
      context.go
  2. 9
    8
      funcmap.go
  3. 2
    0
      sessions.go
  4. 1
    1
      templates/base.html

+ 3
- 2
context.go View File

@@ -9,8 +9,9 @@ import (
9 9
 )
10 10
 
11 11
 type context struct {
12
-	User  sessionUser
13
-	Token string
12
+	User     sessionUser
13
+	Token    string
14
+	Language string
14 15
 }
15 16
 type sessionUser struct {
16 17
 	ID         int

+ 9
- 8
funcmap.go View File

@@ -451,14 +451,6 @@ var funcMap = template.FuncMap{
451 451
 		return x.Val()
452 452
 	},
453 453
 	"getBitcoinAddress": btcaddress.Get,
454
-	"getLanguage": func(c *gin.Context) string {
455
-		for _, l := range getLang(c) {
456
-			if in(l, localeLanguages) {
457
-				return l
458
-			}
459
-		}
460
-		return ""
461
-	},
462 454
 	"languageInformation": func() []langInfo {
463 455
 		return languageInformation
464 456
 	},
@@ -559,6 +551,15 @@ func getDiscord() *oauth2.Config {
559 551
 	}
560 552
 }
561 553
 
554
+func getLanguageFromGin(c *gin.Context) string {
555
+	for _, l := range getLang(c) {
556
+		if in(l, localeLanguages) {
557
+			return l
558
+		}
559
+	}
560
+	return ""
561
+}
562
+
562 563
 func init() {
563 564
 	rand.Seed(time.Now().UnixNano())
564 565
 }

+ 2
- 0
sessions.go View File

@@ -70,6 +70,8 @@ func sessionInitializer() func(c *gin.Context) {
70 70
 			addBannedMessage = true
71 71
 		}
72 72
 
73
+		ctx.Language = getLanguageFromGin(c)
74
+
73 75
 		c.Set("context", ctx)
74 76
 		c.Set("session", sess)
75 77
 

+ 1
- 1
templates/base.html View File

@@ -82,7 +82,7 @@
82 82
 			<a href="https://twitter.com/_ripple" title="{{ .T "Follow us on Twitter!" }}"><i class="twitter icon"></i></a>
83 83
 			<a href="https://reddit.com/r/osuripple" title="{{ .T "Share irrelevant opinions on reddit!" }}"><i class="reddit square icon"></i></a> |
84 84
 			<div class="ui scrolling dropdown" style="margin-left: 5px;" id="language-selector">
85
-				{{ $lang := getLanguage .Gin }}
85
+				{{ $lang := .Context.Language }}
86 86
 				{{ with $lang }}
87 87
 					{{ $info := languageInformationByNameShort . }}
88 88
 					<div class="text"><i class="{{ $info.CountryShort }} flag"></i>{{ $info.Name }}</div>

Loading…
Cancel
Save