forked from gosexy/gettext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gettext.go
219 lines (175 loc) · 6.65 KB
/
gettext.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2012-2016 José Carlos Nieto, https://menteslibres.net/xiam
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Package gettext provides bindings for GNU Gettext.
package gettext
/*
// #cgo LDFLAGS: -lintl // Use this if: /usr/bin/ld: cannot find -lintl, see https://github.com/gosexy/gettext/issues/1
#include <libintl.h>
#include <locale.h>
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"strings"
"unsafe"
)
var (
// LcAll is for all of the locale.
LcAll = uint(C.LC_ALL)
// LcCollate is for regular expression matching (it determines the meaning of
// range expressions and equivalence classes) and string collation.
LcCollate = uint(C.LC_COLLATE)
// LcCtype is for regular expression matching, character classification,
// conversion, case-sensitive comparison, and wide character functions.
LcCtype = uint(C.LC_CTYPE)
// LcMessages is for localizable natural-language messages.
LcMessages = uint(C.LC_MESSAGES)
// LcMonetary is for monetary formatting.
LcMonetary = uint(C.LC_MONETARY)
// LcNumeric is for number formatting (such as the decimal point and the
// thousands separator).
LcNumeric = uint(C.LC_NUMERIC)
// LcTime is for time and date formatting.
LcTime = uint(C.LC_TIME)
)
// Deprecated but kept for backwards compatibility.
var (
LC_ALL = LcAll
LC_COLLATE = LcCollate
LC_CTYPE = LcCtype
LC_MESSAGES = LcMessages
LC_MONETARY = LcMonetary
LC_NUMERIC = LcNumeric
LC_TIME = LcTime
)
// SetLocale sets the program's current locale.
func SetLocale(category uint, locale string) string {
clocale := C.CString(locale)
defer C.free(unsafe.Pointer(clocale))
return C.GoString(C.setlocale(C.int(category), clocale))
}
// BindTextdomain sets the directory containing message catalogs.
func BindTextdomain(domainname string, dirname string) string {
cdirname := C.CString(dirname)
defer C.free(unsafe.Pointer(cdirname))
cdomainname := C.CString(domainname)
defer C.free(unsafe.Pointer(cdomainname))
return C.GoString(C.bindtextdomain(cdomainname, cdirname))
}
// BindTextdomainCodeset sets the output codeset for message catalogs on the
// given domainname.
func BindTextdomainCodeset(domainname string, codeset string) string {
cdomainname := C.CString(domainname)
defer C.free(unsafe.Pointer(cdomainname))
ccodeset := C.CString(codeset)
defer C.free(unsafe.Pointer(ccodeset))
return C.GoString(C.bind_textdomain_codeset(cdomainname, ccodeset))
}
// Textdomain sets or retrieves the current message domain.
func Textdomain(domainname string) string {
cdomainname := C.CString(domainname)
defer C.free(unsafe.Pointer(cdomainname))
return C.GoString(C.textdomain(cdomainname))
}
// Gettext attempts to translate a text string into the user's system language,
// by looking up the translation in a message catalog.
func Gettext(msgid string) string {
cmsgid := C.CString(msgid)
defer C.free(unsafe.Pointer(cmsgid))
return C.GoString(C.gettext(cmsgid))
}
// DGettext is like Gettext(), but looks up the message in the specified
// domain.
func DGettext(domain string, msgid string) string {
cdomain := cDomainName(domain)
defer C.free(unsafe.Pointer(cdomain))
cmsgid := C.CString(msgid)
defer C.free(unsafe.Pointer(cmsgid))
return C.GoString(C.dgettext(cdomain, cmsgid))
}
// DCGettext is like Gettext(), but looks up the message in the specified
// domain and category.
func DCGettext(domain string, msgid string, category uint) string {
cdomain := cDomainName(domain)
defer C.free(unsafe.Pointer(cdomain))
cmsgid := C.CString(msgid)
defer C.free(unsafe.Pointer(cmsgid))
return C.GoString(C.dcgettext(cdomain, cmsgid, C.int(category)))
}
// NGettext attempts to translate a text string into the user's system
// language, by looking up the appropriate plural form of the translation in a
// message catalog.
func NGettext(msgid string, msgidPlural string, n uint64) string {
cmsgid := C.CString(msgid)
defer C.free(unsafe.Pointer(cmsgid))
cmsgidPlural := C.CString(msgidPlural)
defer C.free(unsafe.Pointer(cmsgidPlural))
return C.GoString(C.ngettext(cmsgid, cmsgidPlural, C.ulong(n)))
}
// Sprintf is like fmt.Sprintf() but without %!(EXTRA) errors.
func Sprintf(format string, a ...interface{}) string {
expects := strings.Count(format, "%") - strings.Count(format, "%%")
if expects > 0 {
arguments := make([]interface{}, expects)
for i := 0; i < expects; i++ {
if len(a) > i {
arguments[i] = a[i]
}
}
return fmt.Sprintf(format, arguments...)
}
return format
}
// DNGettext is like NGettext(), but looks up the message in the specified
// domain.
func DNGettext(domainname string, msgid string, msgidPlural string, n uint64) string {
cdomainname := cDomainName(domainname)
cmsgid := C.CString(msgid)
cmsgidPlural := C.CString(msgidPlural)
defer func() {
C.free(unsafe.Pointer(cdomainname))
C.free(unsafe.Pointer(cmsgid))
C.free(unsafe.Pointer(cmsgidPlural))
}()
return C.GoString(C.dngettext(cdomainname, cmsgid, cmsgidPlural, C.ulong(n)))
}
// DCNGettext is like NGettext(), but looks up the message in the specified
// domain and category.
func DCNGettext(domainname string, msgid string, msgidPlural string, n uint64, category uint) string {
cdomainname := cDomainName(domainname)
cmsgid := C.CString(msgid)
cmsgidPlural := C.CString(msgidPlural)
defer func() {
C.free(unsafe.Pointer(cdomainname))
C.free(unsafe.Pointer(cmsgid))
C.free(unsafe.Pointer(cmsgidPlural))
}()
return C.GoString(C.dcngettext(cdomainname, cmsgid, cmsgidPlural, C.ulong(n), C.int(category)))
}
// cDomainName returns the domain name CString that can be nil.
func cDomainName(domain string) *C.char {
if domain == "" {
return nil
}
// The caller is responsible for freeing this up.
return C.CString(domain)
}