...
1
2
3 package main
4
5 import (
6 "golang.org/x/text/language"
7 "golang.org/x/text/message"
8 "golang.org/x/text/message/catalog"
9 )
10
11 type dictionary struct {
12 index []uint32
13 data string
14 }
15
16 func (d *dictionary) Lookup(key string) (data string, ok bool) {
17 p, ok := messageKeyToIndex[key]
18 if !ok {
19 return "", false
20 }
21 start, end := d.index[p], d.index[p+1]
22 if start == end {
23 return "", false
24 }
25 return d.data[start:end], true
26 }
27
28 func init() {
29 dict := map[string]catalog.Dictionary{
30 "en": &dictionary{index: enIndex, data: enData},
31 "zh": &dictionary{index: zhIndex, data: zhData},
32 }
33 fallback := language.MustParse("en")
34 cat, err := catalog.NewFromMap(dict, catalog.Fallback(fallback))
35 if err != nil {
36 panic(err)
37 }
38 message.DefaultCatalog = cat
39 }
40
41 var messageKeyToIndex = map[string]int{
42 "Do you like your browser (%s)?\n": 1,
43 "Hello %s!\n": 0,
44 }
45
46 var enIndex = []uint32{
47 0x00000000, 0x00000012, 0x00000039,
48 }
49
50 const enData string = "" +
51 "\x04\x00\x01\x0a\x0d\x02Hello %[1]s!\x04\x00\x01\x0a\x22\x02Do you like " +
52 "your browser (%[1]s)?"
53
54 var zhIndex = []uint32{
55 0x00000000, 0x00000000, 0x00000000,
56 }
57
58 const zhData string = ""
59
60
61
View as plain text