You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

localestore.go 4.0KB

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