diff --git a/i18n/bundle.go b/i18n/bundle.go index dfe4d7a..d15970d 100644 --- a/i18n/bundle.go +++ b/i18n/bundle.go @@ -12,6 +12,9 @@ import ( // UnmarshalFunc unmarshals data into v. type UnmarshalFunc func(data []byte, v interface{}) error +// ParsePathFunc parses language tag and format from path. +type ParsePathFunc func(path string) (langTag, format string) + // Bundle stores a set of messages and pluralization rules. // Most applications only need a single bundle // that is initialized early in the application's lifecycle. @@ -24,6 +27,7 @@ type Bundle struct { pluralRules plural.Rules tags []language.Tag matcher language.Matcher + parsePathFunc ParsePathFunc } // artTag is the language tag used for artificial languages @@ -49,6 +53,11 @@ func (b *Bundle) RegisterUnmarshalFunc(format string, unmarshalFunc UnmarshalFun b.unmarshalFuncs[format] = unmarshalFunc } +// SetParsePathFunc sets the function that parses the language tag and format from a file path. +func (b *Bundle) SetParsePathFunc(parsePathFunc ParsePathFunc) { + b.parsePathFunc = parsePathFunc +} + // LoadMessageFile loads the bytes from path // and then calls ParseMessageFileBytes. func (b *Bundle) LoadMessageFile(path string) (*MessageFile, error) { @@ -73,7 +82,7 @@ func (b *Bundle) MustLoadMessageFile(path string) { // // The language tag of the file is everything after the second to last "." or after the last path separator, but before the format. func (b *Bundle) ParseMessageFileBytes(buf []byte, path string) (*MessageFile, error) { - messageFile, err := ParseMessageFileBytes(buf, path, b.unmarshalFuncs) + messageFile, err := ParseMessageFileBytesWithParsePathFunc(buf, path, b.unmarshalFuncs, b.parsePathFunc) if err != nil { return nil, err } diff --git a/i18n/parse.go b/i18n/parse.go index fc7f707..ae974d4 100644 --- a/i18n/parse.go +++ b/i18n/parse.go @@ -19,7 +19,26 @@ type MessageFile struct { // ParseMessageFileBytes returns the messages parsed from file. func ParseMessageFileBytes(buf []byte, path string, unmarshalFuncs map[string]UnmarshalFunc) (*MessageFile, error) { - lang, format := parsePath(path) + return ParseMessageFileBytesWithParsePathFunc(buf, path, unmarshalFuncs, parsePath) +} + +// ParseMessageFileBytesWithParsePathFunc parses message files based on file content and path. +// It uses a custom file path parsing function and a map of deserialization functions to handle different formats. +// The main purpose of this function is to parse the file content into a MessageFile structure according to the file path and format. +// Parameters: +// - buf: The file content in byte slice form. +// - path: The file path string, used to determine the language and format. +// - unmarshalFuncs: A map containing deserialization functions for different formats. +// - parsePathFunc: A custom function for parsing file paths, used to obtain language and format information. +// Return values: +// - *MessageFile: A pointer to the parsed MessageFile structure. +// - error: Error information, if any. +func ParseMessageFileBytesWithParsePathFunc(buf []byte, path string, unmarshalFuncs map[string]UnmarshalFunc, parsePathFunc ParsePathFunc) (*MessageFile, error) { + if parsePathFunc == nil { + parsePathFunc = parsePath + } + + lang, format := parsePathFunc(path) tag := language.Make(lang) messageFile := &MessageFile{ Path: path,