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

feat: add support for custom path parsing function #346

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion i18n/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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
}
Expand Down
21 changes: 20 additions & 1 deletion i18n/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down