diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-19 09:02:18 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-19 16:23:00 +0200 |
commit | 47c05c47e0b663632a649ee5d256acc1a32fe9e4 (patch) | |
tree | c4cc45e6f7f950bf6e4f779e544a76db68aab9c7 /resource/resource.go | |
parent | e590cc26eb1363a4b84603f051b20bd43fd1f7bd (diff) |
Add language merge support for Pages in resource.Resources
Fixes #4644
Diffstat (limited to 'resource/resource.go')
-rw-r--r-- | resource/resource.go | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/resource/resource.go b/resource/resource.go index 12e1160cf..e92706803 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -34,10 +34,11 @@ import ( ) var ( - _ Resource = (*genericResource)(nil) - _ metaAssigner = (*genericResource)(nil) - _ Source = (*genericResource)(nil) - _ Cloner = (*genericResource)(nil) + _ Resource = (*genericResource)(nil) + _ metaAssigner = (*genericResource)(nil) + _ Source = (*genericResource)(nil) + _ Cloner = (*genericResource)(nil) + _ ResourcesLanguageMerger = (*Resources)(nil) ) const DefaultResourceType = "unknown" @@ -97,6 +98,16 @@ type Resource interface { Content() (interface{}, error) } +type ResourcesLanguageMerger interface { + MergeByLanguage(other Resources) Resources + // Needed for integration with the tpl package. + MergeByLanguageInterface(other interface{}) (interface{}, error) +} + +type translatedResource interface { + TranslationKey() string +} + // Resources represents a slice of resources, which can be a mix of different types. // I.e. both pages and images etc. type Resources []Resource @@ -222,6 +233,36 @@ func getGlob(pattern string) (glob.Glob, error) { } +// MergeByLanguage adds missing translations in r1 from r2. +func (r1 Resources) MergeByLanguage(r2 Resources) Resources { + result := append(Resources(nil), r1...) + m := make(map[string]bool) + for _, r := range r1 { + if translated, ok := r.(translatedResource); ok { + m[translated.TranslationKey()] = true + } + } + + for _, r := range r2 { + if translated, ok := r.(translatedResource); ok { + if _, found := m[translated.TranslationKey()]; !found { + result = append(result, r) + } + } + } + return result +} + +// MergeByLanguageInterface is the generic version of MergeByLanguage. It +// is here just so it can be called from the tpl package. +func (r1 Resources) MergeByLanguageInterface(in interface{}) (interface{}, error) { + r2, ok := in.(Resources) + if !ok { + return nil, fmt.Errorf("%T cannot be merged by language", in) + } + return r1.MergeByLanguage(r2), nil +} + type Spec struct { *helpers.PathSpec |