如何在CKAN中实现数据集标题与描述的多语言翻译?
Hey there! I’ve dug into how opendata.swiss handles multilingual dataset content with their ckanext-switzerland extension, so let’s break this down clearly—since you noticed the i18n folder but didn’t see dataset-specific translations, that’s because those work differently from the extension’s UI text.
First: The Key Difference
The i18n folder in ckanext-switzerland is for translating extension UI elements (like button labels, form prompts, and system messages), not the actual dataset content (titles, descriptions, etc.). Dataset translations are stored directly in the dataset’s metadata as separate fields, not in PO/MO files.
How opendata.swiss Implements It
Here’s the core workflow they use:
1. Add Multilingual Fields to the Dataset Schema
The extension adds custom fields to the dataset metadata schema to store translations. For example:
title_de,title_en,title_fr,title_it(for titles in each supported language)description_de,description_en,description_fr,description_it(for descriptions)
These are defined in the extension’s schema.py file, where they extend CKAN’s default dataset schema to include these language-specific fields.
2. Update the Dataset Edit Form
The extension modifies the dataset editing form to add input fields for each language. This lets dataset creators/admins enter translated titles and descriptions directly when creating/editing a dataset.
3. Dynamically Render Content Based on Current Language
On the frontend (dataset detail pages, search results, etc.), the extension uses custom helper functions to pull the right field based on the user’s selected language. For example:
- If the user is viewing the site in English, the template will render
title_eninstead of the defaulttitlefield. - If a translation doesn’t exist for a language, it falls back to a default (usually German, for opendata.swiss).
You’ll find code like {{ h.switzerland_dataset_title(pkg) }} in the extension’s template files—this helper function handles the logic of selecting the correct title field based on the current language.
Steps to Implement This in Your Own CKAN Instance
If you want to replicate this:
- Extend the schema: In your custom extension, add language-specific fields to the dataset schema using
update_config_schemaor by overriding the default schema. - Customize the edit form: Add input fields for each language to the dataset edit template, or use CKAN’s form helpers to inject these fields dynamically.
- Build language-aware helpers: Create helper functions that check the current site language (via CKAN’s
h.lang()function) and return the corresponding translated field. Fall back to a default language if the translation is missing. - Update frontend templates: Replace references to
pkg.titleandpkg.descriptionwith your custom helper functions in all relevant templates (dataset detail, search results, etc.).
Quick Note on Language Switching
CKAN’s core language switcher (the header dropdown) sets the lang parameter, which your helper functions can use to determine which field to display. Make sure your helpers tie into this system to keep translations in sync with the user’s language selection.
内容的提问来源于stack exchange,提问作者davisoski

