aboutsummaryrefslogtreecommitdiff
path: root/xgettext/main.go
diff options
context:
space:
mode:
authorJosé Carlos Nieto <jose.carlos@menteslibres.net>2016-02-07 07:03:29 -0600
committerJosé Carlos Nieto <jose.carlos@menteslibres.net>2016-02-07 07:03:29 -0600
commit6a8fac99a5315b373dea6838af2e8ed76cd86de8 (patch)
tree9015da42a6258d1a475d7f0a063132dc9e6293a3 /xgettext/main.go
parentMerge pull request #6 from mvo5/feature/xgettext (diff)
downloadgotext-6a8fac99a5315b373dea6838af2e8ed76cd86de8.tar.gz
gotext-6a8fac99a5315b373dea6838af2e8ed76cd86de8.tar.xz
Closes #7
Diffstat (limited to 'xgettext/main.go')
-rw-r--r--xgettext/main.go105
1 files changed, 65 insertions, 40 deletions
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)