Skip to content

Commit

Permalink
layout: implement nested locales
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Aug 4, 2024
1 parent a8acf84 commit 2c98c59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
13 changes: 13 additions & 0 deletions layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,17 @@ func TestLayout(t *testing.T) {
Description: "Some description",
PreviewURL: "https://preview.picture",
}))

assert.Equal(t,
"This is an article.",
lt.TextLocale("en", "article_message"),
)
assert.Equal(t,
lt.TextLocale("en", "nested.example", "an example"),
"This is an example.",
)
assert.Equal(t,
lt.TextLocale("en", "nested.another.example", "another example"),
"This is another example.",
)
}
9 changes: 8 additions & 1 deletion layout/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
article_message: This is an article.
article_message: This is an article.

nested:
example: |-
This is {{ . }}.
another:
example: |-
This is {{ . }}.
13 changes: 9 additions & 4 deletions layout/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,23 @@ func (lt *Layout) parseLocales(dir string) error {
return err
}

var texts map[string]string
var texts map[string]interface{}
if err := yaml.Unmarshal(data, &texts); err != nil {
return err
}

v := viper.New()
if err := v.MergeConfigMap(texts); err != nil {
return err
}

name := fi.Name()
name = strings.TrimSuffix(name, filepath.Ext(name))

tmpl := template.New(name).Funcs(lt.funcs)
for key, text := range texts {
_, err = tmpl.New(key).Parse(strings.Trim(text, "\r\n"))
if err != nil {
for _, key := range v.AllKeys() {
text := strings.Trim(v.GetString(key), "\r\n")
if _, err := tmpl.New(key).Parse(text); err != nil {
return err
}
}
Expand Down

0 comments on commit 2c98c59

Please sign in to comment.