@@ -17,6 +17,7 @@ import (
1717type Locale interface {
1818 Language () string
1919 Tr (string , ... interface {}) string
20+ TrN (cnt interface {}, key1 , keyN string , args ... interface {}) string
2021}
2122
2223// LangType represents a lang type
@@ -99,3 +100,66 @@ func (l *locale) Language() string {
99100func (l * locale ) Tr (format string , args ... interface {}) string {
100101 return i18n .Tr (l .Lang , format , args ... )
101102}
103+
104+ // Language specific rules for translating plural texts
105+ var trNLangRules = map [string ]func (int64 ) int {
106+ "en-US" : func (cnt int64 ) int {
107+ if cnt == 1 {
108+ return 0
109+ }
110+ return 1
111+ },
112+ "lv-LV" : func (cnt int64 ) int {
113+ if cnt % 10 == 1 && cnt % 100 != 11 {
114+ return 0
115+ }
116+ return 1
117+ },
118+ "ru-RU" : func (cnt int64 ) int {
119+ if cnt % 10 == 1 && cnt % 100 != 11 {
120+ return 0
121+ }
122+ return 1
123+ },
124+ "zh-CN" : func (cnt int64 ) int {
125+ return 0
126+ },
127+ "zh-HK" : func (cnt int64 ) int {
128+ return 0
129+ },
130+ "zh-TW" : func (cnt int64 ) int {
131+ return 0
132+ },
133+ "fr-FR" : func (cnt int64 ) int {
134+ if cnt > - 2 && cnt < 2 {
135+ return 0
136+ }
137+ return 1
138+ },
139+ }
140+
141+ // TrN returns translated message for plural text translation
142+ func (l * locale ) TrN (cnt interface {}, key1 , keyN string , args ... interface {}) string {
143+ var c int64
144+ if t , ok := cnt .(int ); ok {
145+ c = int64 (t )
146+ } else if t , ok := cnt .(int16 ); ok {
147+ c = int64 (t )
148+ } else if t , ok := cnt .(int32 ); ok {
149+ c = int64 (t )
150+ } else if t , ok := cnt .(int64 ); ok {
151+ c = t
152+ } else {
153+ return l .Tr (keyN , args ... )
154+ }
155+
156+ ruleFunc , ok := trNLangRules [l .Lang ]
157+ if ! ok {
158+ ruleFunc = trNLangRules ["en-US" ]
159+ }
160+
161+ if ruleFunc (c ) == 0 {
162+ return l .Tr (key1 , args ... )
163+ }
164+ return l .Tr (keyN , args ... )
165+ }
0 commit comments