In multilingual sites, posts often share the same attachment files (for example, PDFs, config files, or scripts). If each language version maintains download links manually, link drift and missing files become common over time.
This article introduces a reusable Hugo shortcode, bundle-file, to solve this problem.
Goal
Keep multilingual post files and attachments in the same Page Bundle directory, for example:
|
|
This maximizes reuse and avoids duplicate copies. After Hugo builds HTML pages, all language versions should point to the same attachment instead of duplicated files.
Shortcode Implementation
File: layouts/shortcodes/bundle-file.html
|
|
bundle-file works with a simple strategy:
- Look up the file in current page resources first.
- If not found, search the same file name in other translated pages.
- Output a download link when found, or show a missing-file message when not found.
Parameters
name: attachment filename (required)text: link label (optional); defaults toname
Usage Example
|
|
Without text:
|
|
Pre-publish Checklist
- Put attachments and post files in the same Page Bundle.
- Ensure
nameexactly matches the real filename (including case). - Click links in local preview to confirm accessibility.
Summary
bundle-file turns multilingual attachment handling from manual path maintenance into rule-based automatic lookup.
For long-lived knowledge bases and technical blogs, this reduces broken-link risk and pre-release review time.