diff options
author | José Carlos <jose.carlos@menteslibres.net> | 2016-02-20 07:33:14 -0600 |
---|---|---|
committer | José Carlos <jose.carlos@menteslibres.net> | 2016-02-20 07:33:14 -0600 |
commit | ea584c8c09c390260b68a9e1a15d60f7ffe480c5 (patch) | |
tree | d84ea7ec068b7998adae2bd4580db9dc78584c56 /gettext.go | |
parent | Merge branch 'master' of github.com:gosexy/gettext (diff) | |
parent | Code cleaning. (diff) | |
download | gotext-ea584c8c09c390260b68a9e1a15d60f7ffe480c5.tar.gz gotext-ea584c8c09c390260b68a9e1a15d60f7ffe480c5.tar.xz |
Merge pull request #9 from gosexy/issue-5
Code cleaning.
Diffstat (limited to 'gettext.go')
-rw-r--r-- | gettext.go | 215 |
1 files changed, 103 insertions, 112 deletions
@@ -1,31 +1,30 @@ -/* - Copyright (c) 2012 José Carlos Nieto, http://xiam.menteslibres.org/ - - 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. -*/ - +// 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 /* - #include <libintl.h> + #include <locale.h> #include <stdlib.h> */ @@ -38,127 +37,117 @@ import ( ) var ( - // For all of the locale. - LC_ALL = uint(C.LC_ALL) + // LcAll is for all of the locale. + LcAll = uint(C.LC_ALL) - // For regular expression matching (it determines the meaning of range - // expressions and equivalence classes) and string collation. - LC_COLATE = uint(C.LC_ALL) + // LcColate is for regular expression matching (it determines the meaning of + // range expressions and equivalence classes) and string collation. + LcColate = uint(C.LC_ALL) - // For regular expression matching, character classification, conversion, - // case-sensitive comparison, and wide character functions. - LC_CTYPE = uint(C.LC_CTYPE) + // LcCtype is for regular expression matching, character classification, + // conversion, case-sensitive comparison, and wide character functions. + LcCtype = uint(C.LC_CTYPE) - // For localizable natural-language messages. - LC_MESSAGES = uint(C.LC_MESSAGES) + // LcMessages is for localizable natural-language messages. + LcMessages = uint(C.LC_MESSAGES) - // For monetary formatting. - LC_MONETARY = uint(C.LC_MONETARY) + // LcMonetary is for monetary formatting. + LcMonetary = uint(C.LC_MONETARY) - // For number formatting (such as the decimal point and the thousands - // separator). - LC_NUMERIC = uint(C.LC_NUMERIC) + // LcNumeric is for number formatting (such as the decimal point and the + // thousands separator). + LcNumeric = uint(C.LC_NUMERIC) - // For time and date formatting. - LC_TIME = uint(C.LC_TIME) + // LcTime is for time and date formatting. + LcTime = uint(C.LC_TIME) ) -// Sets or queries the program's current locale. +// SetLocale sets the program's current locale. func SetLocale(category uint, locale string) string { clocale := C.CString(locale) + defer C.free(unsafe.Pointer(clocale)) - res := C.GoString(C.setlocale(C.int(category), clocale)) - - C.free(unsafe.Pointer(clocale)) - return res + return C.GoString(C.setlocale(C.int(category), clocale)) } -// Sets directory containing message catalogs. +// BindTextdomain sets the directory containing message catalogs. func BindTextdomain(domainname string, dirname string) string { cdirname := C.CString(dirname) - cdomainname := C.CString(domainname) + defer C.free(unsafe.Pointer(cdirname)) - res := C.GoString(C.bindtextdomain(cdomainname, cdirname)) + cdomainname := C.CString(domainname) + defer C.free(unsafe.Pointer(cdomainname)) - C.free(unsafe.Pointer(cdirname)) - C.free(unsafe.Pointer(cdomainname)) - return res + return C.GoString(C.bindtextdomain(cdomainname, cdirname)) } -// Sets the output codeset for message catalogs for domain domainname. +// BindTextdomainCodeset sets the output codeset for message catalogs on the +// given domainname. func BindTextdomainCodeset(domainname string, codeset string) string { cdomainname := C.CString(domainname) - ccodeset := C.CString(codeset) + defer C.free(unsafe.Pointer(cdomainname)) - res := C.GoString(C.bind_textdomain_codeset(cdomainname, ccodeset)) + ccodeset := C.CString(codeset) + defer C.free(unsafe.Pointer(ccodeset)) - C.free(unsafe.Pointer(cdomainname)) - C.free(unsafe.Pointer(ccodeset)) - return res + return C.GoString(C.bind_textdomain_codeset(cdomainname, ccodeset)) } -// Sets or retrieves the current message domain. +// Textdomain sets or retrieves the current message domain. func Textdomain(domainname string) string { cdomainname := C.CString(domainname) + defer C.free(unsafe.Pointer(cdomainname)) - res := C.GoString(C.textdomain(cdomainname)) - - C.free(unsafe.Pointer(cdomainname)) - return res + return C.GoString(C.textdomain(cdomainname)) } -// Attempt to translate a text string into the user's native language, by -// looking up the translation in a message catalog. +// 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)) - res := C.GoString(C.gettext(cmsgid)) - - C.free(unsafe.Pointer(cmsgid)) - return res + return C.GoString(C.gettext(cmsgid)) } -// Like Gettext(), but looking up the message in the specified domain. +// DGettext is like Gettext(), but looks up the message in the specified +// domain. func DGettext(domain string, msgid string) string { cdomain := cDomainName(domain) - cmsgid := C.CString(msgid) + defer C.free(unsafe.Pointer(cdomain)) - res := C.GoString(C.dgettext(cdomain, cmsgid)) + cmsgid := C.CString(msgid) + defer C.free(unsafe.Pointer(cmsgid)) - C.free(unsafe.Pointer(cdomain)) - C.free(unsafe.Pointer(cmsgid)) - return res + return C.GoString(C.dgettext(cdomain, cmsgid)) } -// Like Gettext(), but looking up the message in the specified domain and -// category. +// 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) - cmsgid := C.CString(msgid) + defer C.free(unsafe.Pointer(cdomain)) - res := C.GoString(C.dcgettext(cdomain, cmsgid, C.int(category))) + cmsgid := C.CString(msgid) + defer C.free(unsafe.Pointer(cmsgid)) - C.free(unsafe.Pointer(cdomain)) - C.free(unsafe.Pointer(cmsgid)) - return res + return C.GoString(C.dcgettext(cdomain, cmsgid, C.int(category))) } -// 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. -func NGettext(msgid string, msgid_plural string, n uint64) string { +// 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) - cmsgid_plural := C.CString(msgid_plural) - - res := C.GoString(C.ngettext(cmsgid, cmsgid_plural, C.ulong(n))) + defer C.free(unsafe.Pointer(cmsgid)) - C.free(unsafe.Pointer(cmsgid)) - C.free(unsafe.Pointer(cmsgid_plural)) + cmsgidPlural := C.CString(msgidPlural) + defer C.free(unsafe.Pointer(cmsgidPlural)) - return res + return C.GoString(C.ngettext(cmsgid, cmsgidPlural, C.ulong(n))) } -// Like fmt.Sprintf() but without %!(EXTRA) errors. +// Sprintf is like fmt.Sprintf() but without %!(EXTRA) errors. func Sprintf(format string, a ...interface{}) string { expects := strings.Count(format, "%") - strings.Count(format, "%%") @@ -175,35 +164,36 @@ func Sprintf(format string, a ...interface{}) string { return format } -// Like NGettext(), but looking up the message in the specified domain. -func DNGettext(domainname string, msgid string, msgid_plural string, n uint64) string { +// 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) - cmsgid_plural := C.CString(msgid_plural) + cmsgidPlural := C.CString(msgidPlural) - res := C.GoString(C.dngettext(cdomainname, cmsgid, cmsgid_plural, C.ulong(n))) + defer func() { + C.free(unsafe.Pointer(cdomainname)) + C.free(unsafe.Pointer(cmsgid)) + C.free(unsafe.Pointer(cmsgidPlural)) + }() - C.free(unsafe.Pointer(cdomainname)) - C.free(unsafe.Pointer(cmsgid)) - C.free(unsafe.Pointer(cmsgid_plural)) - - return res + return C.GoString(C.dngettext(cdomainname, cmsgid, cmsgidPlural, C.ulong(n))) } -// Like NGettext(), but looking up the message in the specified domain and -// category. -func DCNGettext(domainname string, msgid string, msgid_plural string, n uint64, category uint) string { +// 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) - cmsgid_plural := C.CString(msgid_plural) - - res := C.GoString(C.dcngettext(cdomainname, cmsgid, cmsgid_plural, C.ulong(n), C.int(category))) + cmsgidPlural := C.CString(msgidPlural) - C.free(unsafe.Pointer(cdomainname)) - C.free(unsafe.Pointer(cmsgid)) - C.free(unsafe.Pointer(cmsgid_plural)) + defer func() { + C.free(unsafe.Pointer(cdomainname)) + C.free(unsafe.Pointer(cmsgid)) + C.free(unsafe.Pointer(cmsgidPlural)) + }() - return res + return C.GoString(C.dcngettext(cdomainname, cmsgid, cmsgidPlural, C.ulong(n), C.int(category))) } // cDomainName returns the domain name CString that can be nil. @@ -211,5 +201,6 @@ func cDomainName(domain string) *C.char { if domain == "" { return nil } + // The caller is responsible for freeing this up. return C.CString(domain) } |