Skip to content

Commit d498472

Browse files
author
Gusted
committed
Use simple map
1 parent 4788be5 commit d498472

File tree

3 files changed

+19
-252
lines changed

3 files changed

+19
-252
lines changed

modules/mph/mph.go

Lines changed: 0 additions & 141 deletions
This file was deleted.

modules/mph/mph_test.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

modules/translation/i18n/i18n.go

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"strings"
1212

1313
"code.gitea.io/gitea/modules/log"
14-
"code.gitea.io/gitea/modules/mph"
1514
"code.gitea.io/gitea/modules/setting"
1615

1716
"gopkg.in/ini.v1"
@@ -31,17 +30,12 @@ type locale struct {
3130

3231
type LocaleStore struct {
3332
// 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
4539
}
4640

4741
func NewLocaleStore() *LocaleStore {
@@ -69,42 +63,16 @@ func (ls *LocaleStore) AddLocaleByIni(langName, langDesc string, localeFile inte
6963
// For development, live-reload of the translation files is important.
7064
// For production, we can do some expensive work and then make the querying fast.
7165
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()
10671
}
10772
}
73+
// Append the key->value to the offsetToTranslationMap variable.
74+
ls.offsetToTranslationMap = append(ls.offsetToTranslationMap, keyToValue)
75+
10876
// Help Go's GC.
10977
iniFile = nil
11078

@@ -207,8 +175,11 @@ func Tr(lang, trKey string, trArgs ...interface{}) string {
207175
}
208176

209177
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+
}
212183

213184
if len(trArgs) > 0 {
214185
fmtArgs := make([]interface{}, 0, len(trArgs))

0 commit comments

Comments
 (0)