Skip to content

Commit 2c98c59

Browse files
committed
layout: implement nested locales
1 parent a8acf84 commit 2c98c59

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

layout/layout_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,17 @@ func TestLayout(t *testing.T) {
132132
Description: "Some description",
133133
PreviewURL: "https://preview.picture",
134134
}))
135+
136+
assert.Equal(t,
137+
"This is an article.",
138+
lt.TextLocale("en", "article_message"),
139+
)
140+
assert.Equal(t,
141+
lt.TextLocale("en", "nested.example", "an example"),
142+
"This is an example.",
143+
)
144+
assert.Equal(t,
145+
lt.TextLocale("en", "nested.another.example", "another example"),
146+
"This is another example.",
147+
)
135148
}

layout/locales/en.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
article_message: This is an article.
1+
article_message: This is an article.
2+
3+
nested:
4+
example: |-
5+
This is {{ . }}.
6+
another:
7+
example: |-
8+
This is {{ . }}.

layout/parser.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,18 +246,23 @@ func (lt *Layout) parseLocales(dir string) error {
246246
return err
247247
}
248248

249-
var texts map[string]string
249+
var texts map[string]interface{}
250250
if err := yaml.Unmarshal(data, &texts); err != nil {
251251
return err
252252
}
253253

254+
v := viper.New()
255+
if err := v.MergeConfigMap(texts); err != nil {
256+
return err
257+
}
258+
254259
name := fi.Name()
255260
name = strings.TrimSuffix(name, filepath.Ext(name))
256261

257262
tmpl := template.New(name).Funcs(lt.funcs)
258-
for key, text := range texts {
259-
_, err = tmpl.New(key).Parse(strings.Trim(text, "\r\n"))
260-
if err != nil {
263+
for _, key := range v.AllKeys() {
264+
text := strings.Trim(v.GetString(key), "\r\n")
265+
if _, err := tmpl.New(key).Parse(text); err != nil {
261266
return err
262267
}
263268
}

0 commit comments

Comments
 (0)