|
| 1 | +//go:build ignore |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "gopkg.in/ini.v1" |
| 13 | +) |
| 14 | + |
| 15 | +func main() { |
| 16 | + if len(os.Args) != 2 { |
| 17 | + println("usage: backport-locales <to-ref>") |
| 18 | + println("eg: backport-locales release/v1.19") |
| 19 | + os.Exit(1) |
| 20 | + } |
| 21 | + |
| 22 | + ini.PrettyFormat = false |
| 23 | + mustNoErr := func(err error) { |
| 24 | + if err != nil { |
| 25 | + panic(err) |
| 26 | + } |
| 27 | + } |
| 28 | + collectInis := func(ref string) map[string]*ini.File { |
| 29 | + inis := map[string]*ini.File{} |
| 30 | + err := filepath.WalkDir("options/locale", func(path string, d os.DirEntry, err error) error { |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + if d.IsDir() || !strings.HasSuffix(d.Name(), ".ini") { |
| 35 | + return nil |
| 36 | + } |
| 37 | + cfg, err := ini.LoadSources(ini.LoadOptions{ |
| 38 | + IgnoreInlineComment: true, |
| 39 | + UnescapeValueCommentSymbols: true, |
| 40 | + }, path) |
| 41 | + mustNoErr(err) |
| 42 | + inis[path] = cfg |
| 43 | + fmt.Printf("collecting: %s @ %s\n", path, ref) |
| 44 | + return nil |
| 45 | + }) |
| 46 | + mustNoErr(err) |
| 47 | + return inis |
| 48 | + } |
| 49 | + |
| 50 | + // collect new locales from current working directory |
| 51 | + inisNew := collectInis("HEAD") |
| 52 | + |
| 53 | + // switch to the target ref, and collect the old locales |
| 54 | + cmd := exec.Command("git", "checkout", os.Args[1]) |
| 55 | + cmd.Stdout = os.Stdout |
| 56 | + cmd.Stderr = os.Stderr |
| 57 | + mustNoErr(cmd.Run()) |
| 58 | + inisOld := collectInis(os.Args[1]) |
| 59 | + |
| 60 | + // use old en-US as the base, and copy the new translations to the old locales |
| 61 | + enUsOld := inisOld["options/locale/locale_en-US.ini"] |
| 62 | + for path, iniOld := range inisOld { |
| 63 | + if iniOld == enUsOld { |
| 64 | + continue |
| 65 | + } |
| 66 | + iniNew := inisNew[path] |
| 67 | + if iniNew == nil { |
| 68 | + continue |
| 69 | + } |
| 70 | + for _, secEnUS := range enUsOld.Sections() { |
| 71 | + secOld := iniOld.Section(secEnUS.Name()) |
| 72 | + secNew := iniNew.Section(secEnUS.Name()) |
| 73 | + for _, keyEnUs := range secEnUS.Keys() { |
| 74 | + if secNew.HasKey(keyEnUs.Name()) { |
| 75 | + oldStr := secOld.Key(keyEnUs.Name()).String() |
| 76 | + newStr := secNew.Key(keyEnUs.Name()).String() |
| 77 | + // a bug: new translations with ";" are all broken |
| 78 | + if strings.Contains(oldStr, ";") && !strings.Contains(newStr, ";") { |
| 79 | + println("skipping", path, secEnUS.Name(), keyEnUs.Name()) |
| 80 | + continue |
| 81 | + } |
| 82 | + secOld.Key(keyEnUs.Name()).SetValue(newStr) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + mustNoErr(iniOld.SaveTo(path)) |
| 87 | + } |
| 88 | +} |
0 commit comments