Skip to content

trans(): use active page language with fallback to default_language #3161

@vladkens

Description

@vladkens

Problem

Zola already knows the active language when rendering a page — it's embedded in the routing context (/es/...lang = "es"). But trans(key="foo") ignores this and always defaults to default_language. And trans(key="foo", lang=lang) throws a hard error if the key is missing in that language.

This makes partial translations impractical. Common scenario: a site where [translations] is fully filled but [languages.es.translations] only has a few keys so far. Right now you have two bad options:

  1. Duplicate all default language keys into every language, even untranslated ones
  2. Never pass lang=lang and keep the UI permanently in the default language

Proposed solutions

Option A — change default behavior of trans() (breaking):
trans(key="foo") without explicit lang= uses the active page language first, falls back to default_language if the key is missing. Strict behavior can be opted into per-call or globally in config.toml:

{{ trans(key="foo") }}               {# active lang → fallback to default #}
{{ trans(key="foo", strict=true) }}  {# error if key missing #}
# config.toml — enforce strict mode globally
[translations]
strict = true

Option B — new function trans_fallback() (non-breaking):

{{ trans_fallback(key="foo") }}       {# active lang → fallback to default #}
{{ trans(key="foo", lang=lang) }}     {# existing strict behavior unchanged #}

Option C — fallback parameter on existing trans() (non-breaking):

{{ trans(key="foo", lang=lang, fallback=true) }}

Personally I find Option B the most pragmatic — it doesn't touch existing behavior at all and gives users an explicit opt-in for the fallback semantic.


Happy to open a PR for whichever direction you prefer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions