Skip to content

Make better use of i18n #20096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Jun 26, 2022
Merged
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
24abae5
Prototyping
Jun 21, 2022
48b9b21
Start work on creating offsets
Jun 22, 2022
8b7ca1c
Modify tests
Jun 22, 2022
8427dbd
Start prototyping with actual MPH
Jun 22, 2022
12605ec
Twiddle around
Jun 23, 2022
b25d0d5
Twiddle around comments
Jun 23, 2022
f70b31a
Convert templates
Jun 23, 2022
f466981
Fix external languages
Jun 23, 2022
ddaf0cb
Fix latest translation
Jun 23, 2022
78bdc57
Fix some test
Jun 23, 2022
ace2fd1
Merge branch 'main' into wow-maths
Jun 23, 2022
24c973b
Tidy up code
Jun 23, 2022
4788be5
Merge branch 'main' into wow-maths
Jun 23, 2022
d498472
Use simple map
Jun 24, 2022
c78f1f1
Merge branch 'main' into wow-maths
Jun 24, 2022
957207b
go mod tidy
Jun 24, 2022
6bfa1ba
Move back to data structure
Jun 24, 2022
36c7844
Apply suggestions from code review
Jun 24, 2022
cac046f
Merge branch 'main' into wow-maths
6543 Jun 24, 2022
432e952
Add some comments
Jun 24, 2022
b2935e7
Merge branch 'main' into wow-maths
Jun 24, 2022
3a6a6b4
Fix tests
Jun 24, 2022
b798d02
Try to fix tests
Jun 24, 2022
4ed361f
Merge branch 'main' into wow-maths
Jun 24, 2022
6568ca1
Use en-US as defacto fallback
Jun 25, 2022
47eb2bb
Use correct slices
Jun 25, 2022
8a1b976
refactor (#4)
wxiaoguang Jun 25, 2022
6d3e754
Remove TryTr, add log for missing translation key
Jun 25, 2022
2cffac5
Merge branch 'main' into wow-maths
zeripath Jun 25, 2022
8334002
Refactor i18n
Jun 25, 2022
f20fd87
Fix live-reloading & check for errors
Jun 25, 2022
1978b74
Make linter happy
Jun 25, 2022
b252dfd
Merge branch 'main' into wow-maths
Jun 25, 2022
05d490d
live-reload with periodic check (#5)
wxiaoguang Jun 26, 2022
fbd3e08
Fix tests
Jun 26, 2022
f66ad02
Merge branch 'main' into wow-maths
Jun 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move back to data structure
- Uses less memory by creating for each language a map.
  • Loading branch information
Gusted committed Jun 24, 2022
commit 6bfa1ba87d033574446d90247f543bef00f61c97
73 changes: 54 additions & 19 deletions modules/translation/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ type locale struct {

type LocaleStore struct {
// After initializing has finished, these fields are read-only.
langNames []string
langDescs []string
langOffsets []int
offsetToTranslationMap []map[string]string
localeMap map[string]*locale
defaultLang string
langNames []string
langDescs []string

langOffsets []int
translationKeys []string
keyToOffset map[string]int
translationValues []string

localeMap map[string]*locale

defaultLang string
defaultLangKeysLen int
}

func NewLocaleStore() *LocaleStore {
return &LocaleStore{localeMap: make(map[string]*locale)}
return &LocaleStore{localeMap: make(map[string]*locale), keyToOffset: make(map[string]int)}
}

// AddLocaleByIni adds locale by ini into the store
Expand All @@ -63,15 +69,44 @@ func (ls *LocaleStore) AddLocaleByIni(langName, langDesc string, localeFile inte
// For development, live-reload of the translation files is important.
// For production, we can do some expensive work and then make the querying fast.
if setting.IsProd {
keyToValue := map[string]string{}
// Go trough all keys and store key->value into a map.
for _, section := range iniFile.Sections() {
for _, key := range section.Keys() {
keyToValue[strings.TrimPrefix(section.Name()+"."+key.Name(), "DEFAULT.")] = key.Value()
// If the language is the default language, then we go trough all keys. These keys
// will become the keys that we consider to support and take into account while going
// trough querying translation keys.
if langName == ls.defaultLang {
idx := 0
// Store all key, value into two slices.
for _, section := range iniFile.Sections() {
for _, key := range section.Keys() {
ls.translationKeys = append(ls.translationKeys, section.Name()+"#"+key.Name())
ls.translationValues = append(ls.translationValues, key.Value())

ls.keyToOffset[strings.TrimPrefix(section.Name()+"."+key.Name(), "DEFAULT.")] = idx
idx++
}
}

ls.defaultLangKeysLen = len(ls.translationKeys)
} else {
// Go trough all the keys that the defaultLang has and append it to translationValues.
// If the lang doesn't have a value for the translation, use the defaultLang's one.
for i := 0; i < ls.defaultLangKeysLen; i++ {
splitted := strings.SplitN(ls.translationKeys[i], "#", 2)
// TODO: optimize for repeated sequential access of section.
section, err := iniFile.GetSection(splitted[0])
if err != nil {
// Section not found? Use the defaultLang's value for this translation key.
ls.translationValues = append(ls.translationValues, ls.translationValues[i])
continue
}
key, err := section.GetKey(splitted[1])
if err != nil {
// Key not found? Use the defaultLang's value for this translation key.
ls.translationValues = append(ls.translationValues, ls.translationValues[i])
continue
}
ls.translationValues = append(ls.translationValues, key.Value())
}
}
// Append the key->value to the offsetToTranslationMap variable.
ls.offsetToTranslationMap = append(ls.offsetToTranslationMap, keyToValue)

// Help Go's GC.
iniFile = nil
Expand Down Expand Up @@ -175,12 +210,12 @@ func Tr(lang, trKey string, trArgs ...interface{}) string {
}

func TrOffset(offset int, trKey string, trArgs ...interface{}) string {
languageTranslationMap := DefaultLocales.offsetToTranslationMap[offset]
trMsg, ok := languageTranslationMap[trKey]
if !ok {
return trKey
}
// Get the offset of the translation key.
keyOffset := DefaultLocales.keyToOffset[trKey]
// Now adjust to use the language's translation of the key.
keyOffset += offset * DefaultLocales.defaultLangKeysLen

trMsg := DefaultLocales.translationValues[keyOffset]
if len(trArgs) > 0 {
fmtArgs := make([]interface{}, 0, len(trArgs))
for _, arg := range trArgs {
Expand Down