From c30a5a683da8f2fc1a9914b9e9ca1a78b8ea44fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Lindstr=C3=B6m?= Date: Thu, 25 Feb 2021 08:42:43 +0100 Subject: [PATCH] Add support for loading bundle from fs.FS 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. --- v2/i18n/bundle.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/v2/i18n/bundle.go b/v2/i18n/bundle.go index 513c127d..3e6cf728 100644 --- a/v2/i18n/bundle.go +++ b/v2/i18n/bundle.go @@ -2,6 +2,7 @@ package i18n import ( "fmt" + "io/fs" "io/ioutil" "github.com/nicksnyder/go-i18n/v2/internal/plural" @@ -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 ".".