Skip to content

Commit

Permalink
Merge pull request #98 from maximilien/feat/92-cobra-merge-string-sho…
Browse files Browse the repository at this point in the history
…w-missing-strings
  • Loading branch information
maximilien authored Jul 14, 2023
2 parents 855df91 + 1186ee5 commit 7f2de0e
Show file tree
Hide file tree
Showing 6 changed files with 253 additions and 100 deletions.
12 changes: 7 additions & 5 deletions cmds/merge_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,29 @@ type mergeStrings struct {
Directory string
}

func NewMergeStrings(options common.Options) *mergeStrings {
func NewMergeStrings(options *common.Options) *mergeStrings {
return &mergeStrings{
options: options,
options: *options,
I18nStringInfos: []common.I18nStringInfo{},
Recurse: options.RecurseFlag,
SourceLanguage: options.SourceLanguageFlag,
Directory: options.DirnameFlag,
}
}

// NewMergeStringsCommand implements 'i18n merge-strings' command
func NewMergeStringsCommand(p *I18NParams, options common.Options) *cobra.Command {
// NewMergeStringsCommand implements 'i18n4go merge-strings' command
func NewMergeStringsCommand(options *common.Options) *cobra.Command {
mergeStringsCmd := &cobra.Command{
Use: "merge-strings",
Short: "Merge translation strings",
RunE: func(cmd *cobra.Command, args []string) error {
return NewMergeStrings(options).Run()
},
}
mergeStringsCmd.Flags().BoolVarP(&options.RecurseFlag, "recursive", "r", false, "recursively extract strings from all files in the same directory as filename or dirName")
mergeStringsCmd.Flags().StringVarP(&options.SourceLanguageFlag, "source-language", "s", "en", "the source language of the file, typically also part of the file name, e.g., \"en_US\"")

// TODO: setup options and params for Cobra command here using common.Options
mergeStringsCmd.Flags().StringVarP(&options.DirnameFlag, "directory", "d", "", "the dir name for which all .go files will have their strings extracted")

return mergeStringsCmd
}
Expand Down
9 changes: 6 additions & 3 deletions cmds/show_missing_strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ type showMissingStrings struct {
Directory string
}

func NewShowMissingStrings(options common.Options) *showMissingStrings {
func NewShowMissingStrings(options *common.Options) *showMissingStrings {
return &showMissingStrings{
options: options,
options: *options,
Directory: options.DirnameFlag,
I18nStringsFilename: options.I18nStringsFilenameFlag,
TranslatedStrings: []string{},
}
}

// NewShowMissingStringsCommand implements 'i18n show-missing-strings' command
func NewShowMissingStringsCommand(p *I18NParams, options common.Options) *cobra.Command {
func NewShowMissingStringsCommand(options *common.Options) *cobra.Command {
showMissingStringsCmd := &cobra.Command{
Use: "show-missing-strings",
Short: "Shows missing strings in translations",
Expand All @@ -59,6 +59,9 @@ func NewShowMissingStringsCommand(p *I18NParams, options common.Options) *cobra.
},
}

showMissingStringsCmd.Flags().StringVarP(&options.DirnameFlag, "directory", "d", "", "the directory containing the go files to validate")
showMissingStringsCmd.Flags().StringVar(&options.I18nStringsFilenameFlag, "i18n-strings-filename", "", "a JSON file with the strings that should be i18n enabled, typically the output of -extract-strings command")

// TODO: setup options and params for Cobra command here using common.Options

return showMissingStringsCmd
Expand Down
6 changes: 4 additions & 2 deletions i18n4go/i18n4go.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func rootCobraCmd(opts common.Options) {
cmd.AddCommand(cmds.NewRewritePackageCommand(&opts))
cmd.AddCommand(cmds.NewVerifyStringsCommand(&opts))
cmd.AddCommand(cmds.NewFixupCommand(&opts))
cmd.AddCommand(cmds.NewMergeStringsCommand(&opts))
cmd.AddCommand(cmds.NewShowMissingStringsCommand(&opts))

if err := cmd.Execute(); err != nil {
fmt.Println(err.Error())
Expand Down Expand Up @@ -171,7 +173,7 @@ func mergeStringsCmd() {
return
}

mergeStrings := cmds.NewMergeStrings(options)
mergeStrings := cmds.NewMergeStrings(&options)

startTime := time.Now()

Expand All @@ -191,7 +193,7 @@ func showMissingStringsCmd() {
return
}

showMissingStrings := cmds.NewShowMissingStrings(options)
showMissingStrings := cmds.NewShowMissingStrings(&options)

startTime := time.Now()

Expand Down
159 changes: 114 additions & 45 deletions integration/merge_strings/d_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,67 +40,136 @@ var _ = Describe("merge-strings -d dirName", func() {
expectedFilesPath = filepath.Join(fixturesPath, "d_option", "expected_output")
})

Context("can combine multiple language files", func() {
Context("merging en files in input_files path", func() {
BeforeEach(func() {
session := Runi18n("-c", "merge-strings", "-v", "-d", filepath.Join(inputFilesPath), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
Context("Using legacy commands", func() {
Context("can combine multiple language files", func() {
Context("merging en files in input_files path", func() {
BeforeEach(func() {
session := Runi18n("-c", "merge-strings", "-v", "-d", filepath.Join(inputFilesPath), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(inputFilesPath, "en.all.json"),
)
})

It("creates an en.all.json that contains translations from both files", func() {
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath, "en.all.json"),
GetFilePath(inputFilesPath, "en.all.json"),
)
})

It("creates an en.all.json for which the translation strings order are stable", func() {
expectedFilePath := GetFilePath(expectedFilesPath, "en.all.json")
actualFilePath := GetFilePath(inputFilesPath, "en.all.json")

expectedBytes, err := ioutil.ReadFile(expectedFilePath)
Ω(err).Should(BeNil())
Ω(expectedBytes).ShouldNot(BeNil())

actualBytes, err := ioutil.ReadFile(actualFilePath)
Ω(err).Should(BeNil())
Ω(actualBytes).ShouldNot(BeNil())

Ω(string(expectedBytes)).Should(Equal(string(actualBytes)))
})
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(inputFilesPath, "en.all.json"),
)
})
Context("merging en files in input_files/reordered path", func() {
BeforeEach(func() {
session := Runi18n("-c", "merge-strings", "-v", "-d", filepath.Join(inputFilesPath, "reordered"), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})

It("creates an en.all.json that contains translations from both files", func() {
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath, "en.all.json"),
GetFilePath(inputFilesPath, "en.all.json"),
)
})
AfterEach(func() {
RemoveAllFiles(
GetFilePath(filepath.Join(inputFilesPath, "reordered"), "en.all.json"),
)
})

It("creates an en.all.json for which the translation strings order are stable", func() {
expectedFilePath := GetFilePath(expectedFilesPath, "en.all.json")
actualFilePath := GetFilePath(inputFilesPath, "en.all.json")
It("creates an en.all.json keeping the stable order", func() {
expectedFilePath := GetFilePath(expectedFilesPath, "en.all.json")
actualFilePath := GetFilePath(filepath.Join(inputFilesPath, "reordered"), "en.all.json")

expectedBytes, err := ioutil.ReadFile(expectedFilePath)
Ω(err).Should(BeNil())
Ω(expectedBytes).ShouldNot(BeNil())
expectedBytes, err := ioutil.ReadFile(expectedFilePath)
Ω(err).Should(BeNil())
Ω(expectedBytes).ShouldNot(BeNil())

actualBytes, err := ioutil.ReadFile(actualFilePath)
Ω(err).Should(BeNil())
Ω(actualBytes).ShouldNot(BeNil())
actualBytes, err := ioutil.ReadFile(actualFilePath)
Ω(err).Should(BeNil())
Ω(actualBytes).ShouldNot(BeNil())

Ω(string(expectedBytes)).Should(Equal(string(actualBytes)))
Ω(string(expectedBytes)).Should(Equal(string(actualBytes)))
})
})
})
})

Context("merging en files in input_files/reordered path", func() {
BeforeEach(func() {
session := Runi18n("-c", "merge-strings", "-v", "-d", filepath.Join(inputFilesPath, "reordered"), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
Context("Using cobra commands", func() {
Context("can combine multiple language files", func() {
Context("merging en files in input_files path", func() {
BeforeEach(func() {
session := Runi18n("merge-strings", "-v", "-d", filepath.Join(inputFilesPath), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(inputFilesPath, "en.all.json"),
)
})

It("creates an en.all.json that contains translations from both files", func() {
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath, "en.all.json"),
GetFilePath(inputFilesPath, "en.all.json"),
)
})

It("creates an en.all.json for which the translation strings order are stable", func() {
expectedFilePath := GetFilePath(expectedFilesPath, "en.all.json")
actualFilePath := GetFilePath(inputFilesPath, "en.all.json")

expectedBytes, err := ioutil.ReadFile(expectedFilePath)
Ω(err).Should(BeNil())
Ω(expectedBytes).ShouldNot(BeNil())

actualBytes, err := ioutil.ReadFile(actualFilePath)
Ω(err).Should(BeNil())
Ω(actualBytes).ShouldNot(BeNil())

Ω(string(expectedBytes)).Should(Equal(string(actualBytes)))
})
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(filepath.Join(inputFilesPath, "reordered"), "en.all.json"),
)
})
Context("merging en files in input_files/reordered path", func() {
BeforeEach(func() {
session := Runi18n("merge-strings", "-v", "-d", filepath.Join(inputFilesPath, "reordered"), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(filepath.Join(inputFilesPath, "reordered"), "en.all.json"),
)
})

It("creates an en.all.json keeping the stable order", func() {
expectedFilePath := GetFilePath(expectedFilesPath, "en.all.json")
actualFilePath := GetFilePath(filepath.Join(inputFilesPath, "reordered"), "en.all.json")
It("creates an en.all.json keeping the stable order", func() {
expectedFilePath := GetFilePath(expectedFilesPath, "en.all.json")
actualFilePath := GetFilePath(filepath.Join(inputFilesPath, "reordered"), "en.all.json")

expectedBytes, err := ioutil.ReadFile(expectedFilePath)
Ω(err).Should(BeNil())
Ω(expectedBytes).ShouldNot(BeNil())
expectedBytes, err := ioutil.ReadFile(expectedFilePath)
Ω(err).Should(BeNil())
Ω(expectedBytes).ShouldNot(BeNil())

actualBytes, err := ioutil.ReadFile(actualFilePath)
Ω(err).Should(BeNil())
Ω(actualBytes).ShouldNot(BeNil())
actualBytes, err := ioutil.ReadFile(actualFilePath)
Ω(err).Should(BeNil())
Ω(actualBytes).ShouldNot(BeNil())

Ω(string(expectedBytes)).Should(Equal(string(actualBytes)))
Ω(string(expectedBytes)).Should(Equal(string(actualBytes)))
})
})
})
})
Expand Down
67 changes: 48 additions & 19 deletions integration/merge_strings/r_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,57 @@ var _ = Describe("merge-strings -d dirName -r", func() {
expectedFilesPath = filepath.Join(fixturesPath, "r_option", "expected_output")
})

Context("can combine multiple language files per directory", func() {
BeforeEach(func() {
session := Runi18n("-c", "merge-strings", "-v", "-r", "-d", filepath.Join(inputFilesPath), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})
Context("Using legacy commands", func() {
Context("can combine multiple language files per directory", func() {
BeforeEach(func() {
session := Runi18n("-c", "merge-strings", "-v", "-r", "-d", filepath.Join(inputFilesPath), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(inputFilesPath, "en.all.json"),
GetFilePath(inputFilesPath+"/sub", "en.all.json"),
)
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(inputFilesPath, "en.all.json"),
GetFilePath(inputFilesPath+"/sub", "en.all.json"),
)
It("en.all.json contains translations from both files", func() {
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath, "en.all.json"),
GetFilePath(inputFilesPath, "en.all.json"),
)
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath+"/sub", "en.all.json"),
GetFilePath(inputFilesPath+"/sub", "en.all.json"),
)
})
})
})

Context("Using cobra commands", func() {
Context("can combine multiple language files per directory", func() {
BeforeEach(func() {
session := Runi18n("merge-strings", "-v", "-r", "-d", filepath.Join(inputFilesPath), "--source-language", "en")
Ω(session.ExitCode()).Should(Equal(0))
})

AfterEach(func() {
RemoveAllFiles(
GetFilePath(inputFilesPath, "en.all.json"),
GetFilePath(inputFilesPath+"/sub", "en.all.json"),
)
})

It("en.all.json contains translations from both files", func() {
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath, "en.all.json"),
GetFilePath(inputFilesPath, "en.all.json"),
)
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath+"/sub", "en.all.json"),
GetFilePath(inputFilesPath+"/sub", "en.all.json"),
)
It("en.all.json contains translations from both files", func() {
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath, "en.all.json"),
GetFilePath(inputFilesPath, "en.all.json"),
)
CompareExpectedToGeneratedTraslationJson(
GetFilePath(expectedFilesPath+"/sub", "en.all.json"),
GetFilePath(inputFilesPath+"/sub", "en.all.json"),
)
})
})
})

Expand Down
Loading

0 comments on commit 7f2de0e

Please sign in to comment.