2023-04-17 11:37:23 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package translation
|
|
|
|
|
2024-02-15 05:48:45 +08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
2025-05-03 14:11:01 +00:00
|
|
|
|
|
|
|
"forgejo.org/modules/translation/i18n"
|
2024-02-15 05:48:45 +08:00
|
|
|
)
|
2023-04-17 11:37:23 +08:00
|
|
|
|
2025-05-03 14:11:01 +00:00
|
|
|
// MockLocale provides a mocked locale without any translations, other than those inserted into MockTranslations by a testcase
|
2024-03-04 09:02:51 +08:00
|
|
|
type MockLocale struct {
|
|
|
|
Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang
|
2025-05-03 14:11:01 +00:00
|
|
|
|
|
|
|
MockTranslations map[string]string
|
2024-03-04 09:02:51 +08:00
|
|
|
}
|
2023-04-17 11:37:23 +08:00
|
|
|
|
|
|
|
var _ Locale = (*MockLocale)(nil)
|
|
|
|
|
|
|
|
func (l MockLocale) Language() string {
|
|
|
|
return "en"
|
|
|
|
}
|
|
|
|
|
2024-02-15 05:48:45 +08:00
|
|
|
func (l MockLocale) TrString(s string, _ ...any) string {
|
2025-05-03 14:11:01 +00:00
|
|
|
if val, ok := l.MockTranslations[s]; ok {
|
|
|
|
return val
|
|
|
|
}
|
2023-04-17 11:37:23 +08:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2024-02-15 05:48:45 +08:00
|
|
|
func (l MockLocale) Tr(s string, a ...any) template.HTML {
|
2025-05-03 14:11:01 +00:00
|
|
|
return template.HTML(l.TrString(s))
|
2024-02-15 05:48:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l MockLocale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
|
|
|
|
return template.HTML(key1)
|
2023-04-17 11:37:23 +08:00
|
|
|
}
|
|
|
|
|
2024-12-30 20:03:37 +01:00
|
|
|
func (l MockLocale) TrPluralString(count any, trKey string, trArgs ...any) template.HTML {
|
|
|
|
return template.HTML(trKey)
|
|
|
|
}
|
|
|
|
|
2025-05-03 14:11:01 +00:00
|
|
|
// TrPluralStringAllForms implements Locale.
|
|
|
|
func (l MockLocale) TrPluralStringAllForms(trKey string) ([]string, []string) {
|
|
|
|
return []string{l.TrString(trKey + i18n.PluralFormSeparator + "one"), l.TrString(trKey + i18n.PluralFormSeparator + "other")}, nil
|
|
|
|
}
|
|
|
|
|
2024-03-31 20:17:12 +05:00
|
|
|
func (l MockLocale) TrSize(s int64) ReadableSize {
|
|
|
|
return ReadableSize{fmt.Sprint(s), ""}
|
2024-03-19 15:52:32 +05:00
|
|
|
}
|
|
|
|
|
2025-03-09 15:12:30 +00:00
|
|
|
func (l MockLocale) HasKey(key string) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-04-17 11:37:23 +08:00
|
|
|
func (l MockLocale) PrettyNumber(v any) string {
|
|
|
|
return fmt.Sprint(v)
|
|
|
|
}
|