aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--LICENSE2
-rw-r--r--README.md27
-rw-r--r--gettext.go117
3 files changed, 115 insertions, 31 deletions
diff --git a/LICENSE b/LICENSE
index 4cf4dde..92a8fde 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2012 José Carlos Nieto, http://xiam.menteslibres.org/
+Copyright (c) 2012-2013 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
diff --git a/README.md b/README.md
index 97b3581..e6f52ba 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,16 @@
## gosexy/gettext
-``gosexy/gettext`` is a wrapper of [GNU gettext][1], an internationalization and localization library for writing multilingual systems.
+Go bindings for [GNU gettext][1], an internationalization and localization
+library for writing multilingual systems.
## Requeriments
-The GNU C library. If you're using GNU/Linux, FreeBSD or OSX you should already have it.
+The GNU C library. If you're using GNU/Linux, FreeBSD or OSX you should already
+have it.
## Installation
-Just pull from the repository:
+Use `go get` to download and install the binding:
```sh
% go get github.com/gosexy/gettext
@@ -37,30 +39,33 @@ func main() {
}
```
-You can use ``os.Setenv`` to set the ``LANGUAGE`` environment variable or set it from a terminal:
+You can use `os.Setenv` to set the `LANGUAGE` environment variable or set it
+on a terminal:
```sh
% export LANGUAGE="es_MX.utf8"
% ./gettext-program
```
-Note that ``xgettext`` does not officially support Go syntax yet, however, you can generate a valid ``.pot`` file by forcing
-``xgettest`` to use the C++ syntax:
+Note that `xgettext` does not officially support Go syntax yet, however, you
+can generate a valid `.pot` file by forcing `xgettest` to use the C++
+syntax:
```sh
% xgettext -d example -s gettext_test.go -o example.pot -L c++ -i --keyword=NGettext:1,2 --keyword=Gettext
```
-This will generate a ``example.pot`` file.
+This will generate a `example.pot` file.
-After translating the .pot file, you must generate .po and .mo files and remember to set the UTF-8 charset.
+After translating the `.pot` file, you must generate `.po` and `.mo` files and
+remember to set the UTF-8 charset.
```sh
% msginit -l es_MX -o example.po -i example.pot
% msgfmt -c -v -o example.mo example.po
```
-Finally, move the .mo file to an appropriate location.
+Finally, move the `.mo` file to an appropriate location.
```sh
% mv example.mo examples/es_MX.utf8/LC_MESSAGES/example.mo
@@ -68,13 +73,13 @@ Finally, move the .mo file to an appropriate location.
## Documentation
-You can read ``gosexy/gettext`` documentation from a terminal
+You can read `gosexy/gettext` documentation from a terminal
```go
% go doc github.com/gosexy/gettext
```
-Or you can [browse it](http://go.pkgdoc.org/github.com/gosexy/gettext) online.
+Or you can [browse it](http://godoc.org/github.com/gosexy/gettext) online.
The original gettext documentation could be very useful as well:
diff --git a/gettext.go b/gettext.go
index b8fd4c9..352c857 100644
--- a/gettext.go
+++ b/gettext.go
@@ -27,22 +27,26 @@ package gettext
#include <libintl.h>
#include <locale.h>
+#include <stdlib.h>
*/
import "C"
import (
"fmt"
"strings"
+ "unsafe"
)
var (
// For all of the locale.
LC_ALL = uint(C.LC_ALL)
- // For regular expression matching (it determines the meaning of range expressions and equivalence classes) and string collation.
+ // For regular expression matching (it determines the meaning of range
+ // expressions and equivalence classes) and string collation.
LC_COLATE = uint(C.LC_ALL)
- // For regular expression matching, character classification, conversion, case-sensitive comparison, and wide character functions.
+ // For regular expression matching, character classification, conversion,
+ // case-sensitive comparison, and wide character functions.
LC_CTYPE = uint(C.LC_CTYPE)
// For localizable natural-language messages.
@@ -51,7 +55,8 @@ var (
// For monetary formatting.
LC_MONETARY = uint(C.LC_MONETARY)
- // For number formatting (such as the decimal point and the thousands separator).
+ // For number formatting (such as the decimal point and the thousands
+ // separator).
LC_NUMERIC = uint(C.LC_NUMERIC)
// For time and date formatting.
@@ -60,44 +65,97 @@ var (
// Sets or queries the program's current locale.
func SetLocale(category uint, locale string) string {
- return C.GoString(C.setlocale(C.int(category), C.CString(locale)))
+ clocale := C.CString(locale)
+
+ res := C.GoString(C.setlocale(C.int(category), clocale))
+
+ C.free(unsafe.Pointer(clocale))
+ return res
}
// Sets directory containing message catalogs.
func BindTextdomain(domainname string, dirname string) string {
- return C.GoString(C.bindtextdomain(C.CString(domainname), C.CString(dirname)))
+ cdirname := C.CString(dirname)
+ cdomainname := C.CString(domainname)
+
+ res := C.GoString(C.bindtextdomain(cdomainname, cdirname))
+
+ C.free(unsafe.Pointer(cdirname))
+ C.free(unsafe.Pointer(cdomainname))
+ return res
}
// Sets the output codeset for message catalogs for domain domainname.
func BindTextdomainCodeset(domainname string, codeset string) string {
- return C.GoString(C.bind_textdomain_codeset(C.CString(domainname), C.CString(codeset)))
+ cdomainname := C.CString(domainname)
+ ccodeset := C.CString(codeset)
+
+ res := C.GoString(C.bind_textdomain_codeset(cdomainname, ccodeset))
+
+ C.free(unsafe.Pointer(cdomainname))
+ C.free(unsafe.Pointer(ccodeset))
+ return res
}
// Sets or retrieves the current message domain.
func Textdomain(domainname string) string {
- return C.GoString(C.textdomain(C.CString(domainname)))
+ cdomainname := C.CString(domainname)
+
+ res := C.GoString(C.textdomain(cdomainname))
+
+ C.free(unsafe.Pointer(cdomainname))
+ return res
}
-// Attempt to translate a text string into the user's native language, by looking up the translation in a message
-// catalog.
+// Attempt to translate a text string into the user's native language, by
+// looking up the translation in a message catalog.
func Gettext(msgid string) string {
- return C.GoString(C.gettext(C.CString(msgid)))
+ cmsgid := C.CString(msgid)
+
+ res := C.GoString(C.gettext(cmsgid))
+
+ C.free(unsafe.Pointer(cmsgid))
+ return res
}
// Like Gettext(), but looking up the message in the specified domain.
func DGettext(domain string, msgid string) string {
- return C.GoString(C.dgettext(C.CString(domain), C.CString(msgid)))
+ cdomain := C.CString(domain)
+ cmsgid := C.CString(msgid)
+
+ res := C.GoString(C.dgettext(cdomain, cmsgid))
+
+ C.free(unsafe.Pointer(cdomain))
+ C.free(unsafe.Pointer(cmsgid))
+ return res
}
-// Like Gettext(), but looking up the message in the specified domain and category.
+// Like Gettext(), but looking up the message in the specified domain and
+// category.
func DCGettext(domain string, msgid string, category uint) string {
- return C.GoString(C.dcgettext(C.CString(domain), C.CString(msgid), C.int(category)))
+ cdomain := C.CString(domain)
+ cmsgid := C.CString(msgid)
+
+ res := C.GoString(C.dcgettext(cdomain, cmsgid, C.int(category)))
+
+ C.free(unsafe.Pointer(cdomain))
+ C.free(unsafe.Pointer(cmsgid))
+ return res
}
-// 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.
+// 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 {
- return C.GoString(C.ngettext(C.CString(msgid), C.CString(msgid_plural), C.ulong(n)))
+ cmsgid := C.CString(msgid)
+ cmsgid_plural := C.CString(msgid_plural)
+
+ res := C.GoString(C.ngettext(cmsgid, cmsgid_plural, C.ulong(n)))
+
+ C.free(unsafe.Pointer(cmsgid))
+ C.free(unsafe.Pointer(cmsgid_plural))
+
+ return res
}
// Like fmt.Sprintf() but without %!(EXTRA) errors.
@@ -119,10 +177,31 @@ 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 {
- return C.GoString(C.dngettext(C.CString(domainname), C.CString(msgid), C.CString(msgid_plural), C.ulong(n)))
+ cdomainname := C.CString(domainname)
+ cmsgid := C.CString(msgid)
+ cmsgid_plural := C.CString(msgid_plural)
+
+ res := C.GoString(C.dngettext(cdomainname, cmsgid, cmsgid_plural, C.ulong(n)))
+
+ C.free(unsafe.Pointer(cdomainname))
+ C.free(unsafe.Pointer(cmsgid))
+ C.free(unsafe.Pointer(cmsgid_plural))
+
+ return res
}
-// Like NGettext(), but looking up the message in the specified domain and category.
+// 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 {
- return C.GoString(C.dcngettext(C.CString(domainname), C.CString(msgid), C.CString(msgid_plural), C.ulong(n), C.int(category)))
+ cdomainname := C.CString(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)))
+
+ C.free(unsafe.Pointer(cdomainname))
+ C.free(unsafe.Pointer(cmsgid))
+ C.free(unsafe.Pointer(cmsgid_plural))
+
+ return res
}