...

Source file src/github.com/chai2010/gettext-go/tr_test.go

Documentation: github.com/chai2010/gettext-go

     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 gettext
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/chai2010/gettext-go/mo"
    11  	"github.com/chai2010/gettext-go/po"
    12  )
    13  
    14  func TestTranslator_Po(t *testing.T) {
    15  	tr, err := newPoTranslator("test", []byte(testTrPoData))
    16  	if err != nil {
    17  		t.Fatal(err)
    18  	}
    19  	for _, v := range testTrData {
    20  		if out := tr.PGettext(v.msgctxt, v.msgid); out != v.msgstr {
    21  			t.Fatalf("%s/%s: expect = %s, got = %s", v.msgctxt, v.msgid, v.msgstr, out)
    22  		}
    23  	}
    24  }
    25  
    26  func TestTranslator_Mo(t *testing.T) {
    27  	tr, err := newMoTranslator("test", poToMoData(t, []byte(testTrPoData)))
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	for _, v := range testTrData {
    32  		if out := tr.PGettext(v.msgctxt, v.msgid); out != v.msgstr {
    33  			t.Fatalf("%s/%s: expect = %s, got = %s", v.msgctxt, v.msgid, v.msgstr, out)
    34  		}
    35  		break
    36  	}
    37  }
    38  
    39  func poToMoData(t *testing.T, data []byte) []byte {
    40  	poFile, err := po.Load(data)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	moFile := &mo.File{
    45  		MimeHeader: mo.Header{
    46  			ProjectIdVersion:        poFile.MimeHeader.ProjectIdVersion,
    47  			ReportMsgidBugsTo:       poFile.MimeHeader.ReportMsgidBugsTo,
    48  			POTCreationDate:         poFile.MimeHeader.POTCreationDate,
    49  			PORevisionDate:          poFile.MimeHeader.PORevisionDate,
    50  			LastTranslator:          poFile.MimeHeader.LastTranslator,
    51  			LanguageTeam:            poFile.MimeHeader.LanguageTeam,
    52  			Language:                poFile.MimeHeader.Language,
    53  			MimeVersion:             poFile.MimeHeader.MimeVersion,
    54  			ContentType:             poFile.MimeHeader.ContentType,
    55  			ContentTransferEncoding: poFile.MimeHeader.ContentTransferEncoding,
    56  			PluralForms:             poFile.MimeHeader.PluralForms,
    57  			XGenerator:              poFile.MimeHeader.XGenerator,
    58  			UnknowFields:            poFile.MimeHeader.UnknowFields,
    59  		},
    60  	}
    61  	for _, v := range poFile.Messages {
    62  		moFile.Messages = append(moFile.Messages, mo.Message{
    63  			MsgContext:   v.MsgContext,
    64  			MsgId:        v.MsgId,
    65  			MsgIdPlural:  v.MsgIdPlural,
    66  			MsgStr:       v.MsgStr,
    67  			MsgStrPlural: v.MsgStrPlural,
    68  		})
    69  	}
    70  	return moFile.Data()
    71  }
    72  
    73  var testTrData = []struct {
    74  	msgctxt string
    75  	msgid   string
    76  	msgstr  string
    77  }{
    78  	{"main.init", "Gettext in init.", "Init函数中的Gettext."},
    79  	{"main.main", "Hello, world!", "你好, 世界!"},
    80  	{"main.func", "Gettext in func.", "闭包函数中的Gettext."},
    81  	{"code.google.com/p/gettext-go/examples/hi.SayHi", "pkg hi: Hello, world!", "来自\"Hi\"包的问候: 你好, 世界!"},
    82  }
    83  
    84  var testTrPoData = `
    85  msgctxt "main.init"
    86  msgid "Gettext in init."
    87  msgstr "Init函数中的Gettext."
    88  
    89  msgctxt "main.main"
    90  msgid "Hello, world!"
    91  msgstr "你好, 世界!"
    92  
    93  msgctxt "main.func"
    94  msgid "Gettext in func."
    95  msgstr "闭包函数中的Gettext."
    96  
    97  msgctxt "code.google.com/p/gettext-go/examples/hi.SayHi"
    98  msgid "pkg hi: Hello, world!"
    99  msgstr "来自\"Hi\"包的问候: 你好, 世界!"
   100  `
   101  

View as plain text