* replace interface{} with any
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-07-05 09:46:50 +03:00
коммит произвёл GitHub
родитель b45ff0be5d
Коммит 717a4d04a9
258 изменённых файлов: 1286 добавлений и 1286 удалений

Просмотреть файл

@@ -20,7 +20,7 @@ import (
const defaultLocale = "en"
// TranslateFunc is the type of the translate functions
type TranslateFunc func(translationID string, args ...interface{}) string
type TranslateFunc func(translationID string, args ...any) string
// T is the translate function using the default server language as fallback language
var T TranslateFunc
@@ -135,7 +135,7 @@ func GetSupportedLocales() map[string]string {
func tfuncWithFallback(pref string) TranslateFunc {
t, _ := i18n.Tfunc(pref)
return func(translationID string, args ...interface{}) string {
return func(translationID string, args ...any) string {
if translated := t(translationID, args...); translated != translationID {
return translated
}
@@ -147,21 +147,21 @@ func tfuncWithFallback(pref string) TranslateFunc {
// TranslateAsHTML translates the translationID provided and return a
// template.HTML object
func TranslateAsHTML(t TranslateFunc, translationID string, args map[string]interface{}) template.HTML {
func TranslateAsHTML(t TranslateFunc, translationID string, args map[string]any) template.HTML {
message := t(translationID, escapeForHTML(args))
message = strings.Replace(message, "[[", "<strong>", -1)
message = strings.Replace(message, "]]", "</strong>", -1)
return template.HTML(message)
}
func escapeForHTML(arg interface{}) interface{} {
func escapeForHTML(arg any) any {
switch typedArg := arg.(type) {
case string:
return template.HTMLEscapeString(typedArg)
case *string:
return template.HTMLEscapeString(*typedArg)
case map[string]interface{}:
safeArg := make(map[string]interface{}, len(typedArg))
case map[string]any:
safeArg := make(map[string]any, len(typedArg))
for key, value := range typedArg {
safeArg[key] = escapeForHTML(value)
}
@@ -179,7 +179,7 @@ func escapeForHTML(arg interface{}) interface{} {
// IdentityTfunc returns a translation function that don't translate, only
// returns the same id
func IdentityTfunc() TranslateFunc {
return func(translationID string, args ...interface{}) string {
return func(translationID string, args ...any) string {
return translationID
}
}

Просмотреть файл

@@ -16,7 +16,7 @@ var htmlTestTranslationBundle *bundle.Bundle
func init() {
htmlTestTranslationBundle = bundle.New()
fooBold, _ := translation.NewTranslation(map[string]interface{}{
fooBold, _ := translation.NewTranslation(map[string]any{
"id": "foo.bold",
"translation": "<p>[[{{ .Foo }}]]</p>",
})
@@ -24,7 +24,7 @@ func init() {
}
func TestTranslateAsHTML(t *testing.T) {
assert.EqualValues(t, "<p><strong>&lt;i&gt;foo&lt;/i&gt;</strong></p>", TranslateAsHTML(TranslateFunc(htmlTestTranslationBundle.MustTfunc("en")), "foo.bold", map[string]interface{}{
assert.EqualValues(t, "<p><strong>&lt;i&gt;foo&lt;/i&gt;</strong></p>", TranslateAsHTML(TranslateFunc(htmlTestTranslationBundle.MustTfunc("en")), "foo.bold", map[string]any{
"Foo": "<i>foo</i>",
}))
}
@@ -32,8 +32,8 @@ func TestTranslateAsHTML(t *testing.T) {
func TestEscapeForHTML(t *testing.T) {
stringForPointer := "<b>abc</b>"
for name, tc := range map[string]struct {
In interface{}
Expected interface{}
In any
Expected any
}{
"NoHTML": {
In: "abc",
@@ -48,11 +48,11 @@ func TestEscapeForHTML(t *testing.T) {
Expected: "&lt;b&gt;abc&lt;/b&gt;",
},
"Map": {
In: map[string]interface{}{
In: map[string]any{
"abc": "abc",
"123": "<b>123</b>",
},
Expected: map[string]interface{}{
Expected: map[string]any{
"abc": "abc",
"123": "&lt;b&gt;123&lt;/b&gt;",
},