...

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

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

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

View as plain text