Access multilingual data in partials

I get theme without multilingual support and try to add it.

themes/coHub/layouts/index.html

{{define "main"}}
  {{ partial "hero.html" . }}
{{end}}

I cant’ understand how to render .title and .subtitle with correct language

themes/coHub/partials/hero.html

{{with .Site.Data.hero}}
{{if .enable}}
<h1>{{.title | safeHTML}}</h1>
<p>
  {{ .subtitle }}
</p>
{{end}}
{{end}}

/data/hero.yml

enable: true
title: "Welcome"
subtitle: "Discover"

/data/hero.ru.yml

enable: true
title: "Добро пожаловать"
subtitle: "Откройте"

Also where I should place yml files for all languages

i18n/en.toml (English)
# simple translations
privacy = 'privacy'
security = 'security'

# translations with pluralization
[day]
one = 'day'
other = 'days'

[day_with_count]
one = '{{ . }} day'
other = '{{ . }} days'

i18n/pl.toml (Polish)
# simple translations
privacy = 'prywatność'
security = 'bezpieczeństwo'

# translations with pluralization
[day]
one = 'miesiąc'
few = 'miesiące'
many = 'miesięcy'
other = 'miesiąca'

[day_with_count]
one = '{{ . }} miesiąc'
few = '{{ . }} miesiące'
many = '{{ . }} miesięcy'
other = '{{ . }} miesiąca'

When viewing the English language site:

{{ T "privacy" }} --> privacy
{{ T "security" }} --> security

{{ T "day" 0 }} --> days
{{ T "day" 1 }} --> day
{{ T "day" 2 }} --> days
{{ T "day" 5 }} --> days

{{ T "day_with_count" 0 }} --> 0 days
{{ T "day_with_count" 1 }} --> 1 day
{{ T "day_with_count" 2 }} --> 2 days
{{ T "day_with_count" 5 }} --> 5 days

When viewing the Polish language site:

{{ T "privacy" }} --> prywatność
{{ T "security" }} --> bezpieczeństwo

{{ T "day" 0 }} --> miesięcy
{{ T "day" 1 }} --> miesiąc
{{ T "day" 2 }} --> miesiące
{{ T "day" 5 }} --> miesięcy

{{ T "day_with_count" 0 }} --> 0 miesięcy
{{ T "day_with_count" 1 }} --> 1 miesiąc
{{ T "day_with_count" 2 }} --> 2 miesiące
{{ T "day_with_count" 5 }} --> 5 miesięcy

The Unicode CLDR Plural Rules chart describes the pluralization categories for each language.

1 Like

Thanks for you answer, jmooring!

I know about i18n, I use it for simple words… But what should I do if make blog post - it’s a big text.
Should I add all blog posts texts in i18n/en.toml files?

/content/about/_index.en.md
/content/about/_index.ru.md

works perfect!
I can’t understand mechanic of /data/*.yml files

Sorry, but I do not understand your question.

  • If you need to translate a word within a template, use the T function.
  • If you need to translate a content file, create a content file for each language.
  • If you need to translate a word from a data file, either pass the element through the T function, or create a data file for each language.
  • If you need to translate site parameters, create a params key for each language in your site configuration.
  • If you need to translate menu entries, there are several options depending on how you defined the menu entries.

I need to translate /theme/coHub/layouts/partials/hero.html file using /data/*.yml files.

hero.html loads in /theme/coHub/index.html

{{define "main"}}
  {{ partial "hero.html" . }}
{{end}}

pages without “partials” translated correctly:
/theme/coHub/layouts/about/list.html by files:
/content/about/_index.en.md
/content/about/_index.ru.md

mkdir -p data/en
mkdir -p data/ru
cp data/hero.yml data/en
mv data/hero.yml data/ru
mkdir -p layouts/partials
cp themes/coHub/layouts/partials/hero.html layouts/partials

layouts/partials/hero.html (change first line)

{{with index .Site.Data .Language.Lang "hero" }}

Do the same thing with other data files as needed. The theme was obviously not designed for multilingual setups.

Brilliant!
I can’t google that.
Thanks a lot!
Where I can read how "with index .Site.Data .Language.Lang “hero” works?

Look at the index function in the docs.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.