-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Use flatten translation keys #36225
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
Merged
Merged
Use flatten translation keys #36225
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b6834f5
Fix update locales because crowdin will not remove empty translated v…
lunny f62c2bf
add copyright
lunny 7e45818
Merge branch 'main' into lunny/update_locales
lunny 4b59e0a
Update tools/update-locales.go
lunny e916663
Fix bug when remove json line caused last charactor maybe comma
lunny fda39c0
Merge branch 'lunny/update_locales' of github.com:lunny/gitea into lu…
lunny e673fc5
Use flatten translation keys
lunny 52d93ad
Merge branch 'main' into lunny/update_locales
lunny e0d613f
revert unnecessary change
lunny a533950
revert unnecessary change
lunny acbc17d
Remove blank value of translation keys and remove all translation lan…
lunny f550a3f
Update update-locales.sh
lunny cafd8c9
Update update-locales.sh
lunny b5c570d
Merge branch 'main' into lunny/update_locales
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright 2025 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| //go:build ignore | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| const ( | ||
| localeDir = "options/locale" | ||
| defaultLocaleFileName = "locale_en-US.json" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := run(); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| // remove empty values | ||
| localeFiles, err := filepath.Glob(filepath.Join(localeDir, "*.json")) | ||
| if err != nil { | ||
| return fmt.Errorf("list %s: %w", localeDir, err) | ||
| } | ||
|
|
||
| for _, file := range localeFiles { | ||
| if err := cleanLocaleFile(file); err != nil { | ||
| return fmt.Errorf("clean %s: %w", file, err) | ||
| } | ||
| } | ||
|
|
||
| // remove incomplete translations | ||
| originalLocalePath := filepath.Join(localeDir, defaultLocaleFileName) | ||
| baselineLines, err := countLines(originalLocalePath) | ||
| if err != nil { | ||
| return fmt.Errorf("count %s: %w", originalLocalePath, err) | ||
| } | ||
| threshold := baselineLines / 4 // 25% of baseline | ||
|
|
||
| for _, file := range localeFiles { | ||
| lines, err := countLines(file) | ||
| if err != nil { | ||
| return fmt.Errorf("count %s: %w", file, err) | ||
| } | ||
| fmt.Printf("Removing locale files with less than %d non-empty keys\n", threshold) | ||
| if lines < threshold { | ||
| fmt.Printf("%s: %d\n", file, lines) | ||
| if err := os.Remove(file); err != nil { | ||
| return fmt.Errorf("remove %s: %w", file, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func cleanLocaleFile(path string) error { | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| return fmt.Errorf("read %s: %w", path, err) | ||
| } | ||
|
|
||
| buf := &bytes.Buffer{} | ||
|
|
||
| var lines = []string{} | ||
| scanner := bufio.NewScanner(bytes.NewReader(data)) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| // remove json lines with empty values | ||
| if strings.HasSuffix(line, `: "",`) || strings.HasSuffix(line, `: ""`) { | ||
|
lunny marked this conversation as resolved.
Outdated
|
||
| continue | ||
| } | ||
| lines = append(lines, line) | ||
| } | ||
|
|
||
| for i:=0; i < len(lines); i++ { | ||
| if (strings.HasSuffix(lines[i], "}") || strings.HasSuffix(lines[i], "},")) && i -1 >= 0 && strings.HasSuffix(lines[i-1], ",") { | ||
| // remove trailing comma before closing brace | ||
| lines[i-1] = strings.TrimSuffix(lines[i-1], ",") | ||
| } | ||
| } | ||
|
|
||
| for _, line := range lines { | ||
| buf.WriteString(line + "\n") | ||
| } | ||
|
|
||
| if err := os.WriteFile(path, buf.Bytes(), 0644); err != nil { | ||
| return fmt.Errorf("write %s: %w", path, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func countLines(path string) (int, error) { | ||
| f, err := os.Open(path) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| defer f.Close() | ||
|
|
||
| reader := bufio.NewReader(f) | ||
| buf := make([]byte, 32*1024) | ||
| lines := 0 | ||
| for { | ||
| n, err := reader.Read(buf) | ||
| lines += bytes.Count(buf[:n], []byte{'\n'}) | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| } | ||
| return lines, nil | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.