Skip to content

Commit cbf43d2

Browse files
rozmansizx2c4
authored andcommitted
message/pipeline: sort maps to generate predictable output
When using gotext utility, the output files are always reshuffled, which makes any kind of source versioning and incremental translation a real challenge. This commit sorts the Go files by package name and order index (file name is not available) before processing the string extraction. Furthermore, an issue with sorting plural cases has been resolved. Originally, it sorted ["two", "few", "one", "other"] wrong. Fixes golang/go#33552 Change-Id: I76fc5d40cf4f989e01ba6d897c0a69029ca30337 Reviewed-on: https://go-review.googlesource.com/c/text/+/207281 Run-TryBot: Jason A. Donenfeld <Jason@zx2c4.com> Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
1 parent 09f8d73 commit cbf43d2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

message/pipeline/extract.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"go/token"
1515
"go/types"
1616
"path/filepath"
17+
"sort"
1718
"strings"
1819
"unicode"
1920
"unicode/utf8"
@@ -509,8 +510,14 @@ func (px packageExtracter) getComment(n ast.Node) string {
509510

510511
func (x *extracter) extractMessages() {
511512
prog := x.iprog
513+
keys := make([]*types.Package, 0, len(x.iprog.AllPackages))
514+
for k := range x.iprog.AllPackages {
515+
keys = append(keys, k)
516+
}
517+
sort.Slice(keys, func(i, j int) bool { return keys[i].Path() < keys[j].Path() })
512518
files := []packageExtracter{}
513-
for _, info := range x.iprog.AllPackages {
519+
for _, k := range keys {
520+
info := x.iprog.AllPackages[k]
514521
for _, f := range info.Files {
515522
// Associate comments with nodes.
516523
px := packageExtracter{

message/pipeline/generate.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,11 @@ func assembleSelect(m *Message, s *Select) (msg catmsg.Message, err error) {
271271
func sortCases(cases []string) {
272272
// TODO: implement full interface.
273273
sort.Slice(cases, func(i, j int) bool {
274-
if cases[j] == "other" && cases[i] != "other" {
274+
switch {
275+
case cases[i] != "other" && cases[j] == "other":
275276
return true
277+
case cases[i] == "other" && cases[j] != "other":
278+
return false
276279
}
277280
// the following code relies on '<' < '=' < any letter.
278281
return cmpNumeric(cases[i], cases[j]) == -1

0 commit comments

Comments
 (0)