Skip to content

Commit

Permalink
Add support for loading bundle from fs.FS
Browse files Browse the repository at this point in the history
This commit aims to add a method for loading bundles from fs.FS. The
change is done to be able to get a nice way to load bundles from an
embedded file system.

Since this can be done by implementing the exported API and using
ParseMessageFileBytes externally, this is not a change that is a must
have. Rather a nice shorthand for using the fs.FS.
  • Loading branch information
antonlindstrom committed Feb 25, 2021
1 parent c587b0d commit c30a5a6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions v2/i18n/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i18n

import (
"fmt"
"io/fs"
"io/ioutil"

"github.com/nicksnyder/go-i18n/v2/internal/plural"
Expand Down Expand Up @@ -67,6 +68,17 @@ func (b *Bundle) MustLoadMessageFile(path string) {
}
}

// LoadMessageFileFS is like LoadMessageFile but instead of reading from the
// hosts operating system's file system it reads from the fs file system.
func (b *Bundle) LoadMessageFileFS(fsys fs.FS, path string) (*MessageFile, error) {
buf, err := fs.ReadFile(fsys, path)
if err != nil {
return nil, err
}

return b.ParseMessageFileBytes(buf, path)
}

// ParseMessageFileBytes parses the bytes in buf to add translations to the bundle.
//
// The format of the file is everything after the last ".".
Expand Down

0 comments on commit c30a5a6

Please sign in to comment.