Skip to content

feat(i18n): prefer languageCode translation tables before falling back to key #14204

@chalin

Description

@chalin

Hugo currently loads i18n translation tables based only on the language key in the languages map (Lang). This causes a mismatch when a site sets a more specific languageCode but retains a short language key.

Example:

languages:
  pt:
    languageCode: pt-BR
i18n/
├── en.toml # Default language
├── [No pt.toml file exists here on in themes etc.]
└── pt-br.toml

When rendering {{ T "some-key" }} for the pt site, Hugo falls back to en.toml because it looks only for pt.toml, ignoring the declared languageCode: pt-BR even though a matching pt-br.toml file exists.

Proposed behavior

If languages.<LANG_KEY>.languageCode is set:

  1. Try loading translations using languageCode.
  2. If no matching file exists, fall back to current behavior.

Benefits

The proposed behavior maximizes compatibility with existing behavior, while maximizing utility to site authors. Also it:

  • Makes translation lookup consistent with the language declared for the site.
  • Supports region-specific translations (pt-BR, pt-PT, etc.) without needing multiple language keys.
  • Improves compatibility with themes/modules that ship region-coded translation files (e.g., pt-br.toml, zh-hans.toml).

Related

Minimal site for testing

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions