Skip to content

Commit

Permalink
support artificial languages (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder authored May 8, 2019
1 parent 8ad1b00 commit 245a313
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
6 changes: 6 additions & 0 deletions v2/i18n/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ type Bundle struct {
matcher language.Matcher
}

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

// NewBundle returns a bundle with a default language and a default set of plural rules.
func NewBundle(defaultLanguage language.Tag) *Bundle {
b := &Bundle{
defaultLanguage: defaultLanguage,
pluralRules: plural.DefaultRules(),
}
b.pluralRules[artTag] = b.pluralRules.Rule(language.English)
b.addTag(defaultLanguage)
return b
}
Expand Down
45 changes: 15 additions & 30 deletions v2/i18n/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,13 @@ var everythingMessage = MustNewMessage(map[string]string{
func TestPseudoLanguage(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
expected := "simple simple"
expected := "nuqneH"
bundle.MustParseMessageFileBytes([]byte(`
# Comment
simple = "simple simple"
`), "en-double.toml")
localizer := NewLocalizer(bundle, "en-double")
localized, err := localizer.Localize(&LocalizeConfig{MessageID: "simple"})
if err != nil {
t.Fatal(err)
}
if localized != expected {
t.Fatalf("expected %q\ngot %q", expected, localized)
}
}

func TestPseudoLanguagePlural(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
bundle.MustParseMessageFileBytes([]byte(`
[everything]
few = "few translation"
many = "many translation"
one = "one translation"
other = "other translation"
two = "two translation"
zero = "zero translation"
`), "en-double.toml")
localizer := NewLocalizer(bundle, "en-double")
hello = "`+expected+`"
`), "art-x-klingon.toml")
{
expected := "other translation"
localized, err := localizer.Localize(&LocalizeConfig{MessageID: "everything", PluralCount: 2})
localized, err := NewLocalizer(bundle, "art-x-klingon").Localize(&LocalizeConfig{MessageID: "hello"})
if err != nil {
t.Fatal(err)
}
Expand All @@ -73,15 +49,24 @@ zero = "zero translation"
}
}
{
expected := "one translation"
localized, err := localizer.Localize(&LocalizeConfig{MessageID: "everything", PluralCount: 1})
localized, err := NewLocalizer(bundle, "art").Localize(&LocalizeConfig{MessageID: "hello"})
if err != nil {
t.Fatal(err)
}
if localized != expected {
t.Fatalf("expected %q\ngot %q", expected, localized)
}
}
{
expected := ""
localized, err := NewLocalizer(bundle, "en").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) {
Expand Down

0 comments on commit 245a313

Please sign in to comment.