|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "maps" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "slices" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "decred.org/dcrdex/client/intl" |
| 14 | + oldloc "decred.org/dcrdex/client/webserver/locales" |
| 15 | + newloc "decred.org/dcrdex/client/webserver/newui/locales" |
| 16 | +) |
| 17 | + |
| 18 | +/* |
| 19 | +importlocales looks for entries in the EnUS translations map that don't have |
| 20 | +translations in other maps, and then checks the locales from the old UI to |
| 21 | +see if there is a translation there that can be used, and updates the files. |
| 22 | +*/ |
| 23 | + |
| 24 | +type mapMatch struct { |
| 25 | + filename string |
| 26 | + oldMap map[string]*intl.Translation |
| 27 | + newMap map[string]*intl.Translation |
| 28 | +} |
| 29 | + |
| 30 | +var mapMatches = []mapMatch{ |
| 31 | + {"ar.go", oldloc.Ar, newloc.Ar}, |
| 32 | + {"de-de.go", oldloc.DeDE, newloc.DeDE}, |
| 33 | + {"pl-pl.go", oldloc.PlPL, newloc.PlPL}, |
| 34 | + {"pt-br.go", oldloc.PtBr, newloc.PtBr}, |
| 35 | + {"zh-cn.go", oldloc.ZhCN, newloc.ZhCN}, |
| 36 | +} |
| 37 | + |
| 38 | +func main() { |
| 39 | + newuiLocalesDir, _ := filepath.Abs("../../") |
| 40 | + |
| 41 | + // Extract & sort keys |
| 42 | + orderedKeys := slices.Collect(maps.Keys(newloc.EnUS)) |
| 43 | + slices.Sort(orderedKeys) |
| 44 | + |
| 45 | + // Determine which ones to transfer |
| 46 | + var transferKeys []string |
| 47 | + for _, key := range orderedKeys { |
| 48 | + newTrans := newloc.EnUS[key] |
| 49 | + oldTrans, okOld := oldloc.EnUS[key] |
| 50 | + if okOld && oldTrans.T == newTrans.T { |
| 51 | + transferKeys = append(transferKeys, key) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + for _, match := range mapMatches { |
| 56 | + fpath := filepath.Join(newuiLocalesDir, match.filename) |
| 57 | + content, err := os.ReadFile(fpath) |
| 58 | + if err != nil { |
| 59 | + fmt.Println("Error reading file:", fpath) |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + idx := bytes.LastIndexByte(content, '}') |
| 64 | + if idx == -1 { |
| 65 | + fmt.Println("No closing brace found in", fpath) |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + var newLines []string |
| 70 | + for _, key := range transferKeys { |
| 71 | + // check if key already exists in actual target map |
| 72 | + if _, exists := match.newMap[key]; exists { |
| 73 | + continue |
| 74 | + } |
| 75 | + |
| 76 | + if oldVal, ok := match.oldMap[key]; ok { |
| 77 | + // We need to format it properly. |
| 78 | + if oldVal.Version != 0 { |
| 79 | + newLines = append(newLines, fmt.Sprintf("\t%q: {Version: %d, T: %q},", key, oldVal.Version, oldVal.T)) |
| 80 | + } else { |
| 81 | + newLines = append(newLines, fmt.Sprintf("\t%q: {T: %q},", key, oldVal.T)) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if len(newLines) > 0 { |
| 87 | + prefix := string(content[:idx]) |
| 88 | + prefix = strings.TrimRight(prefix, " \n\r\t") |
| 89 | + if !strings.HasSuffix(prefix, "{") { |
| 90 | + prefix += "\n" |
| 91 | + } else { |
| 92 | + prefix += "\n" |
| 93 | + } |
| 94 | + |
| 95 | + newContent := prefix + strings.Join(newLines, "\n") + "\n}\n" |
| 96 | + err = os.WriteFile(fpath, []byte(newContent), 0644) |
| 97 | + if err != nil { |
| 98 | + fmt.Println("Error writing file:", fpath, err) |
| 99 | + } else { |
| 100 | + fmt.Printf("Updated %s with %d entries\n", match.filename, len(newLines)) |
| 101 | + |
| 102 | + cmd := exec.Command("gofmt", "-w", fpath) |
| 103 | + if err := cmd.Run(); err != nil { |
| 104 | + fmt.Println("Error running gofmt on:", fpath, err) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments