func TestVerifyTranslationsWithNonStringKeys(t *testing.T) {

	n := nl.New()
	// dutch
	uni := New(n, n)

	loc, _ := uni.GetTranslator("nl")
	if loc.Locale() != "nl" {
		t.Errorf("Expected '%s' Got '%s'", "nl", loc.Locale())
	}

	// cardinal checks

	err := loc.AddCardinal(-1, "je {0} dag hebben verlaten", locales.PluralRuleOne, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	// fail cardinal rules
	expected := &ErrMissingPluralTranslation{translationType: "plural", rule: locales.PluralRuleOther, key: -1}
	err = loc.VerifyTranslations()
	if err == nil || err.Error() != expected.Error() {
		t.Errorf("Expected '%s' Got '%s'", expected, err)
	}
}
Esempio n. 2
0
func main() {

	// NOTE: this example is omitting allot of error checking for brevity
	e := en.New()
	universalTraslator = ut.New(e, e, en_CA.New(), nl.New(), fr.New())

	en, _ := universalTraslator.GetTranslator("en")

	// generally used after parsing an http 'Accept-Language' header
	// and this will try to find a matching locale you support or
	// fallback locale.
	// en, _ := ut.FindTranslator([]string{"en", "en_CA", "nl"})

	// this will help
	fmt.Println("Cardinal Plural Rules:", en.PluralsCardinal())
	fmt.Println("Ordinal Plural Rules:", en.PluralsOrdinal())
	fmt.Println("Range Plural Rules:", en.PluralsRange())

	// add basic language only translations
	// last param indicates if it's ok to override the translation if one already exists
	en.Add("welcome", "Welcome {0} to our test", false)

	// add language translations dependant on cardinal plural rules
	en.AddCardinal("days", "You have {0} day left to register", locales.PluralRuleOne, false)
	en.AddCardinal("days", "You have {0} days left to register", locales.PluralRuleOther, false)

	// add language translations dependant on ordinal plural rules
	en.AddOrdinal("day-of-month", "{0}st", locales.PluralRuleOne, false)
	en.AddOrdinal("day-of-month", "{0}nd", locales.PluralRuleTwo, false)
	en.AddOrdinal("day-of-month", "{0}rd", locales.PluralRuleFew, false)
	en.AddOrdinal("day-of-month", "{0}th", locales.PluralRuleOther, false)

	// add language translations dependant on range plural rules
	// NOTE: only one plural rule for range in 'en' locale
	en.AddRange("between", "It's {0}-{1} days away", locales.PluralRuleOther, false)

	// now lets use the translations we just added, in the same order we added them

	fmt.Println(en.T("welcome", "Joeybloggs"))

	fmt.Println(en.C("days", 1, 0, en.FmtNumber(1, 0))) // you'd normally have variables defined for 1 and 0
	fmt.Println(en.C("days", 2, 0, en.FmtNumber(2, 0)))
	fmt.Println(en.C("days", 10456.25, 2, en.FmtNumber(10456.25, 2)))

	fmt.Println(en.O("day-of-month", 1, 0, en.FmtNumber(1, 0)))
	fmt.Println(en.O("day-of-month", 2, 0, en.FmtNumber(2, 0)))
	fmt.Println(en.O("day-of-month", 3, 0, en.FmtNumber(3, 0)))
	fmt.Println(en.O("day-of-month", 4, 0, en.FmtNumber(4, 0)))
	fmt.Println(en.O("day-of-month", 10456.25, 0, en.FmtNumber(10456.25, 0)))

	fmt.Println(en.R("between", 0, 0, 1, 0, en.FmtNumber(0, 0), en.FmtNumber(1, 0)))
	fmt.Println(en.R("between", 1, 0, 2, 0, en.FmtNumber(1, 0), en.FmtNumber(2, 0)))
	fmt.Println(en.R("between", 1, 0, 100, 0, en.FmtNumber(1, 0), en.FmtNumber(100, 0)))
}
func TestGetFallback(t *testing.T) {

	// dutch
	n := nl.New()
	e := en.New()

	uni := New(e, n)

	trans := uni.GetFallback()

	expected := "en"

	if trans.Locale() != expected {
		t.Errorf("Expected '%s' Got '%s'", expected, trans.Locale())
	}
}
func TestAddTranslator(t *testing.T) {

	e := en.New()
	n := nl.New()
	uni := New(e, n)

	tests := []struct {
		trans         locales.Translator
		expected      error
		expectedError bool
		override      bool
	}{
		{
			trans:    en_CA.New(),
			expected: nil,
			override: false,
		},
		{
			trans:         n,
			expected:      &ErrExistingTranslator{locale: n.Locale()},
			expectedError: true,
			override:      false,
		},
		{
			trans:         e,
			expected:      &ErrExistingTranslator{locale: e.Locale()},
			expectedError: true,
			override:      false,
		},
		{
			trans:    e,
			expected: nil,
			override: true,
		},
	}

	for _, tt := range tests {

		err := uni.AddTranslator(tt.trans, tt.override)
		if err != tt.expected {
			if !tt.expectedError || err.Error() != tt.expected.Error() {
				t.Errorf("Expected '%s' Got '%s'", tt.expected, err)
			}
		}
	}
}
func TestVerifyTranslations(t *testing.T) {

	n := nl.New()
	// dutch
	uni := New(n, n)

	loc, _ := uni.GetTranslator("nl")
	if loc.Locale() != "nl" {
		t.Errorf("Expected '%s' Got '%s'", "nl", loc.Locale())
	}

	// cardinal checks

	err := loc.AddCardinal("day", "je {0} dag hebben verlaten", locales.PluralRuleOne, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	// fail cardinal rules
	expected := &ErrMissingPluralTranslation{translationType: "plural", rule: locales.PluralRuleOther, key: "day"}
	err = loc.VerifyTranslations()
	if err == nil || err.Error() != expected.Error() {
		t.Errorf("Expected '%s' Got '%s'", expected, err)
	}

	// success cardinal
	err = loc.AddCardinal("day", "je {0} dagen hebben verlaten", locales.PluralRuleOther, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	err = loc.VerifyTranslations()
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	// range checks
	err = loc.AddRange("day", "je {0}-{1} dagen hebben verlaten", locales.PluralRuleOther, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	// fail range rules
	expected = &ErrMissingPluralTranslation{translationType: "range", rule: locales.PluralRuleOne, key: "day"}
	err = loc.VerifyTranslations()
	if err == nil || err.Error() != expected.Error() {
		t.Errorf("Expected '%s' Got '%s'", expected, err)
	}

	// success range
	err = loc.AddRange("day", "je {0}-{1} dag hebben verlaten", locales.PluralRuleOne, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	err = loc.VerifyTranslations()
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	// ok so 'nl' aka dutch, ony has one plural rule for ordinals, so going to switch to english from here which has 4

	err = uni.AddTranslator(en.New(), false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	loc, _ = uni.GetTranslator("en")
	if loc.Locale() != "en" {
		t.Errorf("Expected '%s' Got '%s'", "en", loc.Locale())
	}

	// ordinal checks

	err = loc.AddOrdinal("day", "{0}st", locales.PluralRuleOne, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	err = loc.AddOrdinal("day", "{0}rd", locales.PluralRuleFew, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	err = loc.AddOrdinal("day", "{0}th", locales.PluralRuleOther, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	// fail ordinal rules
	expected = &ErrMissingPluralTranslation{translationType: "ordinal", rule: locales.PluralRuleTwo, key: "day"}
	err = loc.VerifyTranslations()
	if err == nil || err.Error() != expected.Error() {
		t.Errorf("Expected '%s' Got '%s'", expected, err)
	}

	// success ordinal

	err = loc.AddOrdinal("day", "{0}nd", locales.PluralRuleTwo, false)
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}

	err = loc.VerifyTranslations()
	if err != nil {
		t.Fatalf("Expected '<nil>' Got '%s'", err)
	}
}
func TestRangeTranslation(t *testing.T) {

	n := nl.New()
	uni := New(n, n)

	// dutch
	nl, found := uni.GetTranslator("nl")
	if !found {
		t.Fatalf("Expected '%t' Got '%t'", true, found)
	}

	translations := []struct {
		key           interface{}
		trans         string
		rule          locales.PluralRule
		expected      error
		expectedError bool
		override      bool
	}{
		// bad translation
		{
			key:           "day",
			trans:         "er -{1} dag vertrokken",
			rule:          locales.PluralRuleOne,
			expected:      &ErrRangeTranslation{text: fmt.Sprintf("error: parameter '%s' not found, are you sure you're adding a Range Translation?", paramZero)},
			expectedError: true,
		},
		// bad translation
		{
			key:           "day",
			trans:         "er {0}- dag vertrokken",
			rule:          locales.PluralRuleOne,
			expected:      &ErrRangeTranslation{text: fmt.Sprintf("error: parameter '%s' not found, a Range Translation requires two parameters", paramOne)},
			expectedError: true,
		},
		{
			key:      "day",
			trans:    "er {0}-{1} dag",
			rule:     locales.PluralRuleOne,
			expected: nil,
		},
		{
			key:      "day",
			trans:    "er zijn {0}-{1} dagen over",
			rule:     locales.PluralRuleOther,
			expected: nil,
		},
		// bad translation
		{
			key:           "day",
			trans:         "er zijn {0}-{1} dagen over",
			rule:          locales.PluralRuleOther,
			expected:      &ErrConflictingTranslation{key: "day", rule: locales.PluralRuleOther, text: "er zijn {0}-{1} dagen over"},
			expectedError: true,
		},
		{
			key:      "day",
			trans:    "er {0}-{1} dag vertrokken",
			rule:     locales.PluralRuleOne,
			expected: nil,
			override: true,
		},
	}

	for _, tt := range translations {

		err := nl.AddRange(tt.key, tt.trans, tt.rule, tt.override)
		if err != tt.expected {
			if !tt.expectedError || err.Error() != tt.expected.Error() {
				t.Errorf("Expected '%#v' Got '%s'", tt.expected, err)
			}
		}
	}

	tests := []struct {
		key           interface{}
		num1          float64
		digits1       uint64
		num2          float64
		digits2       uint64
		param1        string
		param2        string
		expected      string
		expectedError bool
	}{
		{
			key:      "day",
			num1:     1,
			digits1:  0,
			num2:     2,
			digits2:  0,
			param1:   string(nl.FmtNumber(1, 0)),
			param2:   string(nl.FmtNumber(2, 0)),
			expected: "er zijn 1-2 dagen over",
		},
		{
			key:      "day",
			num1:     0,
			digits1:  0,
			num2:     1,
			digits2:  0,
			param1:   string(nl.FmtNumber(0, 0)),
			param2:   string(nl.FmtNumber(1, 0)),
			expected: "er 0-1 dag vertrokken",
		},
		{
			key:      "day",
			num1:     0,
			digits1:  0,
			num2:     2,
			digits2:  0,
			param1:   string(nl.FmtNumber(0, 0)),
			param2:   string(nl.FmtNumber(2, 0)),
			expected: "er zijn 0-2 dagen over",
		},
		// bad translations from here
		{
			key:           "d-day",
			num1:          0,
			digits1:       0,
			num2:          2,
			digits2:       0,
			param1:        string(nl.FmtNumber(0, 0)),
			param2:        string(nl.FmtNumber(2, 0)),
			expected:      "",
			expectedError: true,
		},
	}

	for _, tt := range tests {

		s, err := nl.R(tt.key, tt.num1, tt.digits1, tt.num2, tt.digits2, tt.param1, tt.param2)
		if err != nil {
			if !tt.expectedError && err != ErrUnknowTranslation {
				t.Errorf("Expected '<nil>' Got '%s'", err)
			}
		}

		if s != tt.expected {
			t.Errorf("Expected '%s' Got '%s'", tt.expected, s)
		}
	}
}