gitea源码

mock.go 1000B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package translation
  4. import (
  5. "fmt"
  6. "html/template"
  7. "strings"
  8. )
  9. // MockLocale provides a mocked locale without any translations
  10. type MockLocale struct {
  11. Lang, LangName string // these fields are used directly in templates: ctx.Locale.Lang
  12. }
  13. var _ Locale = (*MockLocale)(nil)
  14. func (l MockLocale) Language() string {
  15. return "en"
  16. }
  17. func (l MockLocale) TrString(s string, args ...any) string {
  18. return sprintAny(s, args...)
  19. }
  20. func (l MockLocale) Tr(s string, args ...any) template.HTML {
  21. return template.HTML(sprintAny(s, args...))
  22. }
  23. func (l MockLocale) TrN(cnt any, key1, keyN string, args ...any) template.HTML {
  24. return template.HTML(sprintAny(key1, args...))
  25. }
  26. func (l MockLocale) PrettyNumber(v any) string {
  27. return fmt.Sprint(v)
  28. }
  29. func sprintAny(s string, args ...any) string {
  30. if len(args) == 0 {
  31. return s
  32. }
  33. return s + ":" + fmt.Sprintf(strings.Repeat(",%v", len(args))[1:], args...)
  34. }