Skip to content

Commit ce880f2

Browse files
fnetXLoïc Dachary
authored andcommitted
[I18N] Add Locale merger script
(cherry picked from commit 84469b3c5c5e57f6c8492c4592111b34edc13db7)
1 parent be72ef3 commit ce880f2

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

build/merge-forgejo-locales.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2022 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build ignore
6+
7+
package main
8+
9+
import (
10+
"bufio"
11+
"os"
12+
"regexp"
13+
"strings"
14+
15+
"gopkg.in/ini.v1"
16+
)
17+
18+
const (
19+
trimPrefix = "gitea_"
20+
sourceFolder = "options/locales/"
21+
)
22+
23+
// returns list of locales, still containing the file extension!
24+
func generate_locale_list() []string {
25+
localeFiles, _ := os.ReadDir(sourceFolder)
26+
locales := []string{}
27+
for _, localeFile := range localeFiles {
28+
if !localeFile.IsDir() && strings.HasPrefix(localeFile.Name(), trimPrefix) {
29+
locales = append(locales, strings.TrimPrefix(localeFile.Name(), trimPrefix))
30+
}
31+
}
32+
return locales
33+
}
34+
35+
// replace all occurrences of Gitea with Forgejo
36+
func renameGiteaForgejo(filename string) []byte {
37+
file, err := os.Open(filename)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
replacer := strings.NewReplacer(
43+
"Gitea", "Forgejo",
44+
"https://docs.gitea.io/en-us/install-from-binary/", "https://forgejo.org/download/#installation-from-binary",
45+
"https://github.com/go-gitea/gitea/tree/master/docker", "https://forgejo.org/download/#container-image",
46+
"https://docs.gitea.io/en-us/install-from-package/", "https://forgejo.org/download",
47+
"https://code.gitea.io/gitea", "https://forgejo.org/download",
48+
"code.gitea.io/gitea", "Forgejo",
49+
`<a href="https://github.com/go-gitea/gitea/issues" target="_blank">GitHub</a>`, `<a href="https://codeberg.org/forgejo/forgejo/issues" target="_blank">Codeberg</a>`,
50+
"https://github.com/go-gitea/gitea", "https://codeberg.org/forgejo/forgejo",
51+
"https://blog.gitea.io", "https://forgejo.org/news",
52+
)
53+
54+
out := make([]byte, 0, 1024)
55+
scanner := bufio.NewScanner(file)
56+
scanner.Split(bufio.ScanLines)
57+
for scanner.Scan() {
58+
line := scanner.Text()
59+
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
60+
out = append(out, []byte("\n"+line+"\n")...)
61+
} else if strings.HasPrefix(line, "settings.web_hook_name_gitea") {
62+
out = append(out, []byte("\n"+line+"\n")...)
63+
out = append(out, []byte("settings.web_hook_name_forgejo = Forgejo\n")...)
64+
} else if strings.HasPrefix(line, "migrate.gitea.description") {
65+
re := regexp.MustCompile(`(.*Gitea)`)
66+
out = append(out, []byte(re.ReplaceAllString(line, "${1}/Forgejo")+"\n")...)
67+
} else {
68+
out = append(out, []byte(replacer.Replace(line)+"\n")...)
69+
}
70+
}
71+
file.Close()
72+
return out
73+
}
74+
75+
func main() {
76+
locales := generate_locale_list()
77+
var err error
78+
var localeFile *ini.File
79+
for _, locale := range locales {
80+
giteaLocale := sourceFolder + "gitea_" + locale
81+
localeFile, err = ini.LoadSources(ini.LoadOptions{
82+
IgnoreInlineComment: true,
83+
}, giteaLocale, renameGiteaForgejo(giteaLocale))
84+
if err != nil {
85+
panic(err)
86+
}
87+
err = localeFile.SaveTo("options/locale/locale_" + locale)
88+
if err != nil {
89+
panic(err)
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)