mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
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.
This commit is contained in:
@@ -5,6 +5,8 @@ package i18n
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"slices"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -18,6 +20,8 @@ type locale struct {
|
||||
idxToMsgMap map[int]string // the map idx is generated by store's trKeyToIdxMap
|
||||
}
|
||||
|
||||
var _ Locale = (*locale)(nil)
|
||||
|
||||
type localeStore struct {
|
||||
// After initializing has finished, these fields are read-only.
|
||||
langNames []string
|
||||
@@ -85,20 +89,6 @@ func (store *localeStore) SetDefaultLang(lang string) {
|
||||
store.defaultLang = lang
|
||||
}
|
||||
|
||||
// Tr translates content to target language. fall back to default language.
|
||||
func (store *localeStore) Tr(lang, trKey string, trArgs ...any) string {
|
||||
l, _ := store.Locale(lang)
|
||||
|
||||
return l.Tr(trKey, trArgs...)
|
||||
}
|
||||
|
||||
// Has returns whether the given language has a translation for the provided key
|
||||
func (store *localeStore) Has(lang, trKey string) bool {
|
||||
l, _ := store.Locale(lang)
|
||||
|
||||
return l.Has(trKey)
|
||||
}
|
||||
|
||||
// Locale returns the locale for the lang or the default language
|
||||
func (store *localeStore) Locale(lang string) (Locale, bool) {
|
||||
l, found := store.localeMap[lang]
|
||||
@@ -113,13 +103,11 @@ func (store *localeStore) Locale(lang string) (Locale, bool) {
|
||||
return l, found
|
||||
}
|
||||
|
||||
// Close implements io.Closer
|
||||
func (store *localeStore) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Tr translates content to locale language. fall back to default language.
|
||||
func (l *locale) Tr(trKey string, trArgs ...any) string {
|
||||
func (l *locale) TrString(trKey string, trArgs ...any) string {
|
||||
format := trKey
|
||||
|
||||
idx, ok := l.store.trKeyToIdxMap[trKey]
|
||||
@@ -141,8 +129,23 @@ func (l *locale) Tr(trKey string, trArgs ...any) string {
|
||||
return msg
|
||||
}
|
||||
|
||||
// Has returns whether a key is present in this locale or not
|
||||
func (l *locale) Has(trKey string) bool {
|
||||
func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
|
||||
args := slices.Clone(trArgs)
|
||||
for i, v := range args {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
args[i] = template.HTML(template.HTMLEscapeString(v))
|
||||
case fmt.Stringer:
|
||||
args[i] = template.HTMLEscapeString(v.String())
|
||||
default: // int, float, include template.HTML
|
||||
// do nothing, just use it
|
||||
}
|
||||
}
|
||||
return template.HTML(l.TrString(trKey, args...))
|
||||
}
|
||||
|
||||
// HasKey returns whether a key is present in this locale or not
|
||||
func (l *locale) HasKey(trKey string) bool {
|
||||
idx, ok := l.store.trKeyToIdxMap[trKey]
|
||||
if !ok {
|
||||
return false
|
||||
|
Reference in New Issue
Block a user