code.gitea.io/gitea@v1.21.7/build/backport-locales.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build ignore
     5  
     6  package main
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"os/exec"
    12  	"path/filepath"
    13  	"strings"
    14  
    15  	"code.gitea.io/gitea/modules/container"
    16  	"code.gitea.io/gitea/modules/setting"
    17  )
    18  
    19  func main() {
    20  	if len(os.Args) != 2 {
    21  		println("usage: backport-locales <to-ref>")
    22  		println("eg: backport-locales release/v1.19")
    23  		os.Exit(1)
    24  	}
    25  
    26  	mustNoErr := func(err error) {
    27  		if err != nil {
    28  			panic(err)
    29  		}
    30  	}
    31  	collectInis := func(ref string) map[string]setting.ConfigProvider {
    32  		inis := map[string]setting.ConfigProvider{}
    33  		err := filepath.WalkDir("options/locale", func(path string, d os.DirEntry, err error) error {
    34  			if err != nil {
    35  				return err
    36  			}
    37  			if d.IsDir() || !strings.HasSuffix(d.Name(), ".ini") {
    38  				return nil
    39  			}
    40  			cfg, err := setting.NewConfigProviderForLocale(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  	brokenWarned := make(container.Set[string])
    63  	for path, iniOld := range inisOld {
    64  		if iniOld == enUsOld {
    65  			continue
    66  		}
    67  		iniNew := inisNew[path]
    68  		if iniNew == nil {
    69  			continue
    70  		}
    71  		for _, secEnUS := range enUsOld.Sections() {
    72  			secOld := iniOld.Section(secEnUS.Name())
    73  			secNew := iniNew.Section(secEnUS.Name())
    74  			for _, keyEnUs := range secEnUS.Keys() {
    75  				if secNew.HasKey(keyEnUs.Name()) {
    76  					oldStr := secOld.Key(keyEnUs.Name()).String()
    77  					newStr := secNew.Key(keyEnUs.Name()).String()
    78  					broken := oldStr != "" && strings.Count(oldStr, "%") != strings.Count(newStr, "%")
    79  					broken = broken || strings.Contains(oldStr, "\n") || strings.Contains(oldStr, "\n")
    80  					if broken {
    81  						brokenWarned.Add(secOld.Name() + "." + keyEnUs.Name())
    82  						fmt.Println("----")
    83  						fmt.Printf("WARNING: skip broken locale: %s , [%s] %s\n", path, secEnUS.Name(), keyEnUs.Name())
    84  						fmt.Printf("\told: %s\n", strings.ReplaceAll(oldStr, "\n", "\\n"))
    85  						fmt.Printf("\tnew: %s\n", strings.ReplaceAll(newStr, "\n", "\\n"))
    86  						continue
    87  					}
    88  					secOld.Key(keyEnUs.Name()).SetValue(newStr)
    89  				}
    90  			}
    91  		}
    92  		mustNoErr(iniOld.SaveTo(path))
    93  	}
    94  
    95  	fmt.Println("========")
    96  
    97  	for path, iniNew := range inisNew {
    98  		for _, sec := range iniNew.Sections() {
    99  			for _, key := range sec.Keys() {
   100  				str := sec.Key(key.Name()).String()
   101  				broken := strings.Contains(str, "\n")
   102  				broken = broken || strings.HasPrefix(str, "`") != strings.HasSuffix(str, "`")
   103  				broken = broken || strings.HasPrefix(str, "\"`")
   104  				broken = broken || strings.HasPrefix(str, "`\"")
   105  				broken = broken || strings.Count(str, `"`)%2 == 1
   106  				broken = broken || strings.Count(str, "`")%2 == 1
   107  				if broken && !brokenWarned.Contains(sec.Name()+"."+key.Name()) {
   108  					fmt.Printf("WARNING: found broken locale: %s , [%s] %s\n", path, sec.Name(), key.Name())
   109  					fmt.Printf("\tstr: %s\n", strings.ReplaceAll(str, "\n", "\\n"))
   110  					fmt.Println("----")
   111  				}
   112  			}
   113  		}
   114  	}
   115  }