Skip to content

Commit

Permalink
Fix edge case on Localized Views
Browse files Browse the repository at this point in the history
* When the default language is a higher preference user choice, use the non-suff
ixed template version
  • Loading branch information
stanislas-m committed Nov 24, 2017
1 parent 4b0ea93 commit d739538
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 15 additions & 1 deletion middleware/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (t *Translator) Middleware() buffalo.MiddlewareFunc {
if langs := c.Value("languages"); langs == nil {
c.Set("languages", t.LanguageFinder(t, c))
if t.LocalizedViews {
c.Set("templateSuffixes", c.Value("languages"))
c.Set("templateSuffixes", normalizeSuffixes(t.DefaultLanguage, c.Value("languages").([]string)))
}
}

Expand Down Expand Up @@ -187,3 +187,17 @@ func parseAcceptLanguage(acptLang string) []string {
}
return lqs
}

func normalizeSuffixes(defaultLang string, langSuffixes []string) []string {
var nSuffixes []string

for _, lang := range langSuffixes {
suffix := strings.ToLower(lang)
nSuffixes = append(nSuffixes, suffix)
if suffix == defaultLang {
// Add empty suffix for the default language
nSuffixes = append(nSuffixes, "")
}
}
return nSuffixes
}
7 changes: 6 additions & 1 deletion render/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func (s templateRenderer) exec(name string, data Data) (template.HTML, error) {
rawName := strings.TrimSuffix(name, ext)

for _, suffix := range suffixes {
candidateName := rawName + "_" + suffix + ext
var candidateName string
if suffix == "" {
candidateName = name
} else {
candidateName = rawName + "_" + suffix + ext
}
if s.TemplatesBox.Has(candidateName) {
// Replace name with the existing suffixed version
name = candidateName
Expand Down

0 comments on commit d739538

Please sign in to comment.