Skip to content

Commit

Permalink
checkup now accepts a -q to specify the qualifier
Browse files Browse the repository at this point in the history
1. pass -q to allow things like i18n.T(...)
2. fix the checkup test to pass the -q
3. fix various aspect of checkup code where it was not using correct Println abstraction
  • Loading branch information
maximilien committed Oct 19, 2015
1 parent bee2ed9 commit e8511ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
19 changes: 9 additions & 10 deletions cmds/checkup.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ func (cu *Checkup) Run() error {
sourceStrings, err := cu.findSourceStrings()

if err != nil {
fmt.Println(fmt.Sprintf("Couldn't find any source strings: %s", err.Error()))
cu.Println(fmt.Sprintf("Couldn't find any source strings: %s", err.Error()))
return err
}

locales := findTranslationFiles(".")

englishFiles := locales["en_US"]
if englishFiles == nil {
fmt.Println("Could not find an i18n file for locale: en_US")
cu.Println("Could not find an i18n file for locale: en_US")
return errors.New("Could not find an i18n file for locale: en_US")
}

englishStrings, err := cu.findI18nStrings(englishFiles)

if err != nil {
fmt.Println(fmt.Sprintf("Couldn't find the english strings: %s", err.Error()))
cu.Println(fmt.Sprintf("Couldn't find the english strings: %s", err.Error()))
return err
}

Expand All @@ -82,15 +82,15 @@ func (cu *Checkup) Run() error {
translatedStrings, err := cu.findI18nStrings(i18nFiles)

if err != nil {
fmt.Println(fmt.Sprintf("Couldn't get the strings from %s: %s", locale, err.Error()))
cu.Println(fmt.Sprintf("Couldn't get the strings from %s: %s", locale, err.Error()))
return err
}

err = cu.diffStrings("en_US", locale, englishStrings, translatedStrings)
}

if err == nil {
fmt.Printf("OK")
cu.Printf("OK")
}

return err
Expand Down Expand Up @@ -142,8 +142,7 @@ func (cu *Checkup) inspectFile(file string) (translatedStrings []string, err err
expr := x.Fun.(*ast.SelectorExpr)
if ident, ok := expr.X.(*ast.Ident); ok {
funName := expr.Sel.Name

if ident.Name == "i18n" && (funName == "T" || funName == "t") {
if ident.Name == cu.options.QualifierFlag && (funName == "T" || funName == "t") {
if stringArg, ok := x.Args[0].(*ast.BasicLit); ok {
translatedString, err := strconv.Unquote(stringArg.Value)
if err != nil {
Expand All @@ -170,7 +169,7 @@ func (cu *Checkup) findSourceStrings() (sourceStrings map[string]string, err err
for _, file := range files {
fileStrings, err := cu.inspectFile(file)
if err != nil {
fmt.Println("Error when inspecting go file: ", file)
cu.Println("Error when inspecting go file: ", file)
return sourceStrings, err
}

Expand Down Expand Up @@ -261,14 +260,14 @@ func (cu *Checkup) findI18nStrings(i18nFiles []string) (i18nStrings map[string]s
func (cu *Checkup) diffStrings(sourceNameOne, sourceNameTwo string, stringsOne, stringsTwo map[string]string) (err error) {
for key, _ := range stringsOne {
if stringsTwo[key] == "" {
fmt.Printf("\"%s\" exists in %s, but not in %s\n", key, sourceNameOne, sourceNameTwo)
cu.Printf("\"%s\" exists in %s, but not in %s\n", key, sourceNameOne, sourceNameTwo)
err = errors.New("Strings don't match")
}
}

for key, _ := range stringsTwo {
if stringsOne[key] == "" {
fmt.Printf("\"%s\" exists in %s, but not in %s\n", key, sourceNameTwo, sourceNameOne)
cu.Printf("\"%s\" exists in %s, but not in %s\n", key, sourceNameTwo, sourceNameOne)
err = errors.New("Strings don't match")
}
}
Expand Down
2 changes: 2 additions & 0 deletions common/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Options struct {
RootPathFlag string

InitCodeSnippetFilenameFlag string

QualifierFlag string
}

type I18nStringInfo struct {
Expand Down
5 changes: 4 additions & 1 deletion i18n4go/i18n4go.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/maximilien/i18n4go/common"
)

const VERSION = "v0.2.0"
const VERSION = "v0.2.2"

var options common.Options

Expand Down Expand Up @@ -248,6 +248,8 @@ func init() {

flag.StringVar(&options.InitCodeSnippetFilenameFlag, "init-code-snippet-filename", "", "[optional] the path to a file containing the template snippet for the code that is used for go-i18n initialization")

flag.StringVar(&options.QualifierFlag, "q", "", "[optional] the qualifier string that is used when using the T(...) function, default to nothing but could be set to `i18n` so that all calls would be: i18n.T(...)")

flag.Parse()
}

Expand Down Expand Up @@ -349,6 +351,7 @@ usage: i18n4go -c checkup
CHECKUP:
-c checkup the checkup command which ensures that the strings in code match strings in resource files and vice versa
-q the qualifier to use when calling the T(...), defaults to empty but can be used to set to something like i18n for example, such that, i18n.T(...) is used for T(...) function
FIXUP:
Expand Down
23 changes: 13 additions & 10 deletions integration/checkup/checkup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,13 @@ var _ = Describe("checkup", func() {
}
})

JustBeforeEach(func() {
err = os.Chdir(fixturesPath)
if err != nil {
fmt.Println("Could not change to fixtures directory")
panic(err.Error())
}

session = Runi18n("-c", "checkup")
})

Context("When there are no problems", func() {
BeforeEach(func() {
fixturesPath = filepath.Join("..", "..", "test_fixtures", "checkup", "allgood")
err = os.Chdir(fixturesPath)
Ω(err).ToNot(HaveOccurred(), "Could not change to fixtures directory")

session = Runi18n("-c", "checkup", "-v")
})

It("returns 0", func() {
Expand All @@ -63,20 +57,29 @@ var _ = Describe("checkup", func() {
Context("when the i18n package is fully qualified", func() {
BeforeEach(func() {
fixturesPath = filepath.Join("..", "..", "test_fixtures", "checkup", "qualified")
err = os.Chdir(fixturesPath)
Ω(err).ToNot(HaveOccurred(), "Could not change to fixtures directory")

session = Runi18n("-c", "checkup", "-v", "-q", "i18n")
})

It("returns 0", func() {
Ω(session.ExitCode()).Should(Equal(0))
})

It("prints a reassuring message", func() {
session = Runi18n("-c", "checkup", "-v", "-q", "i18n")
Ω(session).Should(Say("OK"))
})
})

Context("When there are problems", func() {
BeforeEach(func() {
fixturesPath = filepath.Join("..", "..", "test_fixtures", "checkup", "notsogood")
err = os.Chdir(fixturesPath)
Ω(err).ToNot(HaveOccurred(), "Could not change to fixtures directory")

session = Runi18n("-c", "checkup", "-v")
})

It("shows all inconsistent strings and returns 1", func() {
Expand Down

0 comments on commit e8511ee

Please sign in to comment.