@@ -11,7 +11,6 @@ import (
11
11
"strings"
12
12
13
13
"code.gitea.io/gitea/modules/log"
14
- "code.gitea.io/gitea/modules/mph"
15
14
"code.gitea.io/gitea/modules/setting"
16
15
17
16
"gopkg.in/ini.v1"
@@ -31,17 +30,12 @@ type locale struct {
31
30
32
31
type LocaleStore struct {
33
32
// After initializing has finished, these fields are read-only.
34
- langNames []string
35
- langDescs []string
36
- langOffsets []int
37
- // Hashed values of the keys. Used for the construction of the mph.
38
- translationKeysHashed []string
39
- translationKeys []string
40
- translationValues []string
41
- hashFunction * mph.ConstructedHashFunction
42
- localeMap map [string ]* locale
43
- defaultLang string
44
- defaultLangKeysLen int
33
+ langNames []string
34
+ langDescs []string
35
+ langOffsets []int
36
+ offsetToTranslationMap []map [string ]string
37
+ localeMap map [string ]* locale
38
+ defaultLang string
45
39
}
46
40
47
41
func NewLocaleStore () * LocaleStore {
@@ -69,42 +63,16 @@ func (ls *LocaleStore) AddLocaleByIni(langName, langDesc string, localeFile inte
69
63
// For development, live-reload of the translation files is important.
70
64
// For production, we can do some expensive work and then make the querying fast.
71
65
if setting .IsProd {
72
- // If the language is the default language, then we go trough all keys. These keys
73
- // will become the keys that we consider to support and take into account while going
74
- // trough querying translation keys.
75
- if langName == ls .defaultLang {
76
- // Store all key, value into two slices.
77
- for _ , section := range iniFile .Sections () {
78
- for _ , key := range section .Keys () {
79
- ls .translationKeys = append (ls .translationKeys , section .Name ()+ "#" + key .Name ())
80
- ls .translationKeysHashed = append (ls .translationKeysHashed , strings .TrimPrefix (section .Name ()+ "." + key .Name (), "DEFAULT." ))
81
- ls .translationValues = append (ls .translationValues , key .Value ())
82
- }
83
- }
84
-
85
- ls .defaultLangKeysLen = len (ls .translationKeys )
86
- ls .hashFunction = mph .Build (ls .translationKeysHashed )
87
- } else {
88
- // Go trough all the keys that the defaultLang has and append it to translationValues.
89
- // If the lang doesn't have a value for the translation, use the defaultLang's one.
90
- for i := 0 ; i < ls .defaultLangKeysLen ; i ++ {
91
- splitted := strings .SplitN (ls .translationKeys [i ], "#" , 2 )
92
- // TODO: optimize for repeated sequential access of section.
93
- section , err := iniFile .GetSection (splitted [0 ])
94
- if err != nil {
95
- // Section not found? Use the defaultLang's value for this translation key.
96
- ls .translationValues = append (ls .translationValues , ls .translationValues [i ])
97
- continue
98
- }
99
- key , err := section .GetKey (splitted [1 ])
100
- if err != nil {
101
- // Key not found? Use the defaultLang's value for this translation key.
102
- ls .translationValues = append (ls .translationValues , ls .translationValues [i ])
103
- continue
104
- }
105
- ls .translationValues = append (ls .translationValues , key .Value ())
66
+ keyToValue := map [string ]string {}
67
+ // Go trough all keys and store key->value into a map.
68
+ for _ , section := range iniFile .Sections () {
69
+ for _ , key := range section .Keys () {
70
+ keyToValue [strings .TrimPrefix (section .Name ()+ "." + key .Name (), "DEFAULT." )] = key .Value ()
106
71
}
107
72
}
73
+ // Append the key->value to the offsetToTranslationMap variable.
74
+ ls .offsetToTranslationMap = append (ls .offsetToTranslationMap , keyToValue )
75
+
108
76
// Help Go's GC.
109
77
iniFile = nil
110
78
@@ -207,8 +175,11 @@ func Tr(lang, trKey string, trArgs ...interface{}) string {
207
175
}
208
176
209
177
func TrOffset (offset int , trKey string , trArgs ... interface {}) string {
210
- idx := DefaultLocales .hashFunction .Get (trKey ) + uint32 (offset )* uint32 (DefaultLocales .defaultLangKeysLen )
211
- trMsg := DefaultLocales .translationValues [idx ]
178
+ languageTranslationMap := DefaultLocales .offsetToTranslationMap [offset ]
179
+ trMsg , ok := languageTranslationMap [trKey ]
180
+ if ! ok {
181
+ return trKey
182
+ }
212
183
213
184
if len (trArgs ) > 0 {
214
185
fmtArgs := make ([]interface {}, 0 , len (trArgs ))
0 commit comments