diff --git a/go.mod b/go.mod index de3317f..a98ed04 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/maximilien/i18n4go go 1.18 require ( + github.com/go-bindata/go-bindata/v3 v3.1.3 github.com/nicksnyder/go-i18n/v2 v2.4.0 github.com/onsi/ginkgo v1.16.5 github.com/onsi/gomega v1.33.1 @@ -16,10 +17,15 @@ require ( github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/kisielk/errcheck v1.2.0 // indirect github.com/nxadm/tail v1.4.8 // indirect github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f // indirect + golang.org/x/mod v0.17.0 // indirect golang.org/x/net v0.24.0 // indirect + golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.19.0 // indirect + golang.org/x/tools v0.20.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 649d3cc..9a92713 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-bindata/go-bindata/v3 v3.1.3 h1:F0nVttLC3ws0ojc7p60veTurcOm//D4QBODNM7EGrCI= +github.com/go-bindata/go-bindata/v3 v3.1.3/go.mod h1:1/zrpXsLD8YDIbhZRqXzm1Ghc7NhEvIN9+Z6R5/xH4I= github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I= github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= @@ -27,6 +29,8 @@ github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQN github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -60,7 +64,11 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f h1:J5lckAjkw6qYlOZNj90mLYNTEKDvWeuc1yieZ8qUzUE= +golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f/go.mod h1:5qLYkcX4OjUUV8bRuDixDT3tpyyb+LUpUlRWLxfhWrs= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -71,6 +79,8 @@ golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -87,9 +97,12 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191125144606-a911d9008d1f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY= +golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/i18n4go/cmds/rewrite_package.go b/i18n4go/cmds/rewrite_package.go index 23d1625..48993ca 100644 --- a/i18n4go/cmds/rewrite_package.go +++ b/i18n4go/cmds/rewrite_package.go @@ -27,6 +27,7 @@ import ( "go/token" "io/ioutil" + "github.com/go-bindata/go-bindata/v3" "github.com/maximilien/i18n4go/i18n4go/common" "github.com/spf13/cobra" @@ -48,7 +49,9 @@ import ( var T i18n.TranslateFunc func init() { - T = i18n.Init(__FULL_IMPORT_PATH__, i18n.GetResourcesPath()) + T = i18n.Init(__FULL_IMPORT_PATH__, i18n.GetResourcesPath(), func(asset string) ([]byte, error) { + return Asset(asset) + }) }` ) @@ -59,6 +62,8 @@ type rewritePackage struct { OutputDirname string I18nStringsFilename string I18nStringsDirname string + I18nStringsFilePaths []string + RootPackageName string RootPath string InitCodeSnippetFilename string @@ -148,21 +153,48 @@ func (rp *rewritePackage) Printf(msg string, a ...interface{}) (int, error) { } func (rp *rewritePackage) Run() error { - var err error if rp.options.FilenameFlag != "" { - if err = rp.loadStringsToBeTranslated(rp.I18nStringsFilename); err != nil { + if err := rp.loadStringsToBeTranslated(rp.I18nStringsFilename); err != nil { return err } - err = rp.processFilename(rp.options.FilenameFlag) + if err := rp.processFilename(rp.options.FilenameFlag); err != nil { + return err + } + rp.I18nStringsFilePaths = []string{rp.I18nStringsFilename} } else { - err = rp.processDir(rp.options.DirnameFlag, rp.options.RecurseFlag) + if err := rp.processDir(rp.options.DirnameFlag, rp.options.RecurseFlag); err != nil { + return err + } + } + + if err := rp.generateBindataFromI18nStrings(); err != nil { + return err } rp.Println() rp.Println("Total files parsed:", rp.TotalFiles) rp.Println("Total rewritten strings:", rp.TotalStrings) - return err + return nil +} + +func (rp *rewritePackage) generateBindataFromI18nStrings() error { + if rp.RootPackageName != "" { + bindataConfig := bindata.NewConfig() + bindataConfig.Package = rp.RootPackageName + bindataConfig.Output = filepath.Join(rp.OutputDirname, "i18n_resources.go") + for _, i18nFilePath := range rp.I18nStringsFilePaths { + bindataConfig.Input = append(bindataConfig.Input, bindata.InputConfig{ + Path: filepath.Clean(i18nFilePath), + Recursive: false, + }) + } + err := bindata.Translate(bindataConfig) + if err != nil { + return err + } + } + return nil } func (rp *rewritePackage) loadStringsToBeTranslated(fileName string) error { @@ -202,6 +234,7 @@ func (rp *rewritePackage) processDir(dirName string, recursive bool) error { } rp.I18nStringsFilename = filepath.Join(rp.I18nStringsDirname, i18nFilename) + rp.I18nStringsFilePaths = append(rp.I18nStringsFilePaths, rp.I18nStringsFilename) rp.Printf("i18n4go: loading JSON strings from file: %s\n", rp.I18nStringsFilename) if err := rp.loadStringsToBeTranslated(rp.I18nStringsFilename); err != nil { rp.Println("i18n4go: WARNING could not find JSON file:", rp.I18nStringsFilename, err.Error()) @@ -227,6 +260,7 @@ func (rp *rewritePackage) resetProcessing() { rp.ExtractedStrings = nil rp.UpdatedExtractedStrings = nil rp.I18nStringsFilename = "" + rp.RootPackageName = "" rp.SaveExtractedStrings = false } @@ -269,6 +303,10 @@ func (rp *rewritePackage) processFilename(fileName string) error { rp.OutputDirname = filepath.Dir(fileName) } + if rp.RootPackageName == "" { + rp.RootPackageName = astFile.Name.Name + } + outputDir := filepath.Join(rp.OutputDirname, filepath.Dir(rp.relativePathForFile(fileName))) err = rp.addInitFuncToPackage(astFile.Name.Name, outputDir, importPath) if err != nil { diff --git a/i18n4go/i18n/init.go b/i18n4go/i18n/init.go index 8c93ab2..59d22da 100644 --- a/i18n4go/i18n/init.go +++ b/i18n4go/i18n/init.go @@ -16,7 +16,6 @@ package i18n import ( "encoding/json" - "errors" "fmt" "io/ioutil" "os" @@ -35,6 +34,7 @@ const ( ) type TranslateFunc func(translateID string, args ...interface{}) string +type AssetFunc func(asset string) ([]byte, error) var SUPPORTED_LOCALES = map[string]string{ "de": "de_DE", @@ -49,7 +49,7 @@ var SUPPORTED_LOCALES = map[string]string{ "zh": "zh_CN", } var ( - RESOUCES_PATH = filepath.Join("cf", "i18n", "resources") + RESOUCES_PATH = filepath.Join("i18n", "resources") bundle *go_i18n.Bundle ) @@ -64,16 +64,16 @@ func init() { } } -func Init(packageName string, i18nDirname string) TranslateFunc { - userLocale, err := initWithUserLocale(packageName, i18nDirname) +func Init(packageName string, i18nDirname string, assetFn AssetFunc) TranslateFunc { + userLocale, err := initWithUserLocale(packageName, i18nDirname, assetFn) if err != nil { - userLocale = mustLoadDefaultLocale(packageName, i18nDirname) + userLocale = mustLoadDefaultLocale(packageName, i18nDirname, assetFn) } return Tfunc(userLocale, DEFAULT_LOCALE) } -func initWithUserLocale(packageName, i18nDirname string) (string, error) { +func initWithUserLocale(packageName, i18nDirname string, assetFn AssetFunc) (string, error) { userLocale, err := jibber_jabber.DetectIETF() if err != nil { userLocale = DEFAULT_LOCALE @@ -85,7 +85,7 @@ func initWithUserLocale(packageName, i18nDirname string) (string, error) { } userLocale = strings.Replace(userLocale, "-", "_", 1) - err = loadFromAsset(packageName, i18nDirname, userLocale, language) + err = loadFromAsset(packageName, i18nDirname, userLocale, language, assetFn) if err != nil { locale := SUPPORTED_LOCALES[language] if locale == "" { @@ -93,16 +93,16 @@ func initWithUserLocale(packageName, i18nDirname string) (string, error) { } else { userLocale = locale } - err = loadFromAsset(packageName, i18nDirname, userLocale, language) + err = loadFromAsset(packageName, i18nDirname, userLocale, language, assetFn) } return userLocale, err } -func mustLoadDefaultLocale(packageName, i18nDirname string) string { +func mustLoadDefaultLocale(packageName, i18nDirname string, assetFn AssetFunc) string { userLocale := DEFAULT_LOCALE - err := loadFromAsset(packageName, i18nDirname, DEFAULT_LOCALE, DEFAULT_LANGUAGE) + err := loadFromAsset(packageName, i18nDirname, DEFAULT_LOCALE, DEFAULT_LANGUAGE, assetFn) if err != nil { panic("Could not load en_US language files. God save the queen. " + err.Error()) } @@ -110,17 +110,17 @@ func mustLoadDefaultLocale(packageName, i18nDirname string) string { return userLocale } -func loadFromAsset(packageName, assetPath, locale, language string) error { - assetName := locale + ".all.json" - assetKey := filepath.Join(assetPath, language, packageName, assetName) +func loadFromAsset(packageName, assetPath, locale, language string, assetFn AssetFunc) error { + assetName := "all." + locale + ".json" + assetKey := filepath.Join(assetPath, packageName, assetName) - byteArray, err := resources.Asset(assetKey) + byteArray, err := assetFn(assetKey) if err != nil { return err } if len(byteArray) == 0 { - return errors.New(fmt.Sprintf("Could not load i18n asset: %v", assetKey)) + return fmt.Errorf("Could not load i18n asset: %v", assetKey) } tmpDir, err := ioutil.TempDir("", "i18n4go_res") diff --git a/integration/rewrite_package/d_option_test.go b/integration/rewrite_package/d_option_test.go index f822bb8..ea4c932 100644 --- a/integration/rewrite_package/d_option_test.go +++ b/integration/rewrite_package/d_option_test.go @@ -15,7 +15,6 @@ package rewrite_package_test import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -46,7 +45,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -68,13 +67,13 @@ var _ = Describe("rewrite-package -d dirname -r", func() { It("adds T() callExprs wrapping string literals", func() { expectedOutputFile := filepath.Join(expectedFilesPath, "test.go") - bytes, err := ioutil.ReadFile(expectedOutputFile) + bytes, err := os.ReadFile(expectedOutputFile) Ω(err).ShouldNot(HaveOccurred()) expectedOutput := string(bytes) generatedOutputFile := filepath.Join(outputDir, "test.go") - bytes, err = ioutil.ReadFile(generatedOutputFile) + bytes, err = os.ReadFile(generatedOutputFile) Ω(err).ShouldNot(HaveOccurred()) actualOutput := string(bytes) @@ -83,13 +82,13 @@ var _ = Describe("rewrite-package -d dirname -r", func() { It("recurses to files in nested dirs", func() { expectedOutputFile := filepath.Join(expectedFilesPath, "nested_dir", "test.go") - bytes, err := ioutil.ReadFile(expectedOutputFile) + bytes, err := os.ReadFile(expectedOutputFile) Ω(err).ShouldNot(HaveOccurred()) expectedOutput := string(bytes) generatedOutputFile := filepath.Join(outputDir, "nested_dir", "test.go") - bytes, err = ioutil.ReadFile(generatedOutputFile) + bytes, err = os.ReadFile(generatedOutputFile) Ω(err).ShouldNot(HaveOccurred()) actualOutput := string(bytes) @@ -98,30 +97,36 @@ var _ = Describe("rewrite-package -d dirname -r", func() { It("adds a i18n_init.go per package", func() { initFile := filepath.Join(outputDir, "i18n_init.go") - expectedBytes, err := ioutil.ReadFile(initFile) + expectedBytes, err := os.ReadFile(initFile) Ω(err).ShouldNot(HaveOccurred()) expected := strings.TrimSpace(string(expectedBytes)) expectedInitFile := filepath.Join(expectedFilesPath, "i18n_init.go") - actualBytes, err := ioutil.ReadFile(expectedInitFile) + actualBytes, err := os.ReadFile(expectedInitFile) Ω(err).ShouldNot(HaveOccurred()) actual := strings.TrimSpace(string(actualBytes)) Ω(actual).Should(Equal(expected)) initFile = filepath.Join(outputDir, "nested_dir", "i18n_init.go") - expectedBytes, err = ioutil.ReadFile(initFile) + expectedBytes, err = os.ReadFile(initFile) Ω(err).ShouldNot(HaveOccurred()) expected = strings.TrimSpace(string(expectedBytes)) expectedInitFile = filepath.Join(expectedFilesPath, "nested_dir", "i18n_init.go") - actualBytes, err = ioutil.ReadFile(expectedInitFile) + actualBytes, err = os.ReadFile(expectedInitFile) Ω(err).ShouldNot(HaveOccurred()) actual = strings.TrimSpace(string(actualBytes)) Ω(actual).Should(Equal(expected)) }) + It("adds a generated i18n_resources.go file", func() { + i18nRC := filepath.Join(outputDir, "i18n_resources.go") + _, err := os.ReadFile(i18nRC) + Ω(err).ShouldNot(HaveOccurred()) + }) + It("does not translate test files", func() { _, doesFileExistErr := os.Stat(filepath.Join(outputDir, "a_really_bad_test.go")) Ω(os.IsNotExist(doesFileExistErr)).Should(BeTrue()) @@ -135,7 +140,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -172,7 +177,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -210,7 +215,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -255,7 +260,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -276,13 +281,13 @@ var _ = Describe("rewrite-package -d dirname -r", func() { It("adds T() callExprs wrapping string literals", func() { expectedOutputFile := filepath.Join(expectedFilesPath, "test.go") - bytes, err := ioutil.ReadFile(expectedOutputFile) + bytes, err := os.ReadFile(expectedOutputFile) Ω(err).ShouldNot(HaveOccurred()) expectedOutput := string(bytes) generatedOutputFile := filepath.Join(outputDir, "test.go") - bytes, err = ioutil.ReadFile(generatedOutputFile) + bytes, err = os.ReadFile(generatedOutputFile) Ω(err).ShouldNot(HaveOccurred()) actualOutput := string(bytes) @@ -291,13 +296,13 @@ var _ = Describe("rewrite-package -d dirname -r", func() { It("recurses to files in nested dirs", func() { expectedOutputFile := filepath.Join(expectedFilesPath, "nested_dir", "test.go") - bytes, err := ioutil.ReadFile(expectedOutputFile) + bytes, err := os.ReadFile(expectedOutputFile) Ω(err).ShouldNot(HaveOccurred()) expectedOutput := string(bytes) generatedOutputFile := filepath.Join(outputDir, "nested_dir", "test.go") - bytes, err = ioutil.ReadFile(generatedOutputFile) + bytes, err = os.ReadFile(generatedOutputFile) Ω(err).ShouldNot(HaveOccurred()) actualOutput := string(bytes) @@ -306,24 +311,24 @@ var _ = Describe("rewrite-package -d dirname -r", func() { It("adds a i18n_init.go per package", func() { initFile := filepath.Join(outputDir, "i18n_init.go") - expectedBytes, err := ioutil.ReadFile(initFile) + expectedBytes, err := os.ReadFile(initFile) Ω(err).ShouldNot(HaveOccurred()) expected := strings.TrimSpace(string(expectedBytes)) expectedInitFile := filepath.Join(expectedFilesPath, "i18n_init.go") - actualBytes, err := ioutil.ReadFile(expectedInitFile) + actualBytes, err := os.ReadFile(expectedInitFile) Ω(err).ShouldNot(HaveOccurred()) actual := strings.TrimSpace(string(actualBytes)) Ω(actual).Should(Equal(expected)) initFile = filepath.Join(outputDir, "nested_dir", "i18n_init.go") - expectedBytes, err = ioutil.ReadFile(initFile) + expectedBytes, err = os.ReadFile(initFile) Ω(err).ShouldNot(HaveOccurred()) expected = strings.TrimSpace(string(expectedBytes)) expectedInitFile = filepath.Join(expectedFilesPath, "nested_dir", "i18n_init.go") - actualBytes, err = ioutil.ReadFile(expectedInitFile) + actualBytes, err = os.ReadFile(expectedInitFile) Ω(err).ShouldNot(HaveOccurred()) actual = strings.TrimSpace(string(actualBytes)) @@ -343,7 +348,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -379,7 +384,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") @@ -416,7 +421,7 @@ var _ = Describe("rewrite-package -d dirname -r", func() { rootPath = filepath.Join(dir, "..", "..") - outputDir, err = ioutil.TempDir(rootPath, "i18n4go_integration") + outputDir, err = os.MkdirTemp(rootPath, "i18n4go_integration") Ω(err).ShouldNot(HaveOccurred()) fixturesPath = filepath.Join("..", "..", "test_fixtures", "rewrite_package") diff --git a/integration/rewrite_package/f_option_test.go b/integration/rewrite_package/f_option_test.go index 9f61358..ea2351b 100644 --- a/integration/rewrite_package/f_option_test.go +++ b/integration/rewrite_package/f_option_test.go @@ -87,6 +87,12 @@ var _ = Describe("rewrite-package -f filename", func() { Ω(err).ShouldNot(HaveOccurred()) Ω(expectedBytes).ShouldNot(Equal("")) }) + + It("adds the generated i18n_resources.go file", func() { + i18nRC := filepath.Join(outputDir, "i18n_resources.go") + _, err := os.ReadFile(i18nRC) + Ω(err).ShouldNot(HaveOccurred()) + }) }) Context("all strings to rewrite are simple strings", func() { diff --git a/test_fixtures/rewrite_package/f_option/expected_output/i18n_init.go b/test_fixtures/rewrite_package/f_option/expected_output/i18n_init.go index 8d6af48..f894386 100644 --- a/test_fixtures/rewrite_package/f_option/expected_output/i18n_init.go +++ b/test_fixtures/rewrite_package/f_option/expected_output/i18n_init.go @@ -9,5 +9,7 @@ import ( var T i18n.TranslateFunc func init() { - T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "f_option", "input_files"), i18n.GetResourcesPath()) + T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "f_option", "input_files"), i18n.GetResourcesPath(), func(asset string) ([]byte, error) { + return Asset(asset) + }) } diff --git a/test_fixtures/rewrite_package/f_option/expected_output/nested_dir/i18n_init.go b/test_fixtures/rewrite_package/f_option/expected_output/nested_dir/i18n_init.go index 97e0796..2911004 100644 --- a/test_fixtures/rewrite_package/f_option/expected_output/nested_dir/i18n_init.go +++ b/test_fixtures/rewrite_package/f_option/expected_output/nested_dir/i18n_init.go @@ -9,5 +9,7 @@ import ( var T i18n.TranslateFunc func init() { - T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "f_option", "input_files", "nested_dir"), i18n.GetResourcesPath()) + T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "f_option", "input_files", "nested_dir"), i18n.GetResourcesPath(), func(asset string) ([]byte, error) { + return Asset(asset) + }) } diff --git a/test_fixtures/rewrite_package/init_code_snippet_filename/expected_output/i18n_init_default.go b/test_fixtures/rewrite_package/init_code_snippet_filename/expected_output/i18n_init_default.go index f3b47b6..14c1430 100644 --- a/test_fixtures/rewrite_package/init_code_snippet_filename/expected_output/i18n_init_default.go +++ b/test_fixtures/rewrite_package/init_code_snippet_filename/expected_output/i18n_init_default.go @@ -9,5 +9,7 @@ import ( var T i18n.TranslateFunc func init() { - T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "init_code_snippet_filename", "input_files"), i18n.GetResourcesPath()) + T = i18n.Init(filepath.Join("test_fixtures", "rewrite_package", "init_code_snippet_filename", "input_files"), i18n.GetResourcesPath(), func(asset string) ([]byte, error) { + return Asset(asset) + }) }