From 7faca3d38c305ab2ed211e681ddf9d754d3c50af Mon Sep 17 00:00:00 2001 From: Albert Wolszon Date: Wed, 3 Apr 2024 15:58:13 +0200 Subject: [PATCH] Sort ARB keys alphabetically --- convert/poe2arb/converter.go | 10 ++++++++++ convert/poe2arb/testdata/attributes-req-attrs.golden | 6 +++--- convert/poe2arb/testdata/attributes.golden | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/convert/poe2arb/converter.go b/convert/poe2arb/converter.go index 8accdda..1f8a28f 100644 --- a/convert/poe2arb/converter.go +++ b/convert/poe2arb/converter.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "regexp" + "sort" "strings" "github.com/leancodepl/poe2arb/convert" @@ -57,6 +58,15 @@ func (c *Converter) Convert(output io.Writer) error { prefixedRegexp := regexp.MustCompile("(?:([a-zA-Z]+):)?(.*)") var errs []error + // Sort terms by key alphabetically + sort.SliceStable(jsonContents, func(i, j int) bool { + a, b := jsonContents[i], jsonContents[j] + aKey := prefixedRegexp.FindStringSubmatch(a.Term)[2] + bKey := prefixedRegexp.FindStringSubmatch(b.Term)[2] + + return aKey < bKey + }) + for _, term := range jsonContents { // Filter by term prefix matches := prefixedRegexp.FindStringSubmatch(term.Term) diff --git a/convert/poe2arb/testdata/attributes-req-attrs.golden b/convert/poe2arb/testdata/attributes-req-attrs.golden index 6f57654..ee42c36 100644 --- a/convert/poe2arb/testdata/attributes-req-attrs.golden +++ b/convert/poe2arb/testdata/attributes-req-attrs.golden @@ -1,7 +1,5 @@ { "@@locale": "en", - "noAttrs": "Foobar", - "@noAttrs": {}, "attrs": "Foo {bar}", "@attrs": { "placeholders": { @@ -9,5 +7,7 @@ "type": "String" } } - } + }, + "noAttrs": "Foobar", + "@noAttrs": {} } diff --git a/convert/poe2arb/testdata/attributes.golden b/convert/poe2arb/testdata/attributes.golden index 5b849e6..6d4ae6e 100644 --- a/convert/poe2arb/testdata/attributes.golden +++ b/convert/poe2arb/testdata/attributes.golden @@ -1,6 +1,5 @@ { "@@locale": "en", - "noAttrs": "Foobar", "attrs": "Foo {bar}", "@attrs": { "placeholders": { @@ -8,5 +7,6 @@ "type": "String" } } - } + }, + "noAttrs": "Foobar" }