Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: generate i18n_resource binary file for user on rewrite-package #131

Merged
merged 6 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
Expand All @@ -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=
Expand All @@ -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=
Expand Down
50 changes: 44 additions & 6 deletions i18n4go/cmds/rewrite_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
})
}`
)

Expand All @@ -59,6 +62,8 @@ type rewritePackage struct {
OutputDirname string
I18nStringsFilename string
I18nStringsDirname string
I18nStringsFilePaths []string
RootPackageName string
RootPath string
InitCodeSnippetFilename string

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -227,6 +260,7 @@ func (rp *rewritePackage) resetProcessing() {
rp.ExtractedStrings = nil
rp.UpdatedExtractedStrings = nil
rp.I18nStringsFilename = ""
rp.RootPackageName = ""
rp.SaveExtractedStrings = false
}

Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 15 additions & 15 deletions i18n4go/i18n/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package i18n

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -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",
Expand All @@ -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
)

Expand All @@ -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
Expand All @@ -85,42 +85,42 @@ 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 == "" {
userLocale = DEFAULT_LOCALE
} 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())
}

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")
Expand Down
Loading
Loading