diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /build/backport-locales.go | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'build/backport-locales.go')
-rw-r--r-- | build/backport-locales.go | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/build/backport-locales.go b/build/backport-locales.go new file mode 100644 index 0000000..3df83ea --- /dev/null +++ b/build/backport-locales.go @@ -0,0 +1,115 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build ignore + +package main + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + + "code.gitea.io/gitea/modules/container" + "code.gitea.io/gitea/modules/setting" +) + +func main() { + if len(os.Args) != 2 { + fmt.Println("usage: backport-locales <to-ref>") + fmt.Println("eg: backport-locales release/v1.19") + os.Exit(1) + } + + mustNoErr := func(err error) { + if err != nil { + panic(err) + } + } + collectInis := func(ref string) map[string]setting.ConfigProvider { + inis := map[string]setting.ConfigProvider{} + err := filepath.WalkDir("options/locale", func(path string, d os.DirEntry, err error) error { + if err != nil { + return err + } + if d.IsDir() || !strings.HasSuffix(d.Name(), ".ini") { + return nil + } + cfg, err := setting.NewConfigProviderForLocale(path) + mustNoErr(err) + inis[path] = cfg + fmt.Printf("collecting: %s @ %s\n", path, ref) + return nil + }) + mustNoErr(err) + return inis + } + + // collect new locales from current working directory + inisNew := collectInis("HEAD") + + // switch to the target ref, and collect the old locales + cmd := exec.Command("git", "checkout", os.Args[1]) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + mustNoErr(cmd.Run()) + inisOld := collectInis(os.Args[1]) + + // use old en-US as the base, and copy the new translations to the old locales + enUsOld := inisOld["options/locale/locale_en-US.ini"] + brokenWarned := make(container.Set[string]) + for path, iniOld := range inisOld { + if iniOld == enUsOld { + continue + } + iniNew := inisNew[path] + if iniNew == nil { + continue + } + for _, secEnUS := range enUsOld.Sections() { + secOld := iniOld.Section(secEnUS.Name()) + secNew := iniNew.Section(secEnUS.Name()) + for _, keyEnUs := range secEnUS.Keys() { + if secNew.HasKey(keyEnUs.Name()) { + oldStr := secOld.Key(keyEnUs.Name()).String() + newStr := secNew.Key(keyEnUs.Name()).String() + broken := oldStr != "" && strings.Count(oldStr, "%") != strings.Count(newStr, "%") + broken = broken || strings.Contains(oldStr, "\n") || strings.Contains(oldStr, "\n") + if broken { + brokenWarned.Add(secOld.Name() + "." + keyEnUs.Name()) + fmt.Println("----") + fmt.Printf("WARNING: skip broken locale: %s , [%s] %s\n", path, secEnUS.Name(), keyEnUs.Name()) + fmt.Printf("\told: %s\n", strings.ReplaceAll(oldStr, "\n", "\\n")) + fmt.Printf("\tnew: %s\n", strings.ReplaceAll(newStr, "\n", "\\n")) + continue + } + secOld.Key(keyEnUs.Name()).SetValue(newStr) + } + } + } + mustNoErr(iniOld.SaveTo(path)) + } + + fmt.Println("========") + + for path, iniNew := range inisNew { + for _, sec := range iniNew.Sections() { + for _, key := range sec.Keys() { + str := sec.Key(key.Name()).String() + broken := strings.Contains(str, "\n") + broken = broken || strings.HasPrefix(str, "`") != strings.HasSuffix(str, "`") + broken = broken || strings.HasPrefix(str, "\"`") + broken = broken || strings.HasPrefix(str, "`\"") + broken = broken || strings.Count(str, `"`)%2 == 1 + broken = broken || strings.Count(str, "`")%2 == 1 + if broken && !brokenWarned.Contains(sec.Name()+"."+key.Name()) { + fmt.Printf("WARNING: found broken locale: %s , [%s] %s\n", path, sec.Name(), key.Name()) + fmt.Printf("\tstr: %s\n", strings.ReplaceAll(str, "\n", "\\n")) + fmt.Println("----") + } + } + } + } +} |