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