aboutsummaryrefslogtreecommitdiffstats
path: root/modules/translation/translation.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-02-15 05:48:45 +0800
committerGitHub <noreply@github.com>2024-02-14 21:48:45 +0000
commitf3eb835886031df7a562abc123c3f6011c81eca8 (patch)
tree6db218680b00a81f2ea46675d5dde94642a232b9 /modules/translation/translation.go
parent94d06be035bac468129903c9f32e785baf3c1c3b (diff)
downloadgitea-f3eb835886031df7a562abc123c3f6011c81eca8.tar.gz
gitea-f3eb835886031df7a562abc123c3f6011c81eca8.zip
Refactor locale&string&template related code (#29165)
Clarify when "string" should be used (and be escaped), and when "template.HTML" should be used (no need to escape) And help PRs like #29059 , to render the error messages correctly.
Diffstat (limited to 'modules/translation/translation.go')
-rw-r--r--modules/translation/translation.go16
1 files changed, 13 insertions, 3 deletions
diff --git a/modules/translation/translation.go b/modules/translation/translation.go
index dba4de6607..b7c18f610a 100644
--- a/modules/translation/translation.go
+++ b/modules/translation/translation.go
@@ -5,6 +5,7 @@ package translation
import (
"context"
+ "html/template"
"sort"
"strings"
"sync"
@@ -27,8 +28,11 @@ var ContextKey any = &contextKey{}
// Locale represents an interface to translation
type Locale interface {
Language() string
- Tr(string, ...any) string
- TrN(cnt any, key1, keyN string, args ...any) string
+ TrString(string, ...any) string
+
+ Tr(key string, args ...any) template.HTML
+ TrN(cnt any, key1, keyN string, args ...any) template.HTML
+
PrettyNumber(v any) string
}
@@ -144,6 +148,8 @@ type locale struct {
msgPrinter *message.Printer
}
+var _ Locale = (*locale)(nil)
+
// NewLocale return a locale
func NewLocale(lang string) Locale {
if lock != nil {
@@ -216,8 +222,12 @@ var trNLangRules = map[string]func(int64) int{
},
}
+func (l *locale) Tr(s string, args ...any) template.HTML {
+ return l.TrHTML(s, args...)
+}
+
// TrN returns translated message for plural text translation
-func (l *locale) TrN(cnt any, key1, keyN string, args ...any) string {
+func (l *locale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
var c int64
if t, ok := cnt.(int); ok {
c = int64(t)