translator.go 11 KB

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