diff options
Diffstat (limited to 'vendor/github.com/nicksnyder/go-i18n')
3 files changed, 43 insertions, 5 deletions
diff --git a/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.flat.yaml b/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.flat.yaml index 316ac6b20..664128907 100644 --- a/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.flat.yaml +++ b/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.flat.yaml @@ -1,3 +1,7 @@ + + +# Comment +# Comment program_greeting: other: "Hello world" diff --git a/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.yaml b/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.yaml index cdf9c8dc5..a8f5ed9be 100644 --- a/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.yaml +++ b/vendor/github.com/nicksnyder/go-i18n/goi18n/testdata/en-us.yaml @@ -1,3 +1,8 @@ + + + +# Comment +# Comment - id: program_greeting translation: "Hello world" diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go index 2ad1d7cc3..a2291362f 100644 --- a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go +++ b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go @@ -9,6 +9,7 @@ import ( "path/filepath" "reflect" "sync" + "unicode" "github.com/nicksnyder/go-i18n/i18n/language" "github.com/nicksnyder/go-i18n/i18n/translation" @@ -86,8 +87,8 @@ func parseTranslations(filename string, buf []byte) ([]translation.Translation, ext := filepath.Ext(filename) - // `github.com/pelletier/go-toml` has an Unmarshal function, - // that can't unmarshal to maps, so we should parse TOML format separately. + // `github.com/pelletier/go-toml` lacks an Unmarshal function, + // so we should parse TOML separately. if ext == ".toml" { tree, err := toml.LoadReader(bytes.NewReader(buf)) if err != nil { @@ -119,11 +120,39 @@ func parseTranslations(filename string, buf []byte) ([]translation.Translation, } func isStandardFormat(ext string, buf []byte) bool { + buf = deleteLeadingComments(ext, buf) firstRune := rune(buf[0]) - if (ext == ".json" && firstRune == '[') || (ext == ".yaml" && firstRune == '-') { - return true + return (ext == ".json" && firstRune == '[') || (ext == ".yaml" && firstRune == '-') +} + +// deleteLeadingComments deletes leading newlines and comments in buf. +// It only works for ext == ".yaml". +func deleteLeadingComments(ext string, buf []byte) []byte { + if ext != ".yaml" { + return buf } - return false + + for { + buf = bytes.TrimLeftFunc(buf, unicode.IsSpace) + if buf[0] == '#' { + buf = deleteLine(buf) + } else { + break + } + } + + return buf +} + +func deleteLine(buf []byte) []byte { + index := bytes.IndexRune(buf, '\n') + if index == -1 { // If there is only one line without newline ... + return nil // ... delete it and return nothing. + } + if index == len(buf)-1 { // If there is only one line with newline ... + return nil // ... do the same as above. + } + return buf[index+1:] } // unmarshal finds an appropriate unmarshal function for ext |