aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Carlos Nieto <jose.carlos@menteslibres.net>2016-02-20 07:32:53 -0600
committerJosé Carlos Nieto <jose.carlos@menteslibres.net>2016-02-20 07:32:53 -0600
commit5d066dc1ea25bec034cb364f6b2fb73283bda063 (patch)
treed84ea7ec068b7998adae2bd4580db9dc78584c56
parentCode cleaning. (diff)
parentMerge branch 'master' of github.com:gosexy/gettext (diff)
downloadgotext-5d066dc1ea25bec034cb364f6b2fb73283bda063.tar.gz
gotext-5d066dc1ea25bec034cb364f6b2fb73283bda063.tar.xz
Code cleaning.
-rw-r--r--README.md39
-rw-r--r--xgettext/main.go105
2 files changed, 84 insertions, 60 deletions
diff --git a/README.md b/README.md
index d22f6ec..a916b4b 100644
--- a/README.md
+++ b/README.md
@@ -20,6 +20,8 @@ export CGO_CPPFLAGS=-I/usr/local/opt/gettext/include
go get github.com/gosexy/gettext
```
+Installation should be straightforward on Linux.
+
## Installation
Use `go get` to download and install the binding:
@@ -30,6 +32,9 @@ go get github.com/gosexy/gettext
## Usage
+This is an example program showing the `BindTextdomain`, `Textdomain` and
+`SetLocale` bindings:
+
```go
package main
@@ -51,27 +56,27 @@ func main() {
}
```
-You can use `os.Setenv` to set the `LANGUAGE` environment variable or set it
-on a terminal:
+Set the `LANGUAGE` env to the name of the language you want to use in your
+program:
```sh
export LANGUAGE="es_MX.utf8"
-./gettext-program
+./myapp
```
-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:
+You can use the `xgettext` command to extract strings to be translated from a
+Go program:
-```sh
-xgettext -d example -s gettext_test.go -o example.pot -L c++ -i \
---keyword=NGettext:1,2 --keyword=Gettext
+```
+go get github.com/gosexy/gettext/go-xgettext
+
+xgettext -o outfile.pot --keyword=Gettext --keyword-plural=NGettext -i infile.go
```
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 actually translating the `.pot` file, you'll have to generate `.po` and
+`.mo` files with `msginit` and `msgfmt`:
```sh
msginit -l es_MX -o example.po -i example.pot
@@ -86,21 +91,15 @@ mv example.mo examples/es_MX.utf8/LC_MESSAGES/example.mo
## Documentation
-You can read `gosexy/gettext` documentation from a terminal
-
-```sh
-go doc github.com/gosexy/gettext
-```
-
-Or you can [browse it](http://godoc.org/github.com/gosexy/gettext) online.
+Check out the API documentation [godoc.org/github.com/gosexy/gettext)](http://godoc.org/github.com/gosexy/gettext).
-The original gettext documentation could be very useful as well:
+The original gettext documentation:
```sh
man 3 gettext
```
-Here's another [good tutorial][2] on using gettext.
+And here's a [good tutorial][2] on using gettext.
[1]: http://www.gnu.org/software/gettext/
[2]: http://oriya.sarovar.org/docs/gettext_single.html
diff --git a/xgettext/main.go b/xgettext/main.go
index 34c4bfe..881cb9f 100644
--- a/xgettext/main.go
+++ b/xgettext/main.go
@@ -119,19 +119,42 @@ func constructValue(val interface{}) string {
func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bool {
// FIXME: this assume we always have a "gettext.Gettext" style keyword
+ var gettextSelector, gettextFuncName string
l := strings.Split(opts.Keyword, ".")
- gettextSelector := l[0]
- gettextFuncName := l[1]
+ if len(l) > 1 {
+ gettextSelector = l[0]
+ gettextFuncName = l[1]
+ } else {
+ gettextFuncName = l[0]
+ }
+
+ var gettextSelectorPlural, gettextFuncNamePlural string
l = strings.Split(opts.KeywordPlural, ".")
- gettextSelectorPlural := l[0]
- gettextFuncNamePlural := l[1]
+
+ if len(l) > 1 {
+ gettextSelectorPlural = l[0]
+ gettextFuncNamePlural = l[1]
+ } else {
+ gettextFuncNamePlural = l[0]
+ }
switch x := n.(type) {
case *ast.CallExpr:
- if sel, ok := x.Fun.(*ast.SelectorExpr); ok {
- i18nStr := ""
- i18nStrPlural := ""
+ var i18nStr, i18nStrPlural string
+ //if sel, ok := x.Fun.(*ast.Ident); ok {
+
+ //}
+ switch sel := x.Fun.(type) {
+ case *ast.Ident:
+ if sel.Name == gettextFuncNamePlural {
+ i18nStr = x.Args[0].(*ast.BasicLit).Value
+ i18nStrPlural = x.Args[1].(*ast.BasicLit).Value
+ }
+ if sel.Name == gettextFuncName {
+ i18nStr = constructValue(x.Args[0])
+ }
+ case *ast.SelectorExpr:
if sel.Sel.Name == gettextFuncNamePlural && sel.X.(*ast.Ident).Name == gettextSelectorPlural {
i18nStr = x.Args[0].(*ast.BasicLit).Value
i18nStrPlural = x.Args[1].(*ast.BasicLit).Value
@@ -140,47 +163,49 @@ func inspectNodeForTranslations(fset *token.FileSet, f *ast.File, n ast.Node) bo
if sel.Sel.Name == gettextFuncName && sel.X.(*ast.Ident).Name == gettextSelector {
i18nStr = constructValue(x.Args[0])
}
+ }
- formatI18nStr := func(s string) string {
- if s == "" {
- return ""
- }
- // the "`" is special
- if s[0] == '`' {
- // replace inner " with \"
- s = strings.Replace(s, "\"", "\\\"", -1)
- // replace \n with \\n
- s = strings.Replace(s, "\n", "\\n", -1)
- }
- // strip leading and trailing " (or `)
- s = s[1 : len(s)-1]
- return s
- }
-
- // FIXME: too simplistic(?), no %% is considered
- formatHint := ""
- if strings.Contains(i18nStr, "%") || strings.Contains(i18nStrPlural, "%") {
- // well, not quite correct but close enough
- formatHint = "c-format"
- }
+ if i18nStr == "" {
+ break
+ }
- if i18nStr != "" {
- msgidStr := formatI18nStr(i18nStr)
- posCall := fset.Position(n.Pos())
- msgIDs[msgidStr] = append(msgIDs[msgidStr], msgID{
- formatHint: formatHint,
- msgidPlural: formatI18nStr(i18nStrPlural),
- fname: posCall.Filename,
- line: posCall.Line,
- comment: findCommentsForTranslation(fset, f, posCall),
- })
- }
+ // FIXME: too simplistic(?), no %% is considered
+ formatHint := ""
+ if strings.Contains(i18nStr, "%") || strings.Contains(i18nStrPlural, "%") {
+ // well, not quite correct but close enough
+ formatHint = "c-format"
}
+
+ msgidStr := formatI18nStr(i18nStr)
+ posCall := fset.Position(n.Pos())
+ msgIDs[msgidStr] = append(msgIDs[msgidStr], msgID{
+ formatHint: formatHint,
+ msgidPlural: formatI18nStr(i18nStrPlural),
+ fname: posCall.Filename,
+ line: posCall.Line,
+ comment: findCommentsForTranslation(fset, f, posCall),
+ })
}
return true
}
+func formatI18nStr(s string) string {
+ if s == "" {
+ return ""
+ }
+ // the "`" is special
+ if s[0] == '`' {
+ // replace inner " with \"
+ s = strings.Replace(s, "\"", "\\\"", -1)
+ // replace \n with \\n
+ s = strings.Replace(s, "\n", "\\n", -1)
+ }
+ // strip leading and trailing " (or `)
+ s = s[1 : len(s)-1]
+ return s
+}
+
func processFiles(args []string) error {
// go over the input files
msgIDs = make(map[string][]msgID)