@@ -43,15 +43,26 @@ type Config struct {
4343 PluralCount int
4444}
4545
46- // IncludeAssets walks the /internal/locales directory
46+ // IncludeAssetsAndLoadMessageFiles walks the /internal/locales directory
4747// and allows the static assets found to be embedded into the binary
4848// by github.com/markbates/pkger
49- func IncludeAssets () error {
49+ // It also loads all files into memory
50+ func IncludeAssetsAndLoadMessageFiles () error {
51+ localeFileName := fmt .Sprintf ("active.%v" , getLangFormat ())
5052 return pkger .Walk ("/locales" , func (path string , info os.FileInfo , err error ) error {
5153 if err != nil {
5254 return err
5355 }
5456
57+ if info .IsDir () || localeFileName != info .Name () {
58+ return nil
59+ }
60+
61+ err = loadMessageFile (path )
62+ if err != nil {
63+ return err
64+ }
65+
5566 return nil
5667 })
5768}
@@ -77,30 +88,31 @@ func MustLocalizeFromID(messageID string) string {
7788 })
7889}
7990
80- // LoadMessageFiles loads the message file int context
91+ // loadMessageFile loads the message file int context
8192// Using github.com/nicksnyder/go-i18n/v2/i18n
8293// 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" )
94+ func loadMessageFile (path string ) (err error ) {
95+ // open the static i18n file
96+ f , err := pkger .Open (path )
97+ if err != nil {
98+ return err
10399 }
100+ defer f .Close ()
101+ b := bytes .NewBufferString ("" )
102+ // copy to contents of the file to a buffer string
103+ if _ , err = io .Copy (b , f ); err != nil {
104+ panic (err )
105+ }
106+ // read the contents of the file to a byte array
107+ out , _ := ioutil .ReadAll (b )
108+ // load the contents into context
109+ bundle .RegisterUnmarshalFunc ("toml" , toml .Unmarshal )
110+ _ , err = bundle .ParseMessageFileBytes (out , "en.toml" )
111+ if err != nil {
112+ return err
113+ }
114+
115+ return nil
104116}
105117
106118// get the file extension for the current language
0 commit comments