import_export.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package ut
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "github.com/go-playground/locales"
  9. )
  10. type translation struct {
  11. Locale string `json:"locale"`
  12. Key interface{} `json:"key"`
  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. b, err := ioutil.ReadFile(filename)
  113. if err != nil {
  114. return err
  115. }
  116. var trans []translation
  117. switch format {
  118. case JSON:
  119. err = json.Unmarshal(b, &trans)
  120. }
  121. if err != nil {
  122. return err
  123. }
  124. for _, tl := range trans {
  125. locale, found := t.FindTranslator(tl.Locale)
  126. if !found {
  127. return fmt.Errorf("locale '%s' not registered for translation in file '%s'", tl.Locale, filename)
  128. }
  129. pr := stringToPR(tl.PluralRule)
  130. if pr == locales.PluralRuleUnknown {
  131. return locale.Add(tl.Key, tl.Translation, tl.OverrideExisting)
  132. }
  133. switch tl.PluralType {
  134. case cardinalType:
  135. return locale.AddCardinal(tl.Key, tl.Translation, pr, tl.OverrideExisting)
  136. case ordinalType:
  137. return locale.AddOrdinal(tl.Key, tl.Translation, pr, tl.OverrideExisting)
  138. case rangeType:
  139. return locale.AddRange(tl.Key, tl.Translation, pr, tl.OverrideExisting)
  140. default:
  141. return fmt.Errorf("bad plural rule '%#v', incorrect plural information found for translation in file '%s'", tl, filename)
  142. }
  143. }
  144. return nil
  145. }
  146. if !fi.IsDir() {
  147. return processFn(dirnameOrFilename)
  148. }
  149. // recursively go through directory
  150. walker := func(path string, info os.FileInfo, err error) error {
  151. if info.IsDir() {
  152. return nil
  153. }
  154. return processFn(dirnameOrFilename)
  155. }
  156. return filepath.Walk(dirnameOrFilename, walker)
  157. }
  158. func stringToPR(s string) locales.PluralRule {
  159. switch s {
  160. case "One":
  161. return locales.PluralRuleOne
  162. case "Two":
  163. return locales.PluralRuleTwo
  164. case "Few":
  165. return locales.PluralRuleFew
  166. case "Many":
  167. return locales.PluralRuleMany
  168. case "Other":
  169. return locales.PluralRuleOther
  170. default:
  171. return locales.PluralRuleUnknown
  172. }
  173. }