diff --git a/fixtures/ar/categories.po b/fixtures/ar/categories.po index 8a21691..170a6c6 100644 --- a/fixtures/ar/categories.po +++ b/fixtures/ar/categories.po @@ -4,3 +4,16 @@ msgstr "" msgid "Alcohol & Tobacco" msgstr "الكحول والتبغ" + +# this test data is purposely missing msgstr +msgid "%d selected" +msgid_plural "%d selected" + +msgid "Load %d more document" +msgid_plural "Load %d more documents" +msgstr[0] "حمّل %d مستندات إضافيّة" +msgstr[1] "حمّل مستند واحد إضافي" +msgstr[2] "حمّل مستندين إضافيين" +msgstr[3] "حمّل %d مستندات إضافيّة" +msgstr[4] "حمّل %d مستندا إضافيّا" +msgstr[5] "حمّل %d مستند إضافي" \ No newline at end of file diff --git a/fixtures/ar/no_plural_header.po b/fixtures/ar/no_plural_header.po new file mode 100644 index 0000000..e09a9e3 --- /dev/null +++ b/fixtures/ar/no_plural_header.po @@ -0,0 +1,2 @@ +msgid "Alcohol & Tobacco" +msgstr "الكحول والتبغ" diff --git a/gotext.go b/gotext.go index 36c4b9d..fbfc17d 100644 --- a/gotext.go +++ b/gotext.go @@ -171,7 +171,20 @@ func GetN(str, plural string, n int, vars ...interface{}) string { // GetD returns the corresponding Translation in the given domain for a given string. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. func GetD(dom, str string, vars ...interface{}) string { - return GetND(dom, str, str, 1, vars...) + // Try to load default package Locale storage + loadStorage(false) + + // Return Translation + globalConfig.RLock() + + if _, ok := globalConfig.storage.Domains[dom]; !ok { + globalConfig.storage.AddDomain(dom) + } + + tr := globalConfig.storage.GetD(dom, str, vars...) + globalConfig.RUnlock() + + return tr } // GetND retrieves the (N)th plural form of Translation in the given domain for a given string. @@ -208,7 +221,15 @@ func GetNC(str, plural string, n int, ctx string, vars ...interface{}) string { // GetDC returns the corresponding Translation in the given domain for the given string in the given context. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. func GetDC(dom, str, ctx string, vars ...interface{}) string { - return GetNDC(dom, str, str, 1, ctx, vars...) + // Try to load default package Locale storage + loadStorage(false) + + // Return Translation + globalConfig.RLock() + tr := globalConfig.storage.GetDC(dom, str, ctx, vars...) + globalConfig.RUnlock() + + return tr } // GetNDC retrieves the (N)th plural form of Translation in the given domain for a given string. diff --git a/gotext_test.go b/gotext_test.go index f095c63..e02cd44 100644 --- a/gotext_test.go +++ b/gotext_test.go @@ -452,3 +452,60 @@ msgstr "Some random Translation in a context" wg.Wait() } + +func TestPackageArabicTranslation(t *testing.T) { + Configure("fixtures/", "ar", "categories") + + // Plurals formula missing + Plural translation string missing + tr := GetD("categories", "Alcohol & Tobacco") + if tr != "الكحول والتبغ" { + t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr) + } + + // Plural translation string present without translations, should get the msgid_plural + tr = GetND("categories", "%d selected", "%d selected", 10) + if tr != "%d selected" { + t.Errorf("Expected to get '%%d selected', but got '%s'", tr) + } + + //Plurals formula present + Plural translation string present and complete + tr = GetND("categories", "Load %d more document", "Load %d more documents", 0) + if tr != "حمّل %d مستندات إضافيّة" { + t.Errorf("Expected to get 'msgstr[0]', but got '%s'", tr) + } + + tr = GetND("categories", "Load %d more document", "Load %d more documents", 1) + if tr != "حمّل مستند واحد إضافي" { + t.Errorf("Expected to get 'msgstr[1]', but got '%s'", tr) + } + + tr = GetND("categories", "Load %d more document", "Load %d more documents", 2) + if tr != "حمّل مستندين إضافيين" { + t.Errorf("Expected to get 'msgstr[2]', but got '%s'", tr) + } + + tr = GetND("categories", "Load %d more document", "Load %d more documents", 6) + if tr != "حمّل %d مستندات إضافيّة" { + t.Errorf("Expected to get 'msgstr[3]', but got '%s'", tr) + } + + tr = GetND("categories", "Load %d more document", "Load %d more documents", 116) + if tr != "حمّل %d مستندا إضافيّا" { + t.Errorf("Expected to get 'msgstr[4]', but got '%s'", tr) + } + + tr = GetND("categories", "Load %d more document", "Load %d more documents", 102) + if tr != "حمّل %d مستند إضافي" { + t.Errorf("Expected to get 'msgstr[5]', but got '%s'", tr) + } +} + +func TestPackageArabicMissingPluralForm(t *testing.T) { + Configure("fixtures/", "ar", "no_plural_header") + + // Get translation + tr := GetD("no_plural_header", "Alcohol & Tobacco") + if tr != "الكحول والتبغ" { + t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr) + } +} diff --git a/locale_test.go b/locale_test.go index 7e0addb..9ceeb74 100644 --- a/locale_test.go +++ b/locale_test.go @@ -504,11 +504,63 @@ func TestArabicTranslation(t *testing.T) { // Add domain l.AddDomain("categories") - // Get translation + // Plurals formula missing + Plural translation string missing tr := l.GetD("categories", "Alcohol & Tobacco") if tr != "الكحول والتبغ" { t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr) } + + // Plural translation string present without translations, should get the msgid_plural + tr = l.GetND("categories", "%d selected", "%d selected", 10) + if tr != "%d selected" { + t.Errorf("Expected to get '%%d selected', but got '%s'", tr) + } + + //Plurals formula present + Plural translation string present and complete + tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 0) + if tr != "حمّل %d مستندات إضافيّة" { + t.Errorf("Expected to get 'msgstr[0]', but got '%s'", tr) + } + + tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 1) + if tr != "حمّل مستند واحد إضافي" { + t.Errorf("Expected to get 'msgstr[1]', but got '%s'", tr) + } + + tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 2) + if tr != "حمّل مستندين إضافيين" { + t.Errorf("Expected to get 'msgstr[2]', but got '%s'", tr) + } + + tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 6) + if tr != "حمّل %d مستندات إضافيّة" { + t.Errorf("Expected to get 'msgstr[3]', but got '%s'", tr) + } + + tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 116) + if tr != "حمّل %d مستندا إضافيّا" { + t.Errorf("Expected to get 'msgstr[4]', but got '%s'", tr) + } + + tr = l.GetND("categories", "Load %d more document", "Load %d more documents", 102) + if tr != "حمّل %d مستند إضافي" { + t.Errorf("Expected to get 'msgstr[5]', but got '%s'", tr) + } + +} + +func TestArabicMissingPluralForm(t *testing.T) { + // Create Locale + l := NewLocale("fixtures/", "ar") + + // Add domain + l.AddDomain("no_plural_header") + + // Get translation + tr := l.GetD("no_plural_header", "Alcohol & Tobacco") + if tr != "الكحول والتبغ" { + t.Errorf("Expected to get 'الكحول والتبغ', but got '%s'", tr) + } } func TestLocaleBinaryEncoding(t *testing.T) {