aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Carlos Nieto <xiam@menteslibres.org>2012-10-06 16:07:42 -0500
committerJosé Carlos Nieto <xiam@menteslibres.org>2012-10-06 16:07:42 -0500
commitf7f4bcdaad57ce74a2b0e32c771d30178e0b14e1 (patch)
treea28a5a3622436028ae93185a2cb603efa05464ad
parentInitial commit. (diff)
downloadgotext-f7f4bcdaad57ce74a2b0e32c771d30178e0b14e1.tar.gz
gotext-f7f4bcdaad57ce74a2b0e32c771d30178e0b14e1.tar.xz
Adding Deutsch and Español examples.
-rw-r--r--examples/de_DE.utf8/LC_MESSAGES/example.mobin0 -> 606 bytes
-rw-r--r--examples/de_DE.utf8/example.pot37
-rw-r--r--examples/es_MX.utf8/LC_MESSAGES/example.mobin0 -> 614 bytes
-rw-r--r--examples/es_MX.utf8/example.pot37
-rw-r--r--examples/gettext.go31
-rw-r--r--gettext.go22
-rw-r--r--gettext_test.go128
7 files changed, 255 insertions, 0 deletions
diff --git a/examples/de_DE.utf8/LC_MESSAGES/example.mo b/examples/de_DE.utf8/LC_MESSAGES/example.mo
new file mode 100644
index 0000000..36cc2a3
--- /dev/null
+++ b/examples/de_DE.utf8/LC_MESSAGES/example.mo
Binary files differ
diff --git a/examples/de_DE.utf8/example.pot b/examples/de_DE.utf8/example.pot
new file mode 100644
index 0000000..4afd100
--- /dev/null
+++ b/examples/de_DE.utf8/example.pot
@@ -0,0 +1,37 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr "Project-Id-Version: PACKAGE VERSION\n"
+ "Report-Msgid-Bugs-To: \n"
+ "POT-Creation-Date: 2012-10-06 15:47-0500\n"
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+ "Language-Team: LANGUAGE <LL@li.org>\n"
+ "Language: \n"
+ "MIME-Version: 1.0\n"
+ "Content-Type: text/plain; charset=UTF-8\n"
+ "Content-Transfer-Encoding: 8bit\n"
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
+
+#: gettext_test.go:32 gettext_test.go:40
+#, c-format
+msgid "An apple"
+msgid_plural "%d apples"
+msgstr[0] "Ein Apfel"
+msgstr[1] "%d Äpfel"
+
+#: gettext_test.go:56
+msgid "Good bye!"
+msgstr "Aufwiedersehen!"
+
+#: gettext_test.go:48
+msgid "Good morning"
+msgstr "Guten morgen"
+
+#: gettext_test.go:24
+msgid "Hello, world!"
+msgstr "Hallo, Welt!"
diff --git a/examples/es_MX.utf8/LC_MESSAGES/example.mo b/examples/es_MX.utf8/LC_MESSAGES/example.mo
new file mode 100644
index 0000000..ce1ebca
--- /dev/null
+++ b/examples/es_MX.utf8/LC_MESSAGES/example.mo
Binary files differ
diff --git a/examples/es_MX.utf8/example.pot b/examples/es_MX.utf8/example.pot
new file mode 100644
index 0000000..b490af0
--- /dev/null
+++ b/examples/es_MX.utf8/example.pot
@@ -0,0 +1,37 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+#, fuzzy
+msgid ""
+msgstr "Project-Id-Version: PACKAGE VERSION\n"
+ "Report-Msgid-Bugs-To: \n"
+ "POT-Creation-Date: 2012-10-06 15:47-0500\n"
+ "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
+ "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
+ "Language-Team: LANGUAGE <LL@li.org>\n"
+ "Language: \n"
+ "MIME-Version: 1.0\n"
+ "Content-Type: text/plain; charset=UTF-8\n"
+ "Content-Transfer-Encoding: 8bit\n"
+ "Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
+
+#: gettext_test.go:32 gettext_test.go:40
+#, c-format
+msgid "An apple"
+msgid_plural "%d apples"
+msgstr[0] "Una manzana"
+msgstr[1] "%d manzanas"
+
+#: gettext_test.go:56
+msgid "Good bye!"
+msgstr "¡Hasta luego!"
+
+#: gettext_test.go:48
+msgid "Good morning"
+msgstr "Buenos días"
+
+#: gettext_test.go:24
+msgid "Hello, world!"
+msgstr "¡Hola mundo!"
diff --git a/examples/gettext.go b/examples/gettext.go
new file mode 100644
index 0000000..a01e0d5
--- /dev/null
+++ b/examples/gettext.go
@@ -0,0 +1,31 @@
+package main
+
+import (
+ "github.com/gosexy/gettext"
+ "fmt"
+ "os"
+)
+
+func main() {
+ gettext.BindTextdomain("example", ".")
+ gettext.Textdomain("example")
+
+ os.Setenv("LANGUAGE", "es_MX.utf8")
+
+ gettext.SetLocale(gettext.LC_ALL, "")
+
+ fmt.Println(gettext.Gettext("Hello, world!"))
+
+ os.Setenv("LANGUAGE", "de_DE.utf8")
+
+ gettext.SetLocale(gettext.LC_ALL, "")
+
+ fmt.Println(gettext.Gettext("Hello, world!"))
+
+ os.Setenv("LANGUAGE", "en_US.utf8")
+
+ gettext.SetLocale(gettext.LC_ALL, "")
+
+ fmt.Println(gettext.Gettext("Hello, world!"))
+}
+
diff --git a/gettext.go b/gettext.go
index 95ad582..b8fd4c9 100644
--- a/gettext.go
+++ b/gettext.go
@@ -30,6 +30,11 @@ package gettext
*/
import "C"
+import (
+ "fmt"
+ "strings"
+)
+
var (
// For all of the locale.
LC_ALL = uint(C.LC_ALL)
@@ -95,6 +100,23 @@ 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)))
}
+// Like fmt.Sprintf() but without %!(EXTRA) errors.
+func Sprintf(format string, a ...interface{}) string {
+ expects := strings.Count(format, "%") - strings.Count(format, "%%")
+
+ if expects > 0 {
+ arguments := make([]interface{}, expects)
+ for i := 0; i < expects; i++ {
+ if len(a) > i {
+ arguments[i] = a[i]
+ }
+ }
+ return fmt.Sprintf(format, arguments...)
+ }
+
+ 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 {
return C.GoString(C.dngettext(C.CString(domainname), C.CString(msgid), C.CString(msgid_plural), C.ulong(n)))
diff --git a/gettext_test.go b/gettext_test.go
new file mode 100644
index 0000000..8d26edb
--- /dev/null
+++ b/gettext_test.go
@@ -0,0 +1,128 @@
+package gettext
+
+import (
+ "fmt"
+ "os"
+ "testing"
+)
+
+/*
+ NOTE:
+
+ xgettext does not officially support Go syntax, however, you can generate a valid .pot file by forcing
+ xgettest to use the C++ syntax:
+
+ % 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. Remember to set the UTF-8 charset.
+
+ After translating the .pot file, you must generate .po and .mo files.
+
+ % msginit -l es_MX -o example.po -i example.pot
+ % msgfmt -c -v -o example.mo example.po
+
+ And finally, move the .mo file to an appropriate location.
+
+ % mv example.mo examples/es_MX.utf8/LC_MESSAGES/example.mo
+
+*/
+
+func TestSpanishMexico(t *testing.T) {
+
+ os.Setenv("LANGUAGE", "es_MX.utf8")
+
+ SetLocale(LC_ALL, "")
+ BindTextdomain("example", "./examples/")
+ Textdomain("example")
+
+ t1 := Gettext("Hello, world!")
+
+ fmt.Println(t1)
+
+ if t1 != "¡Hola mundo!" {
+ t.Errorf("Failed translation.")
+ }
+
+ t2 := Sprintf(NGettext("An apple", "%d apples", 1), 1, "garbage")
+
+ fmt.Println(t2)
+
+ if t2 != "Una manzana" {
+ t.Errorf("Failed translation.")
+ }
+
+ t3 := Sprintf(NGettext("An apple", "%d apples", 3), 3)
+
+ fmt.Println(t3)
+
+ if t3 != "3 manzanas" {
+ t.Errorf("Failed translation.")
+ }
+
+ t4 := Gettext("Good morning")
+
+ fmt.Println(t4)
+
+ if t4 != "Buenos días" {
+ t.Errorf("Failed translation.")
+ }
+
+ t5 := Gettext("Good bye!")
+
+ fmt.Println(t5)
+
+ if t5 != "¡Hasta luego!" {
+ t.Errorf("Failed translation.")
+ }
+
+}
+
+func TestGermanDeutschland(t *testing.T) {
+
+ os.Setenv("LANGUAGE", "de_DE.utf8")
+
+ SetLocale(LC_ALL, "")
+ BindTextdomain("example", "./examples/")
+ Textdomain("example")
+
+ t1 := Gettext("Hello, world!")
+
+ fmt.Println(t1)
+
+ if t1 != "Hallo, Welt!" {
+ t.Errorf("Failed translation.")
+ }
+
+ t2 := Sprintf(NGettext("An apple", "%d apples", 1), 1, "garbage")
+
+ fmt.Println(t2)
+
+ if t2 != "Ein Apfel" {
+ t.Errorf("Failed translation.")
+ }
+
+ t3 := Sprintf(NGettext("An apple", "%d apples", 3), 3)
+
+ fmt.Println(t3)
+
+ if t3 != "3 Äpfel" {
+ t.Errorf("Failed translation.")
+ }
+
+ t4 := Gettext("Good morning")
+
+ fmt.Println(t4)
+
+ if t4 != "Guten morgen" {
+ t.Errorf("Failed translation.")
+ }
+
+ t5 := Gettext("Good bye!")
+
+ fmt.Println(t5)
+
+ if t5 != "Aufwiedersehen!" {
+ t.Errorf("Failed translation.")
+ }
+
+}