gitea源码

localestore.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package i18n
  4. import (
  5. "errors"
  6. "fmt"
  7. "html/template"
  8. "slices"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // This file implements the static LocaleStore that will not watch for changes
  13. type locale struct {
  14. store *localeStore
  15. langName string
  16. idxToMsgMap map[int]string // the map idx is generated by store's trKeyToIdxMap
  17. }
  18. var _ Locale = (*locale)(nil)
  19. type localeStore struct {
  20. // After initializing has finished, these fields are read-only.
  21. langNames []string
  22. langDescs []string
  23. localeMap map[string]*locale
  24. trKeyToIdxMap map[string]int
  25. defaultLang string
  26. }
  27. // NewLocaleStore creates a static locale store
  28. func NewLocaleStore() LocaleStore {
  29. return &localeStore{localeMap: make(map[string]*locale), trKeyToIdxMap: make(map[string]int)}
  30. }
  31. // AddLocaleByIni adds locale by ini into the store
  32. func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
  33. if _, ok := store.localeMap[langName]; ok {
  34. return errors.New("lang has already been added")
  35. }
  36. store.langNames = append(store.langNames, langName)
  37. store.langDescs = append(store.langDescs, langDesc)
  38. l := &locale{store: store, langName: langName, idxToMsgMap: make(map[int]string)}
  39. store.localeMap[l.langName] = l
  40. iniFile, err := setting.NewConfigProviderForLocale(source, moreSource)
  41. if err != nil {
  42. return fmt.Errorf("unable to load ini: %w", err)
  43. }
  44. for _, section := range iniFile.Sections() {
  45. for _, key := range section.Keys() {
  46. var trKey string
  47. if section.Name() == "" || section.Name() == "DEFAULT" {
  48. trKey = key.Name()
  49. } else {
  50. trKey = section.Name() + "." + key.Name()
  51. }
  52. idx, ok := store.trKeyToIdxMap[trKey]
  53. if !ok {
  54. idx = len(store.trKeyToIdxMap)
  55. store.trKeyToIdxMap[trKey] = idx
  56. }
  57. l.idxToMsgMap[idx] = key.Value()
  58. }
  59. }
  60. return nil
  61. }
  62. func (store *localeStore) HasLang(langName string) bool {
  63. _, ok := store.localeMap[langName]
  64. return ok
  65. }
  66. func (store *localeStore) ListLangNameDesc() (names, desc []string) {
  67. return store.langNames, store.langDescs
  68. }
  69. // SetDefaultLang sets default language as a fallback
  70. func (store *localeStore) SetDefaultLang(lang string) {
  71. store.defaultLang = lang
  72. }
  73. // Locale returns the locale for the lang or the default language
  74. func (store *localeStore) Locale(lang string) (Locale, bool) {
  75. l, found := store.localeMap[lang]
  76. if !found {
  77. var ok bool
  78. l, ok = store.localeMap[store.defaultLang]
  79. if !ok {
  80. // no default - return an empty locale
  81. l = &locale{store: store, idxToMsgMap: make(map[int]string)}
  82. }
  83. }
  84. return l, found
  85. }
  86. func (store *localeStore) Close() error {
  87. return nil
  88. }
  89. func (l *locale) TrString(trKey string, trArgs ...any) string {
  90. format := trKey
  91. idx, ok := l.store.trKeyToIdxMap[trKey]
  92. if ok {
  93. if msg, ok := l.idxToMsgMap[idx]; ok {
  94. format = msg // use the found translation
  95. } else if def, ok := l.store.localeMap[l.store.defaultLang]; ok {
  96. // try to use default locale's translation
  97. if msg, ok := def.idxToMsgMap[idx]; ok {
  98. format = msg
  99. }
  100. }
  101. }
  102. msg, err := Format(format, trArgs...)
  103. if err != nil {
  104. log.Error("Error whilst formatting %q in %s: %v", trKey, l.langName, err)
  105. }
  106. return msg
  107. }
  108. func (l *locale) TrHTML(trKey string, trArgs ...any) template.HTML {
  109. args := slices.Clone(trArgs)
  110. for i, v := range args {
  111. switch v := v.(type) {
  112. case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, template.HTML:
  113. // for most basic types (including template.HTML which is safe), just do nothing and use it
  114. case string:
  115. args[i] = template.HTMLEscapeString(v)
  116. case fmt.Stringer:
  117. args[i] = template.HTMLEscapeString(v.String())
  118. default:
  119. args[i] = template.HTMLEscapeString(fmt.Sprint(v))
  120. }
  121. }
  122. return template.HTML(l.TrString(trKey, args...))
  123. }
  124. // HasKey returns whether a key is present in this locale or not
  125. func (l *locale) HasKey(trKey string) bool {
  126. idx, ok := l.store.trKeyToIdxMap[trKey]
  127. if !ok {
  128. return false
  129. }
  130. _, ok = l.idxToMsgMap[idx]
  131. return ok
  132. }