11package localizer
22
33import (
4+ "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/debug"
45 "bytes"
56 "fmt"
67 "io"
@@ -43,15 +44,26 @@ type Config struct {
4344 PluralCount int
4445}
4546
46- // IncludeAssets walks the /internal/locales directory
47+ // IncludeAssetsAndLoadMessageFiles walks the /internal/locales directory
4748// and allows the static assets found to be embedded into the binary
4849// by github.com/markbates/pkger
49- func IncludeAssets () error {
50+ // It also loads all files into memory
51+ func IncludeAssetsAndLoadMessageFiles () error {
52+ localeFileName := fmt .Sprintf ("active.%v" , getLangFormat ())
5053 return pkger .Walk ("/locales" , func (path string , info os.FileInfo , err error ) error {
5154 if err != nil {
5255 return err
5356 }
5457
58+ if info .IsDir () || localeFileName != info .Name () {
59+ return nil
60+ }
61+
62+ err = loadMessageFile (path )
63+ if err != nil {
64+ return err
65+ }
66+
5567 return nil
5668 })
5769}
@@ -77,30 +89,31 @@ func MustLocalizeFromID(messageID string) string {
7789 })
7890}
7991
80- // LoadMessageFiles loads the message file int context
92+ // loadMessageFile loads the message file int context
8193// Using github.com/nicksnyder/go-i18n/v2/i18n
8294// pathTree to File is an array of the parent directories
83- // For example: ["cmd/kafka/topic/create"] resolves to /locales/cmd/kafka/topic/create/active.en.toml
84- func LoadMessageFiles (dirs ... string ) {
85- for _ , path := range dirs {
86- pathToFile := fmt .Sprintf ("/locales/%v/active.%v" , path , getLangFormat ())
87- // open the static i18n file
88- f , err := pkger .Open (pathToFile )
89- if err != nil {
90- panic (err )
91- }
92- defer f .Close ()
93- b := bytes .NewBufferString ("" )
94- // copy to contents of the file to a buffer string
95- if _ , err := io .Copy (b , f ); err != nil {
96- panic (err )
97- }
98- // read the contents of the file to a byte array
99- out , _ := ioutil .ReadAll (b )
100- // load the contents into context
101- bundle .RegisterUnmarshalFunc ("toml" , toml .Unmarshal )
102- bundle .MustParseMessageFileBytes (out , "en.toml" )
95+ func loadMessageFile (path string ) (err error ) {
96+ // open the static i18n file
97+ f , err := pkger .Open (path )
98+ if err != nil {
99+ return err
103100 }
101+ defer f .Close ()
102+ b := bytes .NewBufferString ("" )
103+ // copy to contents of the file to a buffer string
104+ if _ , err = io .Copy (b , f ); err != nil {
105+ panic (err )
106+ }
107+ // read the contents of the file to a byte array
108+ out , _ := ioutil .ReadAll (b )
109+ // load the contents into context
110+ bundle .RegisterUnmarshalFunc ("toml" , toml .Unmarshal )
111+ _ , err = bundle .ParseMessageFileBytes (out , "en.toml" )
112+ if err != nil {
113+ return err
114+ }
115+
116+ return nil
104117}
105118
106119// get the file extension for the current language
0 commit comments