@@ -24,7 +24,10 @@ type Translator interface {
2424 // adds a normal translation for a particular language/locale
2525 // {#} is the only replacement type accepted and are add infintium
2626 // eg. one: '{0} day left' other: '{0} days left'
27- Add (key interface {}, text string )
27+ Add (key interface {}, text string ) error
28+
29+ // is the same as Add only it allows existing translations to be overridden
30+ Overwrite (key interface {}, text string ) error
2831
2932 // adds a cardinal plural translation for a particular language/locale
3033 // {0} is the only replacement type accepted and only one variable is accepted as
@@ -33,6 +36,9 @@ type Translator interface {
3336 // eg. in locale 'en' one: '{0} day left' other: '{0} days left'
3437 AddCardinal (key interface {}, text string , rule locales.PluralRule ) error
3538
39+ // is the same as AddCardinal only it allows existing translations to be overridden
40+ OverwriteCardinal (key interface {}, text string , rule locales.PluralRule ) error
41+
3642 // adds an ordinal plural translation for a particular language/locale
3743 // {0} is the only replacement type accepted and only one variable is accepted as
3844 // multiple cannot be used for a plural rule determination, unless it is a range;
@@ -41,11 +47,17 @@ type Translator interface {
4147 // - 1st, 2nd, 3rd...
4248 AddOrdinal (key interface {}, text string , rule locales.PluralRule ) error
4349
50+ // is the same as AddOrdinal only it allows for existing translations to be overridden
51+ OverwriteOrdinal (key interface {}, text string , rule locales.PluralRule ) error
52+
4453 // adds a range plural translation for a particular language/locale
4554 // {0} and {1} are the only replacement types accepted and only these are accepted.
4655 // eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
4756 AddRange (key interface {}, text string , rule locales.PluralRule ) error
4857
58+ // is the same as AddRange only allows an existing translation to be overridden
59+ OverwriteRange (key interface {}, text string , rule locales.PluralRule ) error
60+
4961 // creates the translation for the locale given the 'key' and params passed in
5062 T (key interface {}, params ... string ) string
5163
@@ -95,7 +107,20 @@ func newTranslator(trans locales.Translator) Translator {
95107// Add adds a normal translation for a particular language/locale
96108// {#} is the only replacement type accepted and are add infintium
97109// eg. one: '{0} day left' other: '{0} days left'
98- func (t * translator ) Add (key interface {}, text string ) {
110+ func (t * translator ) Add (key interface {}, text string ) error {
111+ return t .add (key , text , false )
112+ }
113+
114+ // Overwrite is the same as Add only it allows existing translations to be overridden
115+ func (t * translator ) Overwrite (key interface {}, text string ) error {
116+ return t .add (key , text , true )
117+ }
118+
119+ func (t * translator ) add (key interface {}, text string , overwrite bool ) error {
120+
121+ if _ , ok := t .translations [key ]; ok && ! overwrite {
122+ return & ErrConflictingTranslation {key : key , text : text }
123+ }
99124
100125 trans := & transText {
101126 text : text ,
@@ -120,6 +145,8 @@ func (t *translator) Add(key interface{}, text string) {
120145 }
121146
122147 t .translations [key ] = trans
148+
149+ return nil
123150}
124151
125152// AddCardinal adds a cardinal plural translation for a particular language/locale
@@ -128,17 +155,25 @@ func (t *translator) Add(key interface{}, text string) {
128155// see AddRange below.
129156// eg. in locale 'en' one: '{0} day left' other: '{0} days left'
130157func (t * translator ) AddCardinal (key interface {}, text string , rule locales.PluralRule ) error {
158+ return t .addCardinal (key , text , rule , false )
159+ }
160+
161+ // OverwriteCardinal is the same as AddCardinal only it allows existing translations to be overridden
162+ func (t * translator ) OverwriteCardinal (key interface {}, text string , rule locales.PluralRule ) error {
163+ return t .addCardinal (key , text , rule , true )
164+ }
165+
166+ func (t * translator ) addCardinal (key interface {}, text string , rule locales.PluralRule , overwrite bool ) error {
131167
132168 tarr , ok := t .cardinalTanslations [key ]
133169 if ok {
134170 // verify not adding a conflicting record
135- if len (tarr ) > 0 && tarr [rule ] != nil {
171+ if len (tarr ) > 0 && tarr [rule ] != nil && ! overwrite {
136172 return & ErrConflictingTranslation {key : key , rule : rule , text : text }
137173 }
138174
139175 } else {
140176 tarr = make ([]* transText , 7 , 7 )
141- // tarr = make([]*transText, len(t.PluralsCardinal())+1, len(t.PluralsCardinal())+1)
142177 t .cardinalTanslations [key ] = tarr
143178 }
144179
@@ -167,11 +202,20 @@ func (t *translator) AddCardinal(key interface{}, text string, rule locales.Plur
167202// see AddRange below.
168203// eg. in locale 'en' one: '{0}st day of spring' other: '{0}nd day of spring' - 1st, 2nd, 3rd...
169204func (t * translator ) AddOrdinal (key interface {}, text string , rule locales.PluralRule ) error {
205+ return t .addOrdinal (key , text , rule , false )
206+ }
207+
208+ // OverwriteOrdinal is the same as AddOrdinal only it allows for existing translations to be overridden
209+ func (t * translator ) OverwriteOrdinal (key interface {}, text string , rule locales.PluralRule ) error {
210+ return t .addOrdinal (key , text , rule , true )
211+ }
212+
213+ func (t * translator ) addOrdinal (key interface {}, text string , rule locales.PluralRule , overwrite bool ) error {
170214
171215 tarr , ok := t .ordinalTanslations [key ]
172216 if ok {
173217 // verify not adding a conflicting record
174- if len (tarr ) > 0 && tarr [rule ] != nil {
218+ if len (tarr ) > 0 && tarr [rule ] != nil && ! overwrite {
175219 return & ErrConflictingTranslation {key : key , rule : rule , text : text }
176220 }
177221
@@ -203,11 +247,20 @@ func (t *translator) AddOrdinal(key interface{}, text string, rule locales.Plura
203247// {0} and {1} are the only replacement types accepted and only these are accepted.
204248// eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
205249func (t * translator ) AddRange (key interface {}, text string , rule locales.PluralRule ) error {
250+ return t .addRange (key , text , rule , false )
251+ }
252+
253+ // OverwriteRange is the same as AddRange only allows an existing translation to be overridden
254+ func (t * translator ) OverwriteRange (key interface {}, text string , rule locales.PluralRule ) error {
255+ return t .addRange (key , text , rule , true )
256+ }
257+
258+ func (t * translator ) addRange (key interface {}, text string , rule locales.PluralRule , overwrite bool ) error {
206259
207260 tarr , ok := t .rangeTanslations [key ]
208261 if ok {
209262 // verify not adding a conflicting record
210- if len (tarr ) > 0 && tarr [rule ] != nil {
263+ if len (tarr ) > 0 && tarr [rule ] != nil && ! overwrite {
211264 return & ErrConflictingTranslation {key : key , rule : rule , text : text }
212265 }
213266
0 commit comments