Skip to content

Add a tool to detect possible unused language keys and remove those which can be confirmed #34737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/pull-compliance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ jobs:
GOOS: linux
GOARCH: 386

translations:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- run: make check-locales

docs:
if: needs.files-changed.outputs.docs == 'true' || needs.files-changed.outputs.actions == 'true'
needs: files-changed
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ update-translations:
mv ./translations/*.ini ./options/locale/
rmdir ./translations

.PHONY: check-locales
check-locales:
@echo "Checking translations..."
$(GO) run build/check-locales.go

.PHONY: generate-gitignore
generate-gitignore: ## update gitignore files
$(GO) run build/generate-gitignores.go
Expand Down
124 changes: 124 additions & 0 deletions build/check-locales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

//go:build ignore

package main

import (
"os"
"path/filepath"
"strings"

"code.gitea.io/gitea/modules/setting"

"github.com/gobwas/glob"
)

func searchTranslationKeyInDirs(keys []string) ([]bool, error) {
res := make([]bool, len(keys))
for _, dir := range []string{
"cmd",
"models",
"modules",
"routers",
"services",
"templates",
} {
if err := searchTranslationKeyInDir(dir, keys, &res); err != nil {
return nil, err
}
}
return res, nil
}

func searchTranslationKeyInDir(dir string, keys []string, res *[]bool) error {
return filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() ||
(!strings.HasSuffix(d.Name(), ".go") && !strings.HasSuffix(d.Name(), ".tmpl")) ||
strings.HasSuffix(d.Name(), "_test.go") { // don't search in test files
return nil
}

bs, err := os.ReadFile(path)
if err != nil {
return err
}
for i, key := range keys {
if !(*res)[i] && strings.Contains(string(bs), `"`+key+`"`) {
(*res)[i] = true
}
}
return nil
})
}

var whitelist = []string{
"repo.signing.wont_sign.*",
"repo.issues.role.*",
"repo.commitstatus.*",
"admin.dashboard.*",
"admin.dashboard.cron.*",
"admin.dashboard.task.*",
"repo.migrate.*.description",
"actions.runners.status.*",
"projects.*.display_name",
"admin.notices.*",
"form.NewBranchName", // FIXME: used in integration tests only
}

func isWhitelisted(key string) bool {
for _, w := range whitelist {
if glob.MustCompile(w).Match(key) {
return true
}
}
return false
}

func main() {
if len(os.Args) != 1 {
println("usage: clean-locales")
os.Exit(1)
}

iniFile, err := setting.NewConfigProviderForLocale("options/locale/locale_en-US.ini")
if err != nil {
panic(err)
}

keys := []string{}
for _, section := range iniFile.Sections() {
for _, key := range section.Keys() {
var trKey string
if section.Name() == "" || section.Name() == "DEFAULT" {
trKey = key.Name()
} else {
trKey = section.Name() + "." + key.Name()
}
if isWhitelisted(trKey) {
continue
}
keys = append(keys, trKey)
}
}

results, err := searchTranslationKeyInDirs(keys)
if err != nil {
panic(err)
}

var found bool
for i, result := range results {
if !result {
found = true
println("unused locale key:", keys[i])
}
}
if found {
os.Exit(1) // exit with error if any unused locale key is found
}
}
Loading
Loading