Skip to content

Commit

Permalink
prefer instance languages when selecting from contentMap
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmethurst committed Nov 21, 2023
1 parent ff358cc commit ab07e01
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 11 deletions.
34 changes: 26 additions & 8 deletions internal/typeutils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,37 @@ func ContentToContentLanguage(
for langTagStr, contentStr = range contentMap {
}

// Only `contentMap` is set, with
// more than one value. Map order
// is not guaranteed so we can't
// know the "primary" language.
// Only `contentMap` is set, with more
// than one value. Map order is not
// guaranteed so we can't know the
// "primary" language.
//
// Just stop at the first non-empty
// langTag and langCnt we can find.
// Try to select using our instance's
// configured preferred languages.
//
// If all else fails, just stop at
// the last tag and content we can find.
default:
instanceLanguages := config.GetInstanceLanguages()
for langTagStr, contentStr = range contentMap {
if langTagStr != "" &&
contentStr != "" {
// Check if this language tag
// is in our instance languages.
found := slices.ContainsFunc(
instanceLanguages,
func(l *language.Language) bool {
return l.TagStr == langTagStr
},
)

if found {
// Break loop to lock
// in this selection.
break
}

// Nothing found; loop will either
// continue, or end with whatever
// random key/value we last selected.
}
}

Expand Down
47 changes: 44 additions & 3 deletions internal/typeutils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"context"
"testing"

"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/language"
)

func TestMisskeyReportContentURLs1(t *testing.T) {
Expand Down Expand Up @@ -50,9 +52,10 @@ misskey-formatted`

func TestContentToContentLanguage(t *testing.T) {
type testcase struct {
content gtsmodel.Content
expectedContent string
expectedLang string
content gtsmodel.Content
instanceLanguages language.Languages
expectedContent string
expectedLang string
}

ctx, cncl := context.WithCancel(context.Background())
Expand Down Expand Up @@ -98,7 +101,45 @@ func TestContentToContentLanguage(t *testing.T) {
expectedContent: "bonjour le monde",
expectedLang: "",
},
{
content: gtsmodel.Content{
Content: "",
ContentMap: map[string]string{
"en": "hello world",
"ru": "Привет, мир!",
"nl": "hallo wereld!",
"ca": "Hola món!",
},
},
instanceLanguages: language.Languages{
{TagStr: "en"},
},
expectedContent: "hello world",
expectedLang: "en",
},
{
content: gtsmodel.Content{
Content: "",
ContentMap: map[string]string{
"en": "hello world",
"ru": "Привет, мир!",
"nl": "hallo wereld!",
"ca": "Hola món!",
},
},
instanceLanguages: language.Languages{
{TagStr: "ca"},
},
expectedContent: "Hola món!",
expectedLang: "ca",
},
} {
langs, err := language.InitLangs(testcase.instanceLanguages.TagStrs())
if err != nil {
t.Fatal(err)
}
config.SetInstanceLanguages(langs)

content, language := ContentToContentLanguage(ctx, testcase.content)
if content != testcase.expectedContent {
t.Errorf(
Expand Down

0 comments on commit ab07e01

Please sign in to comment.