Skip to content

Commit 058e980

Browse files
committed
Add support for falling back to a custom plural resolver if plural forms were not specified in the .po file
1 parent 8cc53ef commit 058e980

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

domain.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ type Domain struct {
4545
trBuffer *Translation
4646
ctxBuffer string
4747
refBuffer string
48+
49+
customPluralResolver func(int) int
4850
}
4951

5052
// Preserve MIMEHeader behaviour, without the canonicalisation
@@ -88,13 +90,21 @@ func NewDomain() *Domain {
8890
return domain
8991
}
9092

93+
func (do *Domain) SetPluralResolver(f func(int) int) {
94+
do.customPluralResolver = f
95+
}
96+
9197
func (do *Domain) pluralForm(n int) int {
9298
// do we really need locking here? not sure how this plurals.Expression works, so sticking with it for now
9399
do.pluralMutex.RLock()
94100
defer do.pluralMutex.RUnlock()
95101

96102
// Failure fallback
97103
if do.pluralforms == nil {
104+
if do.customPluralResolver != nil {
105+
return do.customPluralResolver(n)
106+
}
107+
98108
/* Use the Germanic plural rule. */
99109
if n == 1 {
100110
return 0

po.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ func (po *Po) GetRefs(str string) []string {
8585
return po.domain.GetRefs(str)
8686
}
8787

88+
func (po *Po) SetPluralResolver(f func(int) int) {
89+
po.domain.customPluralResolver = f
90+
}
91+
8892
func (po *Po) Set(id, str string) {
8993
po.domain.Set(id, str)
9094
}

0 commit comments

Comments
 (0)