i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Example using Go Embed.FS

Open ivanvanderbyl opened this issue 3 years ago • 0 comments

Thanks for this great library, I put together an example of how to use it with Go 1.16 embed.FS:

package locales

import (
	"embed"
)

//go:embed */*.yml
var translations embed.FS

func I18n() (*i18n.I18n, error) {
	// assuming embedded files at ./en/*.yml
	return i18n.New(i18n.Assets(translationNames, translations.ReadFile), "en")
}

func translationNames() (filenames []string) {
	fs.WalkDir(translations, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		if d.IsDir() {
			return nil
		}

		filenames = append(filenames, path)
		return nil
	})

	return filenames
}

ivanvanderbyl avatar Jun 21 '21 02:06 ivanvanderbyl