Skip to content

Commit 4e6cacb

Browse files
committed
i18n: refactor
Create some helper functions. In the future, these functions will be reused by other code.
1 parent 44655be commit 4e6cacb

File tree

1 file changed

+46
-28
lines changed

1 file changed

+46
-28
lines changed

tools/compile-translations.go

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ type TranslationEntry struct {
160160
Translated []byte
161161
}
162162

163+
func (entry *TranslationEntry) IsMetadata() bool {
164+
return len(entry.Untranslated) == 0
165+
}
166+
163167
func ExtractGMOStrings(gmoData []byte) []TranslationEntry {
164168
var magic uint32 = binary.LittleEndian.Uint32(gmoData[0:])
165169
var decode binary.ByteOrder
@@ -189,6 +193,45 @@ func ExtractGMOStrings(gmoData []byte) []TranslationEntry {
189193
return entries
190194
}
191195

196+
// Return value is sorted with no duplicates.
197+
func GetLocaleNames(locales map[string][]TranslationEntry) []string {
198+
localeNames := []string{}
199+
for localeName, _ := range locales {
200+
localeNames = append(localeNames, localeName)
201+
}
202+
// Sort to make output deterministic.
203+
sort.Strings(localeNames)
204+
return localeNames
205+
}
206+
207+
// Extracts .Untranslated from each TranslationEntry.
208+
//
209+
// Return value is sorted with no duplicates.
210+
func GetAllUntranslated(locales map[string][]TranslationEntry) [][]byte {
211+
allUntranslated := [][]byte{}
212+
addUntranslated := func(untranslated []byte) {
213+
for _, existingUntranslated := range allUntranslated {
214+
foundDuplicate := bytes.Equal(existingUntranslated, untranslated)
215+
if foundDuplicate {
216+
return
217+
}
218+
}
219+
allUntranslated = append(allUntranslated, untranslated)
220+
}
221+
for _, localeTranslations := range locales {
222+
for _, translation := range localeTranslations {
223+
if !translation.IsMetadata() {
224+
addUntranslated(translation.Untranslated)
225+
}
226+
}
227+
}
228+
// Sort to make output deterministic.
229+
sort.Slice(allUntranslated, func(i int, j int) bool {
230+
return bytes.Compare(allUntranslated[i], allUntranslated[j]) < 0
231+
})
232+
return allUntranslated
233+
}
234+
192235
type TranslationTable struct {
193236
ConstHashTable []TranslationTableConstHashEntry
194237
ConstHashOffsetBasis uint64
@@ -212,10 +255,6 @@ type TranslationTableMappingEntry struct {
212255
func CreateTranslationTable(locales map[string][]TranslationEntry) TranslationTable {
213256
table := TranslationTable{}
214257

215-
isMetadata := func(entry *TranslationEntry) bool {
216-
return len(entry.Untranslated) == 0
217-
}
218-
219258
addStringToTable := func(stringToAdd []byte, outTable *[]byte) uint32 {
220259
offset := uint32(len(*outTable))
221260
*outTable = append(*outTable, stringToAdd...)
@@ -227,29 +266,8 @@ func CreateTranslationTable(locales map[string][]TranslationEntry) TranslationTa
227266
return addStringToTable(stringToAdd, &table.StringTable)
228267
}
229268

230-
var keys [][]byte
231-
addKey := func(key []byte) {
232-
for _, existingKey := range keys {
233-
foundDuplicate := bytes.Equal(existingKey, key)
234-
if foundDuplicate {
235-
return
236-
}
237-
}
238-
keys = append(keys, key)
239-
}
240-
for localeName, localeTranslations := range locales {
241-
table.Locales = append(table.Locales, localeName)
242-
for _, translation := range localeTranslations {
243-
if !isMetadata(&translation) {
244-
addKey(translation.Untranslated)
245-
}
246-
}
247-
}
248-
// Sort to make output deterministic.
249-
sort.Strings(table.Locales)
250-
sort.Slice(keys, func(i int, j int) bool {
251-
return bytes.Compare(keys[i], keys[j]) < 0
252-
})
269+
keys := GetAllUntranslated(locales)
270+
table.Locales = GetLocaleNames(locales)
253271

254272
for _, localeName := range table.Locales {
255273
addStringToTable([]byte(localeName), &table.LocaleTable)
@@ -312,7 +330,7 @@ retry:
312330
for localeIndex, localeName := range table.Locales {
313331
localeTranslations := locales[localeName]
314332
for _, translation := range localeTranslations {
315-
if !isMetadata(&translation) {
333+
if !translation.IsMetadata() {
316334
constHashEntry := table.FindConstHashEntryByUntranslated(translation.Untranslated)
317335
table.MappingTable[constHashEntry.MappingTableIndex].StringOffsets[localeIndex] = addString(translation.Translated)
318336
}

0 commit comments

Comments
 (0)