From c7205ad47e6bd7e7816ef5186f463c53ea4a046e Mon Sep 17 00:00:00 2001 From: SQP Date: Mon, 8 Jun 2015 13:59:21 +0200 Subject: Fix translations with DGettext (and other D... func) when the domain name is empty. According to the doc, NULL allow to use the current domain as fallback: http://www.gnu.org/software/libc/manual/html_node/Translation-with-gettext.html If the domainname parameter is the null pointer the dgettext function is exactly equivalent to gettext since the default value for the domain name is used. --- gettext.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'gettext.go') diff --git a/gettext.go b/gettext.go index 352c857..624ab34 100644 --- a/gettext.go +++ b/gettext.go @@ -120,7 +120,7 @@ func Gettext(msgid string) string { // Like Gettext(), but looking up the message in the specified domain. func DGettext(domain string, msgid string) string { - cdomain := C.CString(domain) + cdomain := cDomainName(domain) cmsgid := C.CString(msgid) res := C.GoString(C.dgettext(cdomain, cmsgid)) @@ -133,7 +133,7 @@ func DGettext(domain string, msgid string) string { // Like Gettext(), but looking up the message in the specified domain and // category. func DCGettext(domain string, msgid string, category uint) string { - cdomain := C.CString(domain) + cdomain := cDomainName(domain) cmsgid := C.CString(msgid) res := C.GoString(C.dcgettext(cdomain, cmsgid, C.int(category))) @@ -177,7 +177,7 @@ func Sprintf(format string, a ...interface{}) string { // Like NGettext(), but looking up the message in the specified domain. func DNGettext(domainname string, msgid string, msgid_plural string, n uint64) string { - cdomainname := C.CString(domainname) + cdomainname := cDomainName(domainname) cmsgid := C.CString(msgid) cmsgid_plural := C.CString(msgid_plural) @@ -193,7 +193,7 @@ func DNGettext(domainname string, msgid string, msgid_plural string, n uint64) s // 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 { - cdomainname := C.CString(domainname) + cdomainname := cDomainName(domainname) cmsgid := C.CString(msgid) cmsgid_plural := C.CString(msgid_plural) @@ -205,3 +205,11 @@ func DCNGettext(domainname string, msgid string, msgid_plural string, n uint64, return res } + +// cDomainName returns the domain name CString that can be nil. +func cDomainName(domain string) *C.char { + if domain == "" { + return nil + } + return C.CString(domain) +} -- cgit v1.2.3