Skip to content

Commit

Permalink
Fix handling of multiple artificial languages
Browse files Browse the repository at this point in the history
This works around what seems to be an upstream by implementing a simplified tag matcher for artificual languages.

Fixes nicksnyder#252
  • Loading branch information
bep committed Apr 24, 2021
1 parent 482713a commit 0a6b4a9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
54 changes: 52 additions & 2 deletions v2/i18n/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,59 @@ type Bundle struct {
matcher language.Matcher
}

// The matcher in x/text/language does not handle artificial languages,
// see https://github.com/golang/go/issues/45749
// This is a simplified matcher thatdelegates to the x/text/language matcher for
// the harder cases.
type matcher struct {
tags []language.Tag
defaultMatcher language.Matcher
}

func newMatcher(tags []language.Tag) language.Matcher {
var hasArt bool
for _, tag := range tags {
base, _ := tag.Base()
hasArt = base == artTagBase
if hasArt {
break
}
}

if !hasArt {
return language.NewMatcher(tags)
}

return matcher{
tags: tags,
defaultMatcher: language.NewMatcher(tags),
}
}

func (m matcher) Match(t ...language.Tag) (language.Tag, int, language.Confidence) {
for _, candidate := range t {
base, _ := candidate.Base()

if base != artTagBase {
break
}

for i, tag := range m.tags {
if tag == candidate {
return candidate, i, language.Exact
}
}
}

return m.defaultMatcher.Match(t...)
}

// artTag is the language tag used for artificial languages
// https://en.wikipedia.org/wiki/Codes_for_constructed_languages
var artTag = language.MustParse("art")
var (
artTag = language.MustParse("art")
artTagBase, _ = artTag.Base()
)

// NewBundle returns a bundle with a default language and a default set of plural rules.
func NewBundle(defaultLanguage language.Tag) *Bundle {
Expand Down Expand Up @@ -126,7 +176,7 @@ func (b *Bundle) addTag(tag language.Tag) {
}
}
b.tags = append(b.tags, tag)
b.matcher = language.NewMatcher(b.tags)
b.matcher = newMatcher(b.tags)
}

// LanguageTags returns the list of language tags
Expand Down
22 changes: 22 additions & 0 deletions v2/i18n/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,28 @@ hello = "`+expected+`"
}
}

func TestPseudoLanguages(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
expected := "a2"
bundle.MustParseMessageFileBytes([]byte(`
hello = "a1"
`), "art-x-a1.toml")
bundle.MustParseMessageFileBytes([]byte(`
hello = "a2"
`), "art-x-a2.toml")

{
localized, err := NewLocalizer(bundle, "art-x-a2").Localize(&LocalizeConfig{MessageID: "hello"})
if err != nil {
t.Fatal(err)
}
if localized != expected {
t.Fatalf("expected %q\ngot %q", expected, localized)
}
}
}

func TestJSON(t *testing.T) {
bundle := NewBundle(language.English)
bundle.MustParseMessageFileBytes([]byte(`{
Expand Down

0 comments on commit 0a6b4a9

Please sign in to comment.