1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// Copyright 2020 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"go/ast"
"go/importer"
"go/parser"
"go/token"
"go/types"
"log"
"path/filepath"
"strings"
"time"
"github.com/chai2010/gettext-go/po"
)
type Package struct {
path string
pkgpath string
pkgname string
filesAbspath []string
fset *token.FileSet
astFiles []*ast.File
typesInfo *types.Info
typesPackage *types.Package
potFile *po.File
}
func LoadPackage(path string) *Package {
p := &Package{
path: path,
pkgpath: gopkgPath(path),
pkgname: gopkgName(path),
filesAbspath: gopkgFilesAbspath(path),
}
var fset = token.NewFileSet()
var astFiles = make([]*ast.File, len(p.filesAbspath))
for i, path := range p.filesAbspath {
f, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
log.Fatal(err)
}
astFiles[i] = f
}
// https://github.com/golang/go/issues/26504
typesConfig := &types.Config{
Importer: importer.For("source", nil),
FakeImportC: true,
}
typesInfo := &types.Info{
Types: make(map[ast.Expr]types.TypeAndValue),
Defs: make(map[*ast.Ident]types.Object),
Uses: make(map[*ast.Ident]types.Object),
Selections: make(map[*ast.SelectorExpr]*types.Selection),
}
typesPackage, err := typesConfig.Check(p.pkgname, fset, astFiles, typesInfo)
if err != nil {
log.Fatal(err)
}
p.fset = fset
p.astFiles = astFiles
p.typesInfo = typesInfo
p.typesPackage = typesPackage
return p
}
func (p *Package) GenPotFile() *po.File {
p.potFile = &po.File{
MimeHeader: po.Header{
Comment: po.Comment{
TranslatorComment: "" +
fmt.Sprintf("package: %s\n\n", p.pkgpath) +
"Generated By gettext-go" + "\n" +
"https://github.com/chai2010/gettext-go" + "\n",
},
ProjectIdVersion: "1.0",
POTCreationDate: time.Now().Format("2006-01-02 15:04-0700"),
LanguageTeam: "golang-china",
Language: "zh_CN",
MimeVersion: "MIME-Version: 1.0",
ContentType: "Content-Type: text/plain; charset=UTF-8",
ContentTransferEncoding: "Content-Transfer-Encoding: 8bit",
},
}
for _, f := range p.astFiles {
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.CallExpr:
switch sel := x.Fun.(type) {
case *ast.SelectorExpr:
if p.isGettextPackage(sel.X) {
p.processGettext(x, sel.Sel.Name)
}
}
}
return true
})
}
return p.potFile
}
func (p *Package) isGettextPackage(node ast.Node) bool {
inner := p.typesPackage.Scope().Innermost(node.Pos())
if ident, ok := node.(*ast.Ident); ok {
if _, obj := inner.LookupParent(ident.Name, node.Pos()); obj != nil {
if pkgName, ok := obj.(*types.PkgName); ok {
if pkg := pkgName.Imported(); pkg != nil {
if pkg.Path() == "github.com/chai2010/gettext-go" {
return true
}
}
}
}
}
return false
}
func (p *Package) processGettext(x *ast.CallExpr, fnName string) {
switch fnName {
case "Gettext": // Gettext(msgid string) string
pos := p.fset.Position(x.Pos())
p.potFile.Messages = append(p.potFile.Messages, po.Message{
Comment: po.Comment{
ReferenceFile: []string{p.pkgpath + "/" + filepath.Base(pos.Filename)},
ReferenceLine: []int{pos.Line},
Flags: []string{"go-format"},
},
MsgContext: "",
MsgId: p.evalStringValue(x.Args[0]),
})
case "PGettext": // PGettext(msgctxt, msgid string) string
pos := p.fset.Position(x.Pos())
p.potFile.Messages = append(p.potFile.Messages, po.Message{
Comment: po.Comment{
ReferenceFile: []string{p.pkgpath + "/" + filepath.Base(pos.Filename)},
ReferenceLine: []int{pos.Line},
Flags: []string{"go-format"},
},
MsgContext: p.evalStringValue(x.Args[0]),
MsgId: p.evalStringValue(x.Args[1]),
})
case "NGettext": // NGettext(msgid, msgidPlural string, n int) string
// TODO
case "PNGettext": // PNGettext(msgctxt, msgid, msgidPlural string, n int) string
// TODO
case "DGettext": // DGettext(domain, msgid string) string
// TODO
case "DPGettext": // DPGettext(domain, msgctxt, msgid string) string
// TODO
case "DNGettext": // DNGettext(domain, msgid, msgidPlural string, n int) string
// TODO
case "DPNGettext": // DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string
// TODO
}
}
func (p *Package) evalStringValue(val interface{}) string {
switch val.(type) {
case *ast.BasicLit:
s := val.(*ast.BasicLit).Value
s = strings.TrimSpace(s)
s = strings.Trim(s, `"`+"\n")
return s
case *ast.BinaryExpr:
if val.(*ast.BinaryExpr).Op != token.ADD {
return ""
}
left := p.evalStringValue(val.(*ast.BinaryExpr).X)
right := p.evalStringValue(val.(*ast.BinaryExpr).Y)
return left[0:len(left)-1] + right[1:len(right)]
default:
panic(fmt.Sprintf("unknown type: %v", val))
}
}
|