import_export.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package ut
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "io"
  8. "github.com/go-playground/locales"
  9. )
  10. type translation struct {
  11. Locale string `json:"locale"`
  12. Key interface{} `json:"key"` // either string or integer
  13. Translation string `json:"trans"`
  14. PluralType string `json:"type,omitempty"`
  15. PluralRule string `json:"rule,omitempty"`
  16. OverrideExisting bool `json:"override,omitempty"`
  17. }
  18. const (
  19. cardinalType = "Cardinal"
  20. ordinalType = "Ordinal"
  21. rangeType = "Range"
  22. )
  23. // ExportFormat is the format of the file import or export
  24. type ExportFormat uint8
  25. // supported Export Formats
  26. const (
  27. JSON ExportFormat = iota
  28. )
  29. // Export writes the translations out to a file on disk.
  30. //
  31. // NOTE: this currently only works with string or int translations keys.
  32. func (t *UniversalTranslator) Export(format ExportFormat, filename string) error {
  33. // build up translations
  34. var trans []translation
  35. for _, locale := range t.translators {
  36. for k, v := range locale.(*translator).translations {
  37. trans = append(trans, translation{
  38. Locale: locale.Locale(),
  39. Key: k,
  40. Translation: v.text,
  41. })
  42. }
  43. for k, pluralTrans := range locale.(*translator).cardinalTanslations {
  44. for i, plural := range pluralTrans {
  45. // leave enough for all plural rules
  46. // but not all are set for all languages.
  47. if plural == nil {
  48. continue
  49. }
  50. trans = append(trans, translation{
  51. Locale: locale.Locale(),
  52. Key: k.(string),
  53. Translation: plural.text,
  54. PluralType: cardinalType,
  55. PluralRule: locales.PluralRule(i).String(),
  56. })
  57. }
  58. }
  59. for k, pluralTrans := range locale.(*translator).ordinalTanslations {
  60. for i, plural := range pluralTrans {
  61. // leave enough for all plural rules
  62. // but not all are set for all languages.
  63. if plural == nil {
  64. continue
  65. }
  66. trans = append(trans, translation{
  67. Locale: locale.Locale(),
  68. Key: k.(string),
  69. Translation: plural.text,
  70. PluralType: ordinalType,
  71. PluralRule: locales.PluralRule(i).String(),
  72. })
  73. }
  74. }
  75. for k, pluralTrans := range locale.(*translator).rangeTanslations {
  76. for i, plural := range pluralTrans {
  77. // leave enough for all plural rules
  78. // but not all are set for all languages.
  79. if plural == nil {
  80. continue
  81. }
  82. trans = append(trans, translation{
  83. Locale: locale.Locale(),
  84. Key: k.(string),
  85. Translation: plural.text,
  86. PluralType: rangeType,
  87. PluralRule: locales.PluralRule(i).String(),
  88. })
  89. }
  90. }
  91. }
  92. var b []byte
  93. var err error
  94. switch format {
  95. case JSON:
  96. b, err = json.MarshalIndent(trans, "", " ")
  97. }
  98. if err != nil {
  99. return err
  100. }
  101. return ioutil.WriteFile(filename, b, 0644)
  102. }
  103. // Import reads the translations out of a file or directory on disk.
  104. //
  105. // NOTE: this currently only works with string or int translations keys.
  106. func (t *UniversalTranslator) Import(format ExportFormat, dirnameOrFilename string) error {
  107. fi, err := os.Stat(dirnameOrFilename)
  108. if err != nil {
  109. return err
  110. }
  111. processFn := func(filename string) error {
  112. f, err := os.Open(filename)
  113. if err != nil {
  114. return err
  115. }
  116. defer f.Close()
  117. return t.ImportByReader(format, f)
  118. }
  119. if !fi.IsDir() {
  120. return processFn(dirnameOrFilename)
  121. }
  122. // recursively go through directory
  123. walker := func(path string, info os.FileInfo, err error) error {
  124. if info.IsDir() {
  125. return nil
  126. }
  127. switch format {
  128. case JSON:
  129. // skip non JSON files
  130. if filepath.Ext(info.Name()) != ".json" {
  131. return nil
  132. }
  133. }
  134. return processFn(path)
  135. }
  136. return filepath.Walk(dirnameOrFilename, walker)
  137. }
  138. // ImportByReader imports the the translations found within the contents read from the supplied reader.
  139. //
  140. // NOTE: generally used when assets have been embedded into the binary and are already in memory.
  141. func (t *UniversalTranslator) ImportByReader(format ExportFormat, reader io.Reader) error {
  142. b, err := ioutil.ReadAll(reader)
  143. if err != nil {
  144. return err
  145. }
  146. var trans []translation
  147. switch format {
  148. case JSON:
  149. err = json.Unmarshal(b, &trans)
  150. }
  151. if err != nil {
  152. return err
  153. }
  154. for _, tl := range trans {
  155. locale, found := t.FindTranslator(tl.Locale)
  156. if !found {
  157. return &ErrMissingLocale{locale: tl.Locale}
  158. }
  159. pr := stringToPR(tl.PluralRule)
  160. if pr == locales.PluralRuleUnknown {
  161. err = locale.Add(tl.Key, tl.Translation, tl.OverrideExisting)
  162. if err != nil {
  163. return err
  164. }
  165. continue
  166. }
  167. switch tl.PluralType {
  168. case cardinalType:
  169. err = locale.AddCardinal(tl.Key, tl.Translation, pr, tl.OverrideExisting)
  170. case ordinalType:
  171. err = locale.AddOrdinal(tl.Key, tl.Translation, pr, tl.OverrideExisting)
  172. case rangeType:
  173. err = locale.AddRange(tl.Key, tl.Translation, pr, tl.OverrideExisting)
  174. default:
  175. return &ErrBadPluralDefinition{tl: tl}
  176. }
  177. if err != nil {
  178. return err
  179. }
  180. }
  181. return nil
  182. }
  183. func stringToPR(s string) locales.PluralRule {
  184. switch s {
  185. case "One":
  186. return locales.PluralRuleOne
  187. case "Two":
  188. return locales.PluralRuleTwo
  189. case "Few":
  190. return locales.PluralRuleFew
  191. case "Many":
  192. return locales.PluralRuleMany
  193. case "Other":
  194. return locales.PluralRuleOther
  195. default:
  196. return locales.PluralRuleUnknown
  197. }
  198. }