File: gettext.go

package info (click to toggle)
golang-github-chai2010-gettext-go 0.0~git20191225.6b9f4b1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,256 kB
  • sloc: makefile: 23
file content (184 lines) | stat: -rw-r--r-- 5,747 bytes parent folder | download | duplicates (4)
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
// 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

var (
	defaultManager = newDomainManager()
)

var (
	DefaultLocale = getDefaultLocale() // use $(LC_MESSAGES) or $(LANG) or "default"
)

// SetLocale sets and queries the program's current locale.
//
// If the locale is not empty string, set the new local.
//
// If the locale is empty string, don't change anything.
//
// Returns is the current locale.
//
// Examples:
//	SetLocale("")      // get locale: return DefaultLocale
//	SetLocale("zh_CN") // set locale: return zh_CN
//	SetLocale("")      // get locale: return zh_CN
func SetLocale(locale string) string {
	return defaultManager.SetLocale(locale)
}

// BindTextdomain sets and queries program's domains.
//
// If the domain and path are all not empty string, bind the new domain.
// If the domain already exists, return error.
//
// If the domain is not empty string, but the path is the empty string,
// delete the domain.
// If the domain don't exists, return error.
//
// If the domain and the path are all empty string, don't change anything.
//
// Returns is the all bind domains.
//
// Examples:
//	BindTextdomain("poedit", "local", nil) // bind "poedit" domain
//	BindTextdomain("", "", nil)            // return all domains
//	BindTextdomain("poedit", "", nil)      // delete "poedit" domain
//	BindTextdomain("", "", nil)            // return all domains
//
// Use zip file:
//	BindTextdomain("poedit", "local.zip", nil)     // bind "poedit" domain
//	BindTextdomain("poedit", "local.zip", zipData) // bind "poedit" domain
//
func BindTextdomain(domain, path string, zipData []byte) (domains, paths []string) {
	return defaultManager.Bind(domain, path, zipData)
}

// Textdomain sets and retrieves the current message domain.
//
// If the domain is not empty string, set the new domains.
//
// If the domain is empty string, don't change anything.
//
// Returns is the all used domains.
//
// Examples:
//	Textdomain("poedit") // set domain: poedit
//	Textdomain("")       // get domain: return poedit
func Textdomain(domain string) string {
	return defaultManager.SetDomain(domain)
}

// Gettext attempt to translate a text string into the user's native language,
// by looking up the translation in a message catalog.
//
// It use the caller's function name as the msgctxt.
//
// Examples:
//	func Foo() {
//		msg := gettext.Gettext("Hello") // msgctxt is "some/package/name.Foo"
//	}
func Gettext(msgid string) string {
	return PGettext(callerName(2), msgid)
}

// Getdata attempt to translate a resource file into the user's native language,
// by looking up the translation in a message catalog.
//
// Examples:
//	func Foo() {
//		Textdomain("hello")
//		BindTextdomain("hello", "local.zip", nilOrZipData)
//		poems := gettext.Getdata("poems.txt")
//	}
func Getdata(name string) []byte {
	return defaultManager.Getdata(name)
}

// NGettext attempt to translate a text string into the user's native language,
// by looking up the appropriate plural form of the translation in a message
// catalog.
//
// It use the caller's function name as the msgctxt.
//
// Examples:
//	func Foo() {
//		msg := gettext.NGettext("%d people", "%d peoples", 2)
//	}
func NGettext(msgid, msgidPlural string, n int) string {
	return PNGettext(callerName(2), msgid, msgidPlural, n)
}

// PGettext attempt to translate a text string into the user's native language,
// by looking up the translation in a message catalog.
//
// Examples:
//	func Foo() {
//		msg := gettext.PGettext("gettext-go.example", "Hello") // msgctxt is "gettext-go.example"
//	}
func PGettext(msgctxt, msgid string) string {
	return PNGettext(msgctxt, msgid, "", 0)
}

// PNGettext attempt to translate a text string into the user's native language,
// by looking up the appropriate plural form of the translation in a message
// catalog.
//
// Examples:
//	func Foo() {
//		msg := gettext.PNGettext("gettext-go.example", "%d people", "%d peoples", 2)
//	}
func PNGettext(msgctxt, msgid, msgidPlural string, n int) string {
	return defaultManager.PNGettext(msgctxt, msgid, msgidPlural, n)
}

// DGettext like Gettext(), but looking up the message in the specified domain.
//
// Examples:
//	func Foo() {
//		msg := gettext.DGettext("poedit", "Hello")
//	}
func DGettext(domain, msgid string) string {
	return DPGettext(domain, callerName(2), msgid)
}

// DNGettext like NGettext(), but looking up the message in the specified domain.
//
// Examples:
//	func Foo() {
//		msg := gettext.PNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2)
//	}
func DNGettext(domain, msgid, msgidPlural string, n int) string {
	return DPNGettext(domain, callerName(2), msgid, msgidPlural, n)
}

// DPGettext like PGettext(), but looking up the message in the specified domain.
//
// Examples:
//	func Foo() {
//		msg := gettext.DPGettext("poedit", "gettext-go.example", "Hello")
//	}
func DPGettext(domain, msgctxt, msgid string) string {
	return DPNGettext(domain, msgctxt, msgid, "", 0)
}

// DPNGettext like PNGettext(), but looking up the message in the specified domain.
//
// Examples:
//	func Foo() {
//		msg := gettext.DPNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2)
//	}
func DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string {
	return defaultManager.DPNGettext(domain, msgctxt, msgid, msgidPlural, n)
}

// DGetdata like Getdata(), but looking up the resource in the specified domain.
//
// Examples:
//	func Foo() {
//		msg := gettext.DGetdata("hello", "poems.txt")
//	}
func DGetdata(domain, name string) []byte {
	return defaultManager.DGetdata(domain, name)
}