1- *赞助 BTC: 1Cbd6oGAUUyBi7X7MaR4np4nTmQZXVgkCW*
2- *赞助 ETH: 0x623A3C3a72186A6336C79b18Ac1eD36e1c71A8a6*
3- *Go语言付费QQ群: 1055927514*
4
5----
6
7# gettext-go: GNU gettext for Go ([Imported By Kubernetes](https://pkg.go.dev/github.com/chai2010/gettext-go@v0.1.0/gettext?tab=importedby))
8
9- PkgDoc: [http://godoc.org/github.com/chai2010/gettext-go](http://godoc.org/github.com/chai2010/gettext-go)
10- PkgDoc: [http://pkg.go.dev/github.com/chai2010/gettext-go](http://pkg.go.dev/github.com/chai2010/gettext-go)
11
12## Install
13
141. `go get github.com/chai2010/gettext-go`
152. `go run hello.go`
16
17The godoc.org or go.dev has more information.
18
19## Examples
20
21```Go
22package main
23
24import (
25 "fmt"
26
27 "github.com/chai2010/gettext-go"
28)
29
30func main() {
31 gettext := gettext.New("hello", "./examples/locale").SetLanguage("zh_CN")
32 fmt.Println(gettext.Gettext("Hello, world!"))
33
34 // Output: 你好, 世界!
35}
36```
37
38```Go
39package main
40
41import (
42 "fmt"
43
44 "github.com/chai2010/gettext-go"
45)
46
47func main() {
48 gettext.SetLanguage("zh_CN")
49 gettext.BindLocale(gettext.New("hello", "locale"))
50
51 // gettext.BindLocale("hello", "locale") // from locale dir
52 // gettext.BindLocale("hello", "locale.zip") // from locale zip file
53 // gettext.BindLocale("hello", "locale.zip", zipData) // from embedded zip data
54
55 // translate source text
56 fmt.Println(gettext.Gettext("Hello, world!"))
57 // Output: 你好, 世界!
58
59 // if no msgctxt in PO file (only msgid and msgstr),
60 // specify context as "" by
61 fmt.Println(gettext.PGettext("", "Hello, world!"))
62 // Output: 你好, 世界!
63
64 // translate resource
65 fmt.Println(string(gettext.Getdata("poems.txt"))))
66 // Output: ...
67}
68```
69
70Go file: [hello.go](https://github.com/chai2010/gettext-go/blob/master/examples/hello.go); PO file: [hello.po](https://github.com/chai2010/gettext-go/blob/master/examples/locale/default/LC_MESSAGES/hello.po);
71
72----
73
74## API Changes (v0.1.0 vs v1.0.0)
75
76### Renamed package path
77
78| v0.1.0 (old) | v1.0.0 (new) |
79| ----------------------------------------------- | --------------------------------------- |
80| `github.com/chai2010/gettext-go/gettext` | `github.com/chai2010/gettext-go` |
81| `github.com/chai2010/gettext-go/gettext/po` | `github.com/chai2010/gettext-go/po` |
82| `github.com/chai2010/gettext-go/gettext/mo` | `github.com/chai2010/gettext-go/mo` |
83| `github.com/chai2010/gettext-go/gettext/plural` | `github.com/chai2010/gettext-go/plural` |
84
85### Renamed functions
86
87| v0.1.0 (old) | v1.0.0 (new) |
88| ---------------------------------- | --------------------------- |
89| `gettext-go/gettext.*` | `gettext-go.*` |
90| `gettext-go/gettext.DefaultLocal` | `gettext-go.DefaultLanguage`|
91| `gettext-go/gettext.BindTextdomain`| `gettext-go.BindLocale` |
92| `gettext-go/gettext.Textdomain` | `gettext-go.SetDomain` |
93| `gettext-go/gettext.SetLocale` | `gettext-go.SetLanguage` |
94| `gettext-go/gettext/po.Load` | `gettext-go/po.LoadFile` |
95| `gettext-go/gettext/po.LoadData` | `gettext-go/po.Load` |
96| `gettext-go/gettext/mo.Load` | `gettext-go/mo.LoadFile` |
97| `gettext-go/gettext/mo.LoadData` | `gettext-go/mo.Load` |
98
99### Use empty string as the default context for `gettext.Gettext`
100
101```go
102package main
103
104// v0.1.0
105// if the **context** missing, use `callerName(2)` as the context:
106
107// v1.0.0
108// if the **context** missing, use empty string as the context:
109
110func main() {
111 gettext.Gettext("hello")
112 // v0.1.0 => gettext.PGettext("main.main", "hello")
113 // v1.0.0 => gettext.PGettext("", "hello")
114
115 gettext.DGettext("domain", "hello")
116 // v0.1.0 => gettext.DPGettext("domain", "main.main", "hello")
117 // v1.0.0 => gettext.DPGettext("domain", "", "hello")
118
119 gettext.NGettext("domain", "hello", "hello2", n)
120 // v0.1.0 => gettext.PNGettext("domain", "main.main", "hello", "hello2", n)
121 // v1.0.0 => gettext.PNGettext("domain", "", "hello", "hello2", n)
122
123 gettext.DNGettext("domain", "hello", "hello2", n)
124 // v0.1.0 => gettext.DPNGettext("domain", "main.main", "hello", "hello2", n)
125 // v1.0.0 => gettext.DPNGettext("domain", "", "hello", "hello2", n)
126}
127```
128
129### `BindLocale` support `FileSystem` interface
130
131```go
132// Use FileSystem:
133// BindLocale(New("poedit", "name", OS("path/to/dir"))) // bind "poedit" domain
134// BindLocale(New("poedit", "name", OS("path/to.zip"))) // bind "poedit" domain
135```
136
137## New API in v1.0.0
138
139`Gettexter` interface:
140
141```go
142type Gettexter interface {
143 FileSystem() FileSystem
144
145 GetDomain() string
146 SetDomain(domain string) Gettexter
147
148 GetLanguage() string
149 SetLanguage(lang string) Gettexter
150
151 Gettext(msgid string) string
152 PGettext(msgctxt, msgid string) string
153
154 NGettext(msgid, msgidPlural string, n int) string
155 PNGettext(msgctxt, msgid, msgidPlural string, n int) string
156
157 DGettext(domain, msgid string) string
158 DPGettext(domain, msgctxt, msgid string) string
159 DNGettext(domain, msgid, msgidPlural string, n int) string
160 DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string
161
162 Getdata(name string) []byte
163 DGetdata(domain, name string) []byte
164}
165
166func New(domain, path string, data ...interface{}) Gettexter
167```
168
169`FileSystem` interface:
170
171```go
172type FileSystem interface {
173 LocaleList() []string
174 LoadMessagesFile(domain, lang, ext string) ([]byte, error)
175 LoadResourceFile(domain, lang, name string) ([]byte, error)
176 String() string
177}
178
179func NewFS(name string, x interface{}) FileSystem
180func OS(root string) FileSystem
181func ZipFS(r *zip.Reader, name string) FileSystem
182func NilFS(name string) FileSystem
183```
184
185----
186
187## BUGS
188
189Please report bugs to <chaishushan@gmail.com>.
190
191Thanks!
View as plain text