|
| 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 | + |
| 60 | + if strings.HasPrefix(line, "license_desc=") { |
| 61 | + line = strings.Replace(line, "GitHub", "Forgejo", 1) |
| 62 | + } |
| 63 | + |
| 64 | + if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { |
| 65 | + out = append(out, []byte("\n"+line+"\n")...) |
| 66 | + } else if strings.HasPrefix(line, "settings.web_hook_name_gitea") { |
| 67 | + out = append(out, []byte("\n"+line+"\n")...) |
| 68 | + out = append(out, []byte("settings.web_hook_name_forgejo = Forgejo\n")...) |
| 69 | + } else if strings.HasPrefix(line, "migrate.gitea.description") { |
| 70 | + re := regexp.MustCompile(`(.*Gitea)`) |
| 71 | + out = append(out, []byte(re.ReplaceAllString(line, "${1}/Forgejo")+"\n")...) |
| 72 | + } else { |
| 73 | + out = append(out, []byte(replacer.Replace(line)+"\n")...) |
| 74 | + } |
| 75 | + } |
| 76 | + file.Close() |
| 77 | + return out |
| 78 | +} |
| 79 | + |
| 80 | +func main() { |
| 81 | + locales := generate_locale_list() |
| 82 | + var err error |
| 83 | + var localeFile *ini.File |
| 84 | + for _, locale := range locales { |
| 85 | + giteaLocale := sourceFolder + "gitea_" + locale |
| 86 | + localeFile, err = ini.LoadSources(ini.LoadOptions{ |
| 87 | + IgnoreInlineComment: true, |
| 88 | + }, giteaLocale, renameGiteaForgejo(giteaLocale)) |
| 89 | + if err != nil { |
| 90 | + panic(err) |
| 91 | + } |
| 92 | + err = localeFile.SaveTo("options/locale/locale_" + locale) |
| 93 | + if err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments