translator.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package ut
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/go-playground/locales"
  7. )
  8. const (
  9. paramZero = "{0}"
  10. paramOne = "{1}"
  11. unknownTranslation = ""
  12. )
  13. // Translator is universal translators
  14. // translator instance which is a thin wrapper
  15. // around locales.Translator instance providing
  16. // some extra functionality
  17. type Translator interface {
  18. locales.Translator
  19. // adds a normal translation for a particular language/locale
  20. // {#} is the only replacement type accepted and are add infintium
  21. // eg. one: '{0} day left' other: '{0} days left'
  22. Add(key interface{}, text string) error
  23. // is the same as Add only it allows existing translations to be overridden
  24. Overwrite(key interface{}, text string) error
  25. // adds a cardinal plural translation for a particular language/locale
  26. // {0} is the only replacement type accepted and only one variable is accepted as
  27. // multiple cannot be used for a plural rule determination, unless it is a range;
  28. // see AddRange below.
  29. // eg. in locale 'en' one: '{0} day left' other: '{0} days left'
  30. AddCardinal(key interface{}, text string, rule locales.PluralRule) error
  31. // is the same as AddCardinal only it allows existing translations to be overridden
  32. OverwriteCardinal(key interface{}, text string, rule locales.PluralRule) error
  33. // adds an ordinal plural translation for a particular language/locale
  34. // {0} is the only replacement type accepted and only one variable is accepted as
  35. // multiple cannot be used for a plural rule determination, unless it is a range;
  36. // see AddRange below.
  37. // eg. in locale 'en' one: '{0}st day of spring' other: '{0}nd day of spring'
  38. // - 1st, 2nd, 3rd...
  39. AddOrdinal(key interface{}, text string, rule locales.PluralRule) error
  40. // is the same as AddOrdinal only it allows for existing translations to be overridden
  41. OverwriteOrdinal(key interface{}, text string, rule locales.PluralRule) error
  42. // adds a range plural translation for a particular language/locale
  43. // {0} and {1} are the only replacement types accepted and only these are accepted.
  44. // eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
  45. AddRange(key interface{}, text string, rule locales.PluralRule) error
  46. // is the same as AddRange only allows an existing translation to be overridden
  47. OverwriteRange(key interface{}, text string, rule locales.PluralRule) error
  48. // creates the translation for the locale given the 'key' and params passed in
  49. T(key interface{}, params ...string) string
  50. // creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments
  51. // and param passed in
  52. C(key interface{}, num float64, digits uint64, param string) (string, error)
  53. // creates the ordinal translation for the locale given the 'key', 'num' and 'digit' arguments
  54. // and param passed in
  55. O(key interface{}, num float64, digits uint64, param string) (string, error)
  56. // creates the range translation for the locale given the 'key', 'num1', 'digit1', 'num2' and
  57. // 'digit2' arguments and 'param1' and 'param2' passed in
  58. R(key interface{}, num1 float64, digits1 uint64, num2 float64, digits2 uint64, param1, param2 string) (string, error)
  59. // VerifyTranslations checks to ensures that no plural rules have been
  60. // missed within the translations.
  61. VerifyTranslations() error
  62. }
  63. var _ Translator = new(translator)
  64. var _ locales.Translator = new(translator)
  65. type translator struct {
  66. locales.Translator
  67. translations map[interface{}]*transText
  68. cardinalTanslations map[interface{}][]*transText // array index is mapped to locales.PluralRule index + the locales.PluralRuleUnknown
  69. ordinalTanslations map[interface{}][]*transText
  70. rangeTanslations map[interface{}][]*transText
  71. }
  72. type transText struct {
  73. text string
  74. indexes []int
  75. }
  76. func newTranslator(trans locales.Translator) Translator {
  77. return &translator{
  78. Translator: trans,
  79. translations: make(map[interface{}]*transText), // translation text broken up by byte index
  80. cardinalTanslations: make(map[interface{}][]*transText),
  81. ordinalTanslations: make(map[interface{}][]*transText),
  82. rangeTanslations: make(map[interface{}][]*transText),
  83. }
  84. }
  85. // Add adds a normal translation for a particular language/locale
  86. // {#} is the only replacement type accepted and are add infintium
  87. // eg. one: '{0} day left' other: '{0} days left'
  88. func (t *translator) Add(key interface{}, text string) error {
  89. return t.add(key, text, false)
  90. }
  91. // Overwrite is the same as Add only it allows existing translations to be overridden
  92. func (t *translator) Overwrite(key interface{}, text string) error {
  93. return t.add(key, text, true)
  94. }
  95. func (t *translator) add(key interface{}, text string, overwrite bool) error {
  96. if _, ok := t.translations[key]; ok && !overwrite {
  97. return &ErrConflictingTranslation{key: key, text: text}
  98. }
  99. trans := &transText{
  100. text: text,
  101. }
  102. var i int
  103. var idx int
  104. var cum int
  105. for {
  106. s := "{" + strconv.Itoa(i) + "}"
  107. idx = strings.Index(text[idx:], s)
  108. if idx == -1 {
  109. break
  110. }
  111. trans.indexes = append(trans.indexes, idx+cum)
  112. idx += len(s)
  113. trans.indexes = append(trans.indexes, idx+cum)
  114. cum += idx
  115. i++
  116. }
  117. t.translations[key] = trans
  118. return nil
  119. }
  120. // AddCardinal adds a cardinal plural translation for a particular language/locale
  121. // {0} is the only replacement type accepted and only one variable is accepted as
  122. // multiple cannot be used for a plural rule determination, unless it is a range;
  123. // see AddRange below.
  124. // eg. in locale 'en' one: '{0} day left' other: '{0} days left'
  125. func (t *translator) AddCardinal(key interface{}, text string, rule locales.PluralRule) error {
  126. return t.addCardinal(key, text, rule, false)
  127. }
  128. // OverwriteCardinal is the same as AddCardinal only it allows existing translations to be overridden
  129. func (t *translator) OverwriteCardinal(key interface{}, text string, rule locales.PluralRule) error {
  130. return t.addCardinal(key, text, rule, true)
  131. }
  132. func (t *translator) addCardinal(key interface{}, text string, rule locales.PluralRule, overwrite bool) error {
  133. tarr, ok := t.cardinalTanslations[key]
  134. if ok {
  135. // verify not adding a conflicting record
  136. if len(tarr) > 0 && tarr[rule] != nil && !overwrite {
  137. return &ErrConflictingTranslation{key: key, rule: rule, text: text}
  138. }
  139. } else {
  140. tarr = make([]*transText, 7, 7)
  141. t.cardinalTanslations[key] = tarr
  142. }
  143. trans := &transText{
  144. text: text,
  145. indexes: make([]int, 2, 2),
  146. }
  147. tarr[rule] = trans
  148. idx := strings.Index(text, paramZero)
  149. if idx == -1 {
  150. tarr[rule] = nil
  151. return &ErrCardinalTranslation{text: fmt.Sprintf("error: parameter '%s' not found, may want to use 'Add' instead of 'AddCardinal'", paramZero)}
  152. }
  153. trans.indexes[0] = idx
  154. trans.indexes[1] = idx + len(paramZero)
  155. return nil
  156. }
  157. // AddOrdinal adds an ordinal plural translation for a particular language/locale
  158. // {0} is the only replacement type accepted and only one variable is accepted as
  159. // multiple cannot be used for a plural rule determination, unless it is a range;
  160. // see AddRange below.
  161. // eg. in locale 'en' one: '{0}st day of spring' other: '{0}nd day of spring' - 1st, 2nd, 3rd...
  162. func (t *translator) AddOrdinal(key interface{}, text string, rule locales.PluralRule) error {
  163. return t.addOrdinal(key, text, rule, false)
  164. }
  165. // OverwriteOrdinal is the same as AddOrdinal only it allows for existing translations to be overridden
  166. func (t *translator) OverwriteOrdinal(key interface{}, text string, rule locales.PluralRule) error {
  167. return t.addOrdinal(key, text, rule, true)
  168. }
  169. func (t *translator) addOrdinal(key interface{}, text string, rule locales.PluralRule, overwrite bool) error {
  170. tarr, ok := t.ordinalTanslations[key]
  171. if ok {
  172. // verify not adding a conflicting record
  173. if len(tarr) > 0 && tarr[rule] != nil && !overwrite {
  174. return &ErrConflictingTranslation{key: key, rule: rule, text: text}
  175. }
  176. } else {
  177. tarr = make([]*transText, 7, 7)
  178. t.ordinalTanslations[key] = tarr
  179. }
  180. trans := &transText{
  181. text: text,
  182. indexes: make([]int, 2, 2),
  183. }
  184. tarr[rule] = trans
  185. idx := strings.Index(text, paramZero)
  186. if idx == -1 {
  187. tarr[rule] = nil
  188. return &ErrOrdinalTranslation{text: fmt.Sprintf("error: parameter '%s' not found, may want to use 'Add' instead of 'AddOrdinal'", paramZero)}
  189. }
  190. trans.indexes[0] = idx
  191. trans.indexes[1] = idx + len(paramZero)
  192. return nil
  193. }
  194. // AddRange adds a range plural translation for a particular language/locale
  195. // {0} and {1} are the only replacement types accepted and only these are accepted.
  196. // eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
  197. func (t *translator) AddRange(key interface{}, text string, rule locales.PluralRule) error {
  198. return t.addRange(key, text, rule, false)
  199. }
  200. // OverwriteRange is the same as AddRange only allows an existing translation to be overridden
  201. func (t *translator) OverwriteRange(key interface{}, text string, rule locales.PluralRule) error {
  202. return t.addRange(key, text, rule, true)
  203. }
  204. func (t *translator) addRange(key interface{}, text string, rule locales.PluralRule, overwrite bool) error {
  205. tarr, ok := t.rangeTanslations[key]
  206. if ok {
  207. // verify not adding a conflicting record
  208. if len(tarr) > 0 && tarr[rule] != nil && !overwrite {
  209. return &ErrConflictingTranslation{key: key, rule: rule, text: text}
  210. }
  211. } else {
  212. tarr = make([]*transText, 7, 7)
  213. t.rangeTanslations[key] = tarr
  214. }
  215. trans := &transText{
  216. text: text,
  217. indexes: make([]int, 4, 4),
  218. }
  219. tarr[rule] = trans
  220. idx := strings.Index(text, paramZero)
  221. if idx == -1 {
  222. tarr[rule] = nil
  223. return &ErrRangeTranslation{text: fmt.Sprintf("error: parameter '%s' not found, are you sure you're adding a Range Translation?", paramZero)}
  224. }
  225. trans.indexes[0] = idx
  226. trans.indexes[1] = idx + len(paramZero)
  227. idx = strings.Index(text, paramOne)
  228. if idx == -1 {
  229. tarr[rule] = nil
  230. return &ErrRangeTranslation{text: fmt.Sprintf("error: parameter '%s' not found, a Range Translation requires two parameters", paramOne)}
  231. }
  232. trans.indexes[2] = idx
  233. trans.indexes[3] = idx + len(paramOne)
  234. return nil
  235. }
  236. // T creates the translation for the locale given the 'key' and params passed in
  237. func (t *translator) T(key interface{}, params ...string) string {
  238. trans, ok := t.translations[key]
  239. if !ok {
  240. return unknownTranslation
  241. }
  242. b := make([]byte, 0, 64)
  243. var start, end, count int
  244. for i := 0; i < len(trans.indexes); i++ {
  245. end = trans.indexes[i]
  246. b = append(b, trans.text[start:end]...)
  247. b = append(b, params[count]...)
  248. i++
  249. start = trans.indexes[i]
  250. count++
  251. }
  252. b = append(b, trans.text[start:]...)
  253. return string(b)
  254. }
  255. // C creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments and param passed in
  256. func (t *translator) C(key interface{}, num float64, digits uint64, param string) (string, error) {
  257. tarr, ok := t.cardinalTanslations[key]
  258. if !ok {
  259. return unknownTranslation, ErrUnknowTranslation
  260. }
  261. rule := t.CardinalPluralRule(num, digits)
  262. trans := tarr[rule]
  263. b := make([]byte, 0, 64)
  264. b = append(b, trans.text[:trans.indexes[0]]...)
  265. b = append(b, param...)
  266. b = append(b, trans.text[trans.indexes[1]:]...)
  267. return string(b), nil
  268. }
  269. // O creates the ordinal translation for the locale given the 'key', 'num' and 'digit' arguments and param passed in
  270. func (t *translator) O(key interface{}, num float64, digits uint64, param string) (string, error) {
  271. tarr, ok := t.ordinalTanslations[key]
  272. if !ok {
  273. return unknownTranslation, ErrUnknowTranslation
  274. }
  275. rule := t.OrdinalPluralRule(num, digits)
  276. trans := tarr[rule]
  277. b := make([]byte, 0, 64)
  278. b = append(b, trans.text[:trans.indexes[0]]...)
  279. b = append(b, param...)
  280. b = append(b, trans.text[trans.indexes[1]:]...)
  281. return string(b), nil
  282. }
  283. // R creates the range translation for the locale given the 'key', 'num1', 'digit1', 'num2' and 'digit2' arguments
  284. // and 'param1' and 'param2' passed in
  285. func (t *translator) R(key interface{}, num1 float64, digits1 uint64, num2 float64, digits2 uint64, param1, param2 string) (string, error) {
  286. tarr, ok := t.rangeTanslations[key]
  287. if !ok {
  288. return unknownTranslation, ErrUnknowTranslation
  289. }
  290. rule := t.RangePluralRule(num1, digits1, num2, digits2)
  291. trans := tarr[rule]
  292. b := make([]byte, 0, 64)
  293. b = append(b, trans.text[:trans.indexes[0]]...)
  294. b = append(b, param1...)
  295. b = append(b, trans.text[trans.indexes[1]:trans.indexes[2]]...)
  296. b = append(b, param2...)
  297. b = append(b, trans.text[trans.indexes[3]:]...)
  298. return string(b), nil
  299. }
  300. // VerifyTranslations checks to ensures that no plural rules have been
  301. // missed within the translations.
  302. func (t *translator) VerifyTranslations() error {
  303. for k, v := range t.cardinalTanslations {
  304. for _, rule := range t.PluralsCardinal() {
  305. if v[rule] == nil {
  306. return &ErrMissingPluralTranslation{translationType: "plural", rule: rule, key: k}
  307. }
  308. }
  309. }
  310. for k, v := range t.ordinalTanslations {
  311. for _, rule := range t.PluralsOrdinal() {
  312. if v[rule] == nil {
  313. return &ErrMissingPluralTranslation{translationType: "ordinal", rule: rule, key: k}
  314. }
  315. }
  316. }
  317. for k, v := range t.rangeTanslations {
  318. for _, rule := range t.PluralsRange() {
  319. if v[rule] == nil {
  320. return &ErrMissingPluralTranslation{translationType: "range", rule: rule, key: k}
  321. }
  322. }
  323. }
  324. return nil
  325. }