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 3.9KB

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