Skip to content
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

Sort terms alphabetically in ARBs #86

Merged
merged 7 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 convert/poe2arb/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"regexp"
"sort"
"strings"

"github.com/leancodepl/poe2arb/convert"
Expand Down Expand Up @@ -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 {
Albert221 marked this conversation as resolved.
Show resolved Hide resolved
a, b := jsonContents[i], jsonContents[j]
aKey := prefixedRegexp.FindStringSubmatch(a.Term)[2]
bKey := prefixedRegexp.FindStringSubmatch(b.Term)[2]

return aKey < bKey
Albert221 marked this conversation as resolved.
Show resolved Hide resolved
})

for _, term := range jsonContents {
// Filter by term prefix
matches := prefixedRegexp.FindStringSubmatch(term.Term)
Expand Down
10 changes: 6 additions & 4 deletions convert/poe2arb/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func TestConverterConvert(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(paths) == 0 {
t.Fatal("no test files found")
}

for _, path := range paths {
_, filename := filepath.Split(path)
Expand Down Expand Up @@ -44,7 +47,7 @@ func TestConverterConvert(t *testing.T) {
expect := string(golden)

// Actual test
actual, err := convert(t, string(source), template, requireResourceAttributes, termPrefix)
actual, err := convert(string(source), template, requireResourceAttributes, termPrefix)

assert.NoError(t, err)
assert.Equal(t, expect, actual)
Expand All @@ -70,15 +73,15 @@ func TestConverterConvert(t *testing.T) {
`

t.Run("issue 41 template", func(t *testing.T) {
actual, err := convert(t, issue41Source, true, false, "")
actual, err := convert(issue41Source, true, false, "")

assert.Error(t, err)
assert.EqualError(t, err, `decoding term "testPlural" failed: missing "other" plural category`)
assert.Equal(t, "", actual)
})

t.Run("issue 41 non-template", func(t *testing.T) {
actual, err := convert(t, issue41Source, false, false, "")
actual, err := convert(issue41Source, false, false, "")

assert.NoError(t, err)
assert.Equal(t, "{\n \"@@locale\": \"en\"\n}\n", actual)
Expand All @@ -94,7 +97,6 @@ func flutterMustParseLocale(lang string) flutter.Locale {
}

func convert(
t *testing.T,
input string,
template bool,
requireResourceAttributes bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"@@locale": "en",
"noAttrs": "Foobar",
"@noAttrs": {},
"attrs": "Foo {bar}",
"@attrs": {
"placeholders": {
"bar": {
"type": "String"
}
}
}
},
"noAttrs": "Foobar",
"@noAttrs": {}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"@@locale": "en",
"noAttrs": "Foobar",
"attrs": "Foo {bar}",
"@attrs": {
"placeholders": {
"bar": {
"type": "String"
}
}
}
},
"noAttrs": "Foobar"
}
Loading