Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading bundle from fs.FS #246

Merged
merged 1 commit into from
Mar 17, 2021

Conversation

antonlindstrom
Copy link
Contributor

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.

This is how it can be used:

package main

import (
        "embed"

        "github.com/nicksnyder/go-i18n/v2/i18n"
)

//go:embed locale.*.json
var LocaleFS embed.FS

func main() {
        bundle := i18n.NewBundle(language.English)
        bundle.RegisterUnmarshalFunc("json", json.Unmarshal)

        bundle.LoadMessageFileFS(LocaleFS, "locale.sv.json")
}

Thanks!

@hachi8833
Copy link

I definitely want the feature❤️

@nicksnyder
Copy link
Owner

Build checks were broken on master but I just fixed that so could you rebase on master?

I think this will fail the Go 1.9 build. Can you use build tags to make this function available in newer versions of go without breaking older versions of Go?

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.
@codecov
Copy link

codecov bot commented Mar 4, 2021

Codecov Report

Merging #246 (58515e2) into main (5d2ec5f) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #246   +/-   ##
=======================================
  Coverage   82.68%   82.68%           
=======================================
  Files          14       14           
  Lines        1178     1178           
=======================================
  Hits          974      974           
  Misses        141      141           
  Partials       63       63           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5d2ec5f...58515e2. Read the comment docs.

@antonlindstrom
Copy link
Contributor Author

Thank you so much for the good review! I completely missed the backwards compatibility. I've moved the method to it's own file and added a build tag, tested with both 1.16 and 1.15 (pre io/fs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants