Parse SetDomain methods
This commit is contained in:
@@ -29,7 +29,8 @@ func main() {
|
|||||||
l := gotext.NewLocale("/path/to/locales/root/dir", "es_UY")
|
l := gotext.NewLocale("/path/to/locales/root/dir", "es_UY")
|
||||||
|
|
||||||
// Load domain '/path/to/locales/root/dir/es_UY/default.po'
|
// Load domain '/path/to/locales/root/dir/es_UY/default.po'
|
||||||
l.AddDomain("default")
|
l.AddDomain("translations")
|
||||||
|
l.SetDomain("translations")
|
||||||
|
|
||||||
// Translate text from domain
|
// Translate text from domain
|
||||||
fmt.Println(l.GetD("translations", "Translate this"))
|
fmt.Println(l.GetD("translations", "Translate this"))
|
||||||
@@ -39,7 +40,7 @@ func main() {
|
|||||||
num := 17
|
num := 17
|
||||||
l.GetN("SingularVar", "PluralVar", num)
|
l.GetN("SingularVar", "PluralVar", num)
|
||||||
|
|
||||||
l.GetDC("domain", "string", "ctx")
|
l.GetDC("domain2", "string", "ctx")
|
||||||
l.GetNDC("translations", "ndc", "ndcs", 7, "NDC-CTX")
|
l.GetNDC("translations", "ndc", "ndcs", 7, "NDC-CTX")
|
||||||
|
|
||||||
f := Fake{}
|
f := Fake{}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/parser"
|
"go/parser"
|
||||||
@@ -12,8 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dirName string
|
debug = flag.Bool("debug", false, "enable debug mode and print AST")
|
||||||
outputDir string
|
dirName = flag.String("in", "", "input dir: /path/to/go/pkg")
|
||||||
|
outputDir = flag.String("out", "", "output dir: /path/to/i18n/files")
|
||||||
fset *token.FileSet
|
fset *token.FileSet
|
||||||
domainFiles map[string]*os.File
|
domainFiles map[string]*os.File
|
||||||
currentDomain = "default"
|
currentDomain = "default"
|
||||||
@@ -21,33 +23,26 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
// Init logger
|
// Init logger
|
||||||
log.SetFlags(0)
|
log.SetFlags(0)
|
||||||
|
|
||||||
// Init domain files
|
// Init domain files
|
||||||
domainFiles = make(map[string]*os.File)
|
domainFiles = make(map[string]*os.File)
|
||||||
|
|
||||||
// Validate args
|
|
||||||
if len(os.Args) < 2 {
|
|
||||||
log.Println("Usage: ")
|
|
||||||
log.Fatal("$ xgotext /path/to/package [ /path/to/output/dir ]")
|
|
||||||
}
|
|
||||||
if len(os.Args) > 2 {
|
|
||||||
outputDir = os.Args[2]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if dir name parameter is valid
|
// Check if dir name parameter is valid
|
||||||
dirName = os.Args[1]
|
log.Println(*dirName)
|
||||||
f, err := os.Stat(dirName)
|
f, err := os.Stat(*dirName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process file or dir
|
// Process file or dir
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
parseDir(dirName)
|
parseDir(*dirName)
|
||||||
} else {
|
} else {
|
||||||
parseFile(dirName)
|
parseFile(*dirName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,7 +53,7 @@ func getDomainFile(domain string) *os.File {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the file doesn't exist, create it.
|
// If the file doesn't exist, create it.
|
||||||
filePath := path.Join(outputDir, domain+".po")
|
filePath := path.Join(*outputDir, domain+".po")
|
||||||
f, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
f, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -138,8 +133,10 @@ func parseFile(fileName string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug
|
// Debug mode
|
||||||
//ast.Print(fset, node)
|
if *debug {
|
||||||
|
ast.Print(fset, node)
|
||||||
|
}
|
||||||
|
|
||||||
ast.Inspect(node, inspectFile)
|
ast.Inspect(node, inspectFile)
|
||||||
|
|
||||||
@@ -182,12 +179,14 @@ func inspectCallExpr(n *ast.CallExpr) {
|
|||||||
case "GetNDC":
|
case "GetNDC":
|
||||||
parseGetNDC(n)
|
parseGetNDC(n)
|
||||||
|
|
||||||
|
case "SetDomain":
|
||||||
|
parseSetDomain(n)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseGet(call *ast.CallExpr) {
|
func parseGet(call *ast.CallExpr) {
|
||||||
// Expect first param to be string
|
|
||||||
if call.Args != nil && len(call.Args) > 0 {
|
if call.Args != nil && len(call.Args) > 0 {
|
||||||
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
||||||
if lit.Kind == token.STRING {
|
if lit.Kind == token.STRING {
|
||||||
@@ -202,7 +201,6 @@ func parseGet(call *ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseGetN(call *ast.CallExpr) {
|
func parseGetN(call *ast.CallExpr) {
|
||||||
// Expect at least 3 params, first 2 strings, third int
|
|
||||||
if call.Args == nil || len(call.Args) < 3 {
|
if call.Args == nil || len(call.Args) < 3 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -234,7 +232,6 @@ func parseGetN(call *ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseGetD(call *ast.CallExpr) {
|
func parseGetD(call *ast.CallExpr) {
|
||||||
// Expect first 2 params to be string
|
|
||||||
if call.Args != nil && len(call.Args) > 1 {
|
if call.Args != nil && len(call.Args) > 1 {
|
||||||
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
||||||
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
||||||
@@ -255,7 +252,6 @@ func parseGetD(call *ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseGetND(call *ast.CallExpr) {
|
func parseGetND(call *ast.CallExpr) {
|
||||||
// Expect first 3 params to be string
|
|
||||||
if call.Args != nil && len(call.Args) > 2 {
|
if call.Args != nil && len(call.Args) > 2 {
|
||||||
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
||||||
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
||||||
@@ -278,7 +274,6 @@ func parseGetND(call *ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseGetC(call *ast.CallExpr) {
|
func parseGetC(call *ast.CallExpr) {
|
||||||
// Expect first 2 params to be string
|
|
||||||
if call.Args != nil && len(call.Args) > 1 {
|
if call.Args != nil && len(call.Args) > 1 {
|
||||||
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
||||||
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
||||||
@@ -296,7 +291,6 @@ func parseGetC(call *ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseGetNC(call *ast.CallExpr) {
|
func parseGetNC(call *ast.CallExpr) {
|
||||||
// Expect at least 4 params. 1, 2, and 3 as string
|
|
||||||
if call.Args != nil && len(call.Args) > 3 {
|
if call.Args != nil && len(call.Args) > 3 {
|
||||||
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
||||||
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
if lit1, ok := call.Args[1].(*ast.BasicLit); ok {
|
||||||
@@ -362,3 +356,16 @@ func parseGetNDC(call *ast.CallExpr) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseSetDomain(call *ast.CallExpr) {
|
||||||
|
if call.Args != nil && len(call.Args) == 1 {
|
||||||
|
if lit, ok := call.Args[0].(*ast.BasicLit); ok {
|
||||||
|
if lit.Kind == token.STRING {
|
||||||
|
cd, err := strconv.Unquote(lit.Value)
|
||||||
|
if err == nil {
|
||||||
|
currentDomain = cd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user