Skip to content

Commit b7d2111

Browse files
authored
Merge pull request #162 from maximilien/refactor/149-fixup-checkup
feat(fixup): added source and resource flags to set source/translation directories
2 parents f5c3783 + b9ccb24 commit b7d2111

File tree

11 files changed

+2013
-1623
lines changed

11 files changed

+2013
-1623
lines changed

bin/validate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if [[ -z $(command -v go-bindata) ]]; then
2727
fi
2828

2929
pushd $PROJ_DIR >/dev/null
30-
go-bindata -pkg resources -o resources -o i18n4go/resources/i18n_resources.go -nocompress i18n4go/i18n/resources/*.json
30+
go-bindata -pkg i18n -o i18n4go/i18n/i18n_resources.go -nocompress i18n4go/i18n/resources/*.json
3131
echo -e "\nCompleted."
3232

3333
popd >/dev/null

i18n4go/cmds/checkup.go

Lines changed: 10 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,8 @@ import (
2020
"io/ioutil"
2121
"path/filepath"
2222
"regexp"
23-
"strconv"
2423
"strings"
2524

26-
"go/ast"
27-
"go/parser"
28-
"go/token"
29-
3025
"github.com/spf13/cobra"
3126

3227
"github.com/maximilien/i18n4go/i18n4go/common"
@@ -52,7 +47,7 @@ func NewCheckup(options *common.Options) *Checkup {
5247
func NewCheckupCommand(options *common.Options) *cobra.Command {
5348
checkupCmd := &cobra.Command{
5449
Use: "checkup",
55-
Short: i18n.T("Checks the transated files"),
50+
Short: i18n.T("Checks the translated files"),
5651
RunE: func(cmd *cobra.Command, args []string) error {
5752
return NewCheckup(options).Run()
5853
},
@@ -68,15 +63,15 @@ func (cu *Checkup) Options() common.Options {
6863
return cu.options
6964
}
7065

71-
func (cu *Checkup) Println(a ...interface{}) (int, error) {
66+
func (cu *Checkup) Println(a ...any) (int, error) {
7267
if cu.options.VerboseFlag {
7368
return fmt.Println(a...)
7469
}
7570

7671
return 0, nil
7772
}
7873

79-
func (cu *Checkup) Printf(msg string, a ...interface{}) (int, error) {
74+
func (cu *Checkup) Printf(msg string, a ...any) (int, error) {
8075
if cu.options.VerboseFlag {
8176
return fmt.Printf(msg, a...)
8277
}
@@ -89,7 +84,7 @@ func (cu *Checkup) Run() error {
8984
sourceStrings, err := cu.findSourceStrings()
9085

9186
if err != nil {
92-
cu.Println(i18n.T("Couldn't find any source strings: {{.Arg0}}", map[string]interface{}{"Arg0": err.Error()}))
87+
cu.Println(i18n.T("Couldn't find any source strings: {{.Arg0}}", map[string]any{"Arg0": err.Error()}))
9388
return err
9489
}
9590

@@ -104,7 +99,7 @@ func (cu *Checkup) Run() error {
10499
englishStrings, err := cu.findI18nStrings(englishFiles)
105100

106101
if err != nil {
107-
cu.Println(i18n.T("Couldn't find the english strings: {{.Arg0}}", map[string]interface{}{"Arg0": err.Error()}))
102+
cu.Println(i18n.T("Couldn't find the english strings: {{.Arg0}}", map[string]any{"Arg0": err.Error()}))
108103
return err
109104
}
110105

@@ -118,7 +113,7 @@ func (cu *Checkup) Run() error {
118113
translatedStrings, err := cu.findI18nStrings(i18nFiles)
119114

120115
if err != nil {
121-
cu.Println(i18n.T("Couldn't get the strings from {{.Arg0}}: {{.Arg1}}", map[string]interface{}{"Arg0": locale, "Arg1": err.Error()}))
116+
cu.Println(i18n.T("Couldn't get the strings from {{.Arg0}}: {{.Arg1}}", map[string]any{"Arg0": locale, "Arg1": err.Error()}))
122117
return err
123118
}
124119

@@ -150,125 +145,12 @@ func getGoFiles(dir string) (files []string) {
150145
return
151146
}
152147

153-
func (cu *Checkup) inspectAssignStmt(stmtMap map[string][]ast.AssignStmt, node *ast.AssignStmt) {
154-
// use a hashmap for defined variables to a list of reassigned variables sharing the same var name
155-
if assignStmt, okIdent := node.Lhs[0].(*ast.Ident); okIdent {
156-
varName := assignStmt.Name
157-
if node.Tok == token.DEFINE {
158-
stmtMap[varName] = []ast.AssignStmt{}
159-
} else if node.Tok == token.ASSIGN {
160-
if _, exists := stmtMap[varName]; exists {
161-
stmtMap[varName] = append(stmtMap[varName], *node)
162-
}
163-
}
164-
}
165-
}
166-
167-
func (cu *Checkup) inspectStmt(translatedStrings []string, stmtMap map[string][]ast.AssignStmt, node ast.AssignStmt) []string {
168-
if strStmtArg, ok := node.Rhs[0].(*ast.BasicLit); ok {
169-
varName := node.Lhs[0].(*ast.Ident).Name
170-
translatedString, err := strconv.Unquote(strStmtArg.Value)
171-
if err != nil {
172-
panic(err.Error())
173-
}
174-
translatedStrings = append(translatedStrings, translatedString)
175-
// apply all translation ids from reassigned variables
176-
if _, exists := stmtMap[varName]; exists {
177-
for _, assignStmt := range stmtMap[varName] {
178-
strVarVal := assignStmt.Rhs[0].(*ast.BasicLit).Value
179-
translatedString, err := strconv.Unquote(strVarVal)
180-
if err != nil {
181-
panic(err.Error())
182-
}
183-
translatedStrings = append(translatedStrings, translatedString)
184-
185-
}
186-
}
187-
}
188-
189-
return translatedStrings
190-
}
191-
192-
func (cu *Checkup) inspectTFunc(translatedStrings []string, stmtMap map[string][]ast.AssignStmt, node ast.CallExpr) []string {
193-
if stringArg, ok := node.Args[0].(*ast.BasicLit); ok {
194-
translatedString, err := strconv.Unquote(stringArg.Value)
195-
if err != nil {
196-
panic(err.Error())
197-
}
198-
translatedStrings = append(translatedStrings, translatedString)
199-
}
200-
if idt, okIdt := node.Args[0].(*ast.Ident); okIdt {
201-
if obj := idt.Obj; obj != nil {
202-
if stmtArg, okStmt := obj.Decl.(*ast.AssignStmt); okStmt {
203-
translatedStrings = cu.inspectStmt(translatedStrings, stmtMap, *stmtArg)
204-
}
205-
}
206-
}
207-
208-
return translatedStrings
209-
}
210-
211-
func (cu *Checkup) inspectCallExpr(translatedStrings []string, stmtMap map[string][]ast.AssignStmt, node *ast.CallExpr) []string {
212-
switch node.Fun.(type) {
213-
case *ast.Ident:
214-
funName := node.Fun.(*ast.Ident).Name
215-
// inspect any T() or t() method calls
216-
if funName == "T" || funName == "t" {
217-
translatedStrings = cu.inspectTFunc(translatedStrings, stmtMap, *node)
218-
}
219-
220-
case *ast.SelectorExpr:
221-
expr := node.Fun.(*ast.SelectorExpr)
222-
if ident, ok := expr.X.(*ast.Ident); ok {
223-
funName := expr.Sel.Name
224-
// inspect any <MODULE>.T() or <MODULE>.t() method calls (eg. i18n.T())
225-
if ident.Name == cu.options.QualifierFlag && (funName == "T" || funName == "t") {
226-
translatedStrings = cu.inspectTFunc(translatedStrings, stmtMap, *node)
227-
}
228-
}
229-
default:
230-
//Skip!
231-
}
232-
233-
return translatedStrings
234-
}
235-
236-
func (cu *Checkup) inspectFile(file string) (translatedStrings []string, err error) {
237-
defineAssignStmtMap := make(map[string][]ast.AssignStmt)
238-
fset := token.NewFileSet()
239-
astFile, err := parser.ParseFile(fset, file, nil, parser.AllErrors)
240-
if err != nil {
241-
cu.Println(err)
242-
return
243-
}
244-
245-
ast.Inspect(astFile, func(n ast.Node) bool {
246-
switch x := n.(type) {
247-
case *ast.AssignStmt:
248-
// inspect any potential translation string in defined / assigned statement nodes
249-
// add node to map if variable contains a translation string
250-
// eg: translation := "Hello {{.FirstName}}"
251-
// T(translation)
252-
// translation = "Hello {{.LastName}}"
253-
// T(translation)
254-
cu.inspectAssignStmt(defineAssignStmtMap, x)
255-
case *ast.CallExpr:
256-
// inspect any T()/t() or <MODULE>.T()/<MODULE>.t() (eg. i18n.T()) method calls using map
257-
/// then retrieve a list of translation strings that were passed into method
258-
translatedStrings = cu.inspectCallExpr(translatedStrings, defineAssignStmtMap, x)
259-
}
260-
return true
261-
})
262-
263-
return
264-
}
265-
266148
func (cu *Checkup) findSourceStrings() (sourceStrings map[string]string, err error) {
267149
sourceStrings = make(map[string]string)
268150
files := getGoFiles(".")
269151

270152
for _, file := range files {
271-
fileStrings, err := cu.inspectFile(file)
153+
fileStrings, err := common.InspectFile(file, cu.options)
272154
if err != nil {
273155
cu.Println(i18n.T("Error when inspecting go file: "), file)
274156
return sourceStrings, err
@@ -294,7 +176,7 @@ func getI18nFile(locale, dir string) (filePath string) {
294176
name := fileInfo.Name()
295177

296178
// assume the file path is a json file and the path contains the locale
297-
if strings.HasSuffix(name, ".json") && strings.Contains(name, fmt.Sprintf("{{.Arg0}}.", map[string]interface{}{"Arg0": locale})) {
179+
if strings.HasSuffix(name, ".json") && strings.Contains(name, fmt.Sprintf("{{.Arg0}}.", map[string]any{"Arg0": locale})) {
298180
filePath = filepath.Join(dir, fileInfo.Name())
299181
break
300182
}
@@ -380,14 +262,14 @@ func (cu *Checkup) findI18nStrings(i18nFiles []string) (i18nStrings map[string]s
380262
func (cu *Checkup) diffStrings(sourceNameOne, sourceNameTwo string, stringsOne, stringsTwo map[string]string) (err error) {
381263
for key, _ := range stringsOne {
382264
if stringsTwo[key] == "" {
383-
cu.Printf(i18n.T("\"{{.Arg0}}\" exists in {{.Arg1}}, but not in {{.Arg2}}\n", map[string]interface{}{"Arg0": key, "Arg1": sourceNameOne, "Arg2": sourceNameTwo}))
265+
cu.Printf(i18n.T("\"{{.Arg0}}\" exists in {{.Arg1}}, but not in {{.Arg2}}\n", map[string]any{"Arg0": key, "Arg1": sourceNameOne, "Arg2": sourceNameTwo}))
384266
err = errors.New(i18n.T("Strings don't match"))
385267
}
386268
}
387269

388270
for key, _ := range stringsTwo {
389271
if stringsOne[key] == "" {
390-
cu.Printf(i18n.T("\"{{.Arg0}}\" exists in {{.Arg1}}, but not in {{.Arg2}}\n", map[string]interface{}{"Arg0": key, "Arg1": sourceNameTwo, "Arg2": sourceNameOne}))
272+
cu.Printf(i18n.T("\"{{.Arg0}}\" exists in {{.Arg1}}, but not in {{.Arg2}}\n", map[string]any{"Arg0": key, "Arg1": sourceNameTwo, "Arg2": sourceNameOne}))
391273
err = errors.New(i18n.T("Strings don't match"))
392274
}
393275
}

i18n4go/cmds/create_translations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func NewCreateTranslations(options *common.Options) *createTranslations {
7575
func NewCreateTranslationsCommand(options *common.Options) *cobra.Command {
7676
createTranslationsCmd := &cobra.Command{
7777
Use: "create-translations",
78-
Short: i18n.T("Creates the transation files"),
78+
Short: i18n.T("Creates the translation files"),
7979
RunE: func(cmd *cobra.Command, args []string) error {
8080
return NewCreateTranslations(options).Run()
8181
},

0 commit comments

Comments
 (0)