diff options
author | José Carlos <jose.carlos@menteslibres.net> | 2016-01-09 09:06:46 -0600 |
---|---|---|
committer | José Carlos <jose.carlos@menteslibres.net> | 2016-01-09 09:06:46 -0600 |
commit | ea286668e74dbb65abeea59f0c51f00cd8df3c73 (patch) | |
tree | b291ecbcd587165c27a3fd16162019daf924348f | |
parent | Styling typos. (diff) | |
parent | Fix translations with DGettext (and other D... func) when the domain name is ... (diff) | |
download | gotext-ea286668e74dbb65abeea59f0c51f00cd8df3c73.tar.gz gotext-ea286668e74dbb65abeea59f0c51f00cd8df3c73.tar.xz |
Merge pull request #2 from sqp/nil_string
Fix translations with DGettext (and other D... func) when the domain name is empty.
-rw-r--r-- | gettext.go | 16 | ||||
-rw-r--r-- | gettext_test.go | 33 |
2 files changed, 45 insertions, 4 deletions
@@ -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) +} diff --git a/gettext_test.go b/gettext_test.go index b651152..8e88180 100644 --- a/gettext_test.go +++ b/gettext_test.go @@ -128,3 +128,36 @@ func TestGermanDeutschland(t *testing.T) { } } + +func TestDGettextFallback(t *testing.T) { + os.Setenv("LANGUAGE", "de_DE.utf8") + + SetLocale(LC_ALL, "") + BindTextdomain("example", "./examples/") + Textdomain("example") + + t1 := DGettext("", "Hello, world!") + + fmt.Println(t1) + + if t1 != "Hallo, Welt!" { + t.Errorf("Failed translation fallback.") + } + + t2 := Sprintf(DNGettext("", "An apple", "%d apples", 1), 1, "garbage") + + fmt.Println(t2) + + if t2 != "Ein Apfel" { + t.Errorf("Failed translation fallback.") + } + + t3 := Sprintf(DNGettext("", "An apple", "%d apples", 3), 3) + + fmt.Println(t3) + + if t3 != "3 Äpfel" { + t.Errorf("Failed translation fallback.") + } + +} |