header.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mo
  5. import (
  6. "bytes"
  7. "fmt"
  8. "strings"
  9. )
  10. // Header is the initial comments "SOME DESCRIPTIVE TITLE", "YEAR"
  11. // and "FIRST AUTHOR <EMAIL@ADDRESS>, YEAR" ought to be replaced by sensible information.
  12. //
  13. // See http://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html#Header-Entry
  14. type Header struct {
  15. ProjectIdVersion string // Project-Id-Version: PACKAGE VERSION
  16. ReportMsgidBugsTo string // Report-Msgid-Bugs-To: FIRST AUTHOR <EMAIL@ADDRESS>
  17. POTCreationDate string // POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE
  18. PORevisionDate string // PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
  19. LastTranslator string // Last-Translator: FIRST AUTHOR <EMAIL@ADDRESS>
  20. LanguageTeam string // Language-Team: golang-china
  21. Language string // Language: zh_CN
  22. MimeVersion string // MIME-Version: 1.0
  23. ContentType string // Content-Type: text/plain; charset=UTF-8
  24. ContentTransferEncoding string // Content-Transfer-Encoding: 8bit
  25. PluralForms string // Plural-Forms: nplurals=2; plural=n == 1 ? 0 : 1;
  26. XGenerator string // X-Generator: Poedit 1.5.5
  27. UnknowFields map[string]string
  28. }
  29. func (p *Header) fromMessage(msg *Message) {
  30. if msg.MsgId != "" || msg.MsgStr == "" {
  31. return
  32. }
  33. lines := strings.Split(msg.MsgStr, "\n")
  34. for i := 0; i < len(lines); i++ {
  35. idx := strings.Index(lines[i], ":")
  36. if idx < 0 {
  37. continue
  38. }
  39. key := strings.TrimSpace(lines[i][:idx])
  40. val := strings.TrimSpace(lines[i][idx+1:])
  41. switch strings.ToUpper(key) {
  42. case strings.ToUpper("Project-Id-Version"):
  43. p.ProjectIdVersion = val
  44. case strings.ToUpper("Report-Msgid-Bugs-To"):
  45. p.ReportMsgidBugsTo = val
  46. case strings.ToUpper("POT-Creation-Date"):
  47. p.POTCreationDate = val
  48. case strings.ToUpper("PO-Revision-Date"):
  49. p.PORevisionDate = val
  50. case strings.ToUpper("Last-Translator"):
  51. p.LastTranslator = val
  52. case strings.ToUpper("Language-Team"):
  53. p.LanguageTeam = val
  54. case strings.ToUpper("Language"):
  55. p.Language = val
  56. case strings.ToUpper("MIME-Version"):
  57. p.MimeVersion = val
  58. case strings.ToUpper("Content-Type"):
  59. p.ContentType = val
  60. case strings.ToUpper("Content-Transfer-Encoding"):
  61. p.ContentTransferEncoding = val
  62. case strings.ToUpper("Plural-Forms"):
  63. p.PluralForms = val
  64. case strings.ToUpper("X-Generator"):
  65. p.XGenerator = val
  66. default:
  67. if p.UnknowFields == nil {
  68. p.UnknowFields = make(map[string]string)
  69. }
  70. p.UnknowFields[key] = val
  71. }
  72. }
  73. }
  74. func (p *Header) toMessage() Message {
  75. return Message{
  76. MsgStr: p.String(),
  77. }
  78. }
  79. // String returns the po format header string.
  80. func (p Header) String() string {
  81. var buf bytes.Buffer
  82. fmt.Fprintf(&buf, `msgid ""`+"\n")
  83. fmt.Fprintf(&buf, `msgstr ""`+"\n")
  84. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Project-Id-Version", p.ProjectIdVersion)
  85. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Report-Msgid-Bugs-To", p.ReportMsgidBugsTo)
  86. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "POT-Creation-Date", p.POTCreationDate)
  87. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "PO-Revision-Date", p.PORevisionDate)
  88. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Last-Translator", p.LastTranslator)
  89. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Language-Team", p.LanguageTeam)
  90. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Language", p.Language)
  91. if p.MimeVersion != "" {
  92. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "MIME-Version", p.MimeVersion)
  93. }
  94. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Content-Type", p.ContentType)
  95. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "Content-Transfer-Encoding", p.ContentTransferEncoding)
  96. if p.XGenerator != "" {
  97. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", "X-Generator", p.XGenerator)
  98. }
  99. for k, v := range p.UnknowFields {
  100. fmt.Fprintf(&buf, `"%s: %s\n"`+"\n", k, v)
  101. }
  102. return buf.String()
  103. }