...

Source file src/github.com/chai2010/gettext-go/mo/header.go

Documentation: github.com/chai2010/gettext-go/mo

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

View as plain text