1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-30 14:09:42 -05:00
forgejo/build/merge-forgejo-locales.go

98 lines
2.9 KiB
Go
Raw Normal View History

[I18N] Add Locale merger script (cherry picked from commit ecc3dae2acaf567cd6b2888cc5c582588c7bfc53) (cherry picked from commit c6c3fc6ace6b6aa4f4877c5a96da54899f2dc2fa) (cherry picked from commit 4833bd594a49b70896c023b41da8a87ba33add0e) [I18N] in the home page replace GitHub with Forgejo The english string that shows on the home page to encourage for contribution no longer reference GitHub. But some translations still do. In this context, and this context only, replace it with Forgejo even though it may not be correct in some cases. Refs: https://codeberg.org/forgejo/forgejo/pulls/450 (cherry picked from commit 71de77f5ae59617eadda9e4992f1867a7acb7f17) (cherry picked from commit 0b5195e4e4ee1c4829d7f4e127cc48d5c0eb3d72) (cherry picked from commit 1252e83f8c7b22e06bb37622900741caa209e9eb) (cherry picked from commit 2ceb1b2b5faa32ca71716ed4c98d18b0942909e7) (cherry picked from commit 59233055b1a8e9bc79c5501d6e2c2649a6d1b26b) (cherry picked from commit 378ed6a21e0a57c3bb5211eb8ade4db499cdd7d9) (cherry picked from commit 2d661119c3b8a8278458005273f6a32fa3216050) (cherry picked from commit 2a95ff4107786bbf6c5b141a42e2efbd1bb897f8) (cherry picked from commit 012b5444a80cf2aaa61b2b65efebfe0691725422) (cherry picked from commit 2aaee4f7c4132af6f9a6e0d9aafe0218603b8a5c) (cherry picked from commit 52b3f53cb7dfc86a1287baa40f5f5b6dce946a27) (cherry picked from commit 24e665abe748ff34163e7c870aa0b90ffca86c26) (cherry picked from commit 335f8b2bd1971ce3b39b57b2cfd6d8c9af85ab75) (cherry picked from commit da3890938b7a13a467520fccfa6187376eab3050) (cherry picked from commit 5d0cdd5fe228906d378152b2472f7399fa9e3c45) (cherry picked from commit 81d325dfd31f3e4d8a499934d9a4ea4c8c781310) (cherry picked from commit edd6cdc50eb686e92feff9fb50ac009624491d5e) (cherry picked from commit f92199a1d7e3c58ee8bf93e98d8d0b39004f4247) (cherry picked from commit f2beae60c341a180a9a86a985c1dcdc22f7cd9e2) (cherry picked from commit 708ebe98f3c68c4534ae41f75b0e529cc90d3f9b)
2022-12-14 13:55:13 -05:00
// Copyright 2022 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//go:build ignore
package main
import (
"bufio"
"os"
"regexp"
"strings"
"gopkg.in/ini.v1"
)
const (
trimPrefix = "gitea_"
sourceFolder = "options/locales/"
)
// returns list of locales, still containing the file extension!
func generate_locale_list() []string {
localeFiles, _ := os.ReadDir(sourceFolder)
locales := []string{}
for _, localeFile := range localeFiles {
if !localeFile.IsDir() && strings.HasPrefix(localeFile.Name(), trimPrefix) {
locales = append(locales, strings.TrimPrefix(localeFile.Name(), trimPrefix))
}
}
return locales
}
// replace all occurrences of Gitea with Forgejo
func renameGiteaForgejo(filename string) []byte {
file, err := os.Open(filename)
if err != nil {
panic(err)
}
replacer := strings.NewReplacer(
"Gitea", "Forgejo",
"https://docs.gitea.io/en-us/install-from-binary/", "https://forgejo.org/download/#installation-from-binary",
"https://github.com/go-gitea/gitea/tree/master/docker", "https://forgejo.org/download/#container-image",
"https://docs.gitea.io/en-us/install-from-package/", "https://forgejo.org/download",
"https://code.gitea.io/gitea", "https://forgejo.org/download",
"code.gitea.io/gitea", "Forgejo",
`<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>`,
"https://github.com/go-gitea/gitea", "https://codeberg.org/forgejo/forgejo",
"https://blog.gitea.io", "https://forgejo.org/news",
)
out := make([]byte, 0, 1024)
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "license_desc=") {
line = strings.Replace(line, "GitHub", "Forgejo", 1)
}
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
out = append(out, []byte("\n"+line+"\n")...)
} else if strings.HasPrefix(line, "settings.web_hook_name_gitea") {
out = append(out, []byte("\n"+line+"\n")...)
out = append(out, []byte("settings.web_hook_name_forgejo = Forgejo\n")...)
} else if strings.HasPrefix(line, "migrate.gitea.description") {
re := regexp.MustCompile(`(.*Gitea)`)
out = append(out, []byte(re.ReplaceAllString(line, "${1}/Forgejo")+"\n")...)
} else {
out = append(out, []byte(replacer.Replace(line)+"\n")...)
}
}
file.Close()
return out
}
func main() {
locales := generate_locale_list()
var err error
var localeFile *ini.File
for _, locale := range locales {
giteaLocale := sourceFolder + "gitea_" + locale
localeFile, err = ini.LoadSources(ini.LoadOptions{
IgnoreInlineComment: true,
}, giteaLocale, renameGiteaForgejo(giteaLocale))
if err != nil {
panic(err)
}
err = localeFile.SaveTo("options/locale/locale_" + locale)
if err != nil {
panic(err)
}
}
}