You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在CKAN中实现数据集标题与描述的多语言翻译?

How to Translate Dataset Titles & Descriptions in CKAN (Like opendata.swiss)

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_en instead of the default title field.
  • 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_schema or 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.title and pkg.description with 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:09:56