From 192fa013647e364564db434c0e2e8b6f2c66cd71 Mon Sep 17 00:00:00 2001 From: Chris Vaughn Date: Thu, 16 Apr 2020 17:29:45 -0500 Subject: [PATCH 1/2] call Translation.Get & GetC directly instead of calling Translation.GetN with default of plural=1 * add correct Arabic pluralform rules to test --- fixtures/ar/categories.po | 2 +- locale.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/fixtures/ar/categories.po b/fixtures/ar/categories.po index 16b7718..8a21691 100644 --- a/fixtures/ar/categories.po +++ b/fixtures/ar/categories.po @@ -1,6 +1,6 @@ msgid "" msgstr "" -"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"Plural-Forms: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5);\n" msgid "Alcohol & Tobacco" msgstr "الكحول والتبغ" diff --git a/locale.go b/locale.go index c9575c8..56f18a3 100644 --- a/locale.go +++ b/locale.go @@ -182,7 +182,19 @@ func (l *Locale) GetN(str, plural string, n int, vars ...interface{}) string { // GetD returns the corresponding Translation in the given domain for the given string. // Supports optional parameters (vars... interface{}) to be inserted on the formatted string using the fmt.Printf syntax. func (l *Locale) GetD(dom, str string, vars ...interface{}) string { - return l.GetND(dom, str, str, 1, vars...) + // Sync read + l.RLock() + defer l.RUnlock() + + if l.Domains != nil { + if _, ok := l.Domains[dom]; ok { + if l.Domains[dom] != nil { + return l.Domains[dom].Get(str, vars...) + } + } + } + + return Printf(str, vars...) } // GetND retrieves the (N)th plural form of Translation in the given domain for the given string. @@ -222,7 +234,19 @@ func (l *Locale) GetNC(str, plural string, n int, ctx string, vars ...interface{ // 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 (l *Locale) GetDC(dom, str, ctx string, vars ...interface{}) string { - return l.GetNDC(dom, str, str, 1, ctx, vars...) + // Sync read + l.RLock() + defer l.RUnlock() + + if l.Domains != nil { + if _, ok := l.Domains[dom]; ok { + if l.Domains[dom] != nil { + return l.Domains[dom].GetC(str, ctx, vars...) + } + } + } + + return Printf(str, vars...) } // GetNDC retrieves the (N)th plural form of Translation in the given domain for the given string in the given context. From d791a97f0193116a4c7f8c2db86493f358cc093d Mon Sep 17 00:00:00 2001 From: Chris Vaughn Date: Sun, 19 Apr 2020 14:50:56 -0500 Subject: [PATCH 2/2] call Get & GetC directly instead of using plural=1 for package methods --- fixtures/ar/categories.po | 13 ++++++++ fixtures/ar/no_plural_header.po | 2 ++ gotext.go | 25 +++++++++++++-- gotext_test.go | 57 +++++++++++++++++++++++++++++++++ locale_test.go | 54 ++++++++++++++++++++++++++++++- 5 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 fixtures/ar/no_plural_header.po 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) {