Zola static site generator and localization (l10n)

100 Views Asked by At

The problem

I'm new to Zola and its templating engine Tera, and I'm struggling with making my own Zola theme easily localizable, even providing some out-of-the-box translations for e.g. English and German. Zolas's fluent integration is not there yet - as of Zola v0.17.2 and AFAIK. This problem is not about a multi-language theme, but only about enabling easy localization.

My "hacky" solution

Consider this text on a page where a blog post is published as an example: Published on July 04, 2023. I want to localize the static text and the dynamic date, and its format.

In the config.toml file, I have default_language = "en", where the language can be set to another language with some existing language-specific resources. I do not want these resources to end up in my config.toml file, but they should be in a different file for each language.

Therefore, I use the data directory with e.g. these files: en-translations.toml, and de-translations.toml. The first file looks like this, the other has the same keys, just different values:

# Global settings
settings_date_locale = "en_US"
settings_date_format = "%B %d, %Y"

# Language resources
published_on = "Published on"

Now, in the base template base.html, on which all other template files are based, I load the language-specific resources into a variable called res:

{% set res         = load_data(path='data/' ~ config.default_language ~ '-translations.toml') %}
{% set date_locale = res.settings_date_locale | default(value="en_US")     %}
{% set date_format = res.settings_date_format | default(value="%B %d, %Y") %}

I could reference e.g. res.settings_date_format directly, but because I want to have a fallback, I save it in a separate variable.

With this preparation, I can now do the following in every template:

<span>
{{ res.published_on }} {{ page.date | date(locale=date_locale, format=date_format) }}
</span>

For default_language = "en" it will be rendered as Published on July 04, 2023.

For default_language = "de" it will be rendered as Veröffentlicht am 04. Juli 2023.

My question

I want to enable easy localization of my theme. The solutions mentioned above feels not elegant, and I'm not sure if there is a better solution, even one that the author of Zola - as of version v0.17.2 - envisioned for localization.

How would someone proficient in Zola and Tera tackle the problem?

0

There are 0 best solutions below