Add default language value in layouts

I just nearly embarrassed myself by adding an issue to the Hugo repo proposing a default value for translation tags. But as always the solution is already baked in.

My thought was: normally when developing something you add all the language right there in the template files (looking at WordPress for instance) and now with Hugo we need to add an i18n/en.toml and reference the values in there in the template. Which basically makes every location where text is printed a double-work, once in the layout and once in the language file.

But no: default to the rescue.

{{ i18n "some_button_title" | default "My great and click-inducing default button title" }}

Thinking this further, this might be a perfect way to parse out all translation keys + values for the default language programmatically.

1 Like

I believe the best practice in developing multilingual applications is to separate structure & logic from content entirely. Enabling the opposite would encourage bad practices.
If they’re getting difficult to handle, you could use something like Poedit to extract all keys automatically and generate / update your translation file. It does so by parsing the translation functions.

1 Like