From 192fa013647e364564db434c0e2e8b6f2c66cd71 Mon Sep 17 00:00:00 2001 From: Chris Vaughn Date: Thu, 16 Apr 2020 17:29:45 -0500 Subject: [PATCH] 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.