Skip to content

Languages

Add a second language end to end, keep heading anchors aligned across translations, and let readers compare two languages side by side.

OINK uses Hugo’s multilingual model as it is: a languages block in hugo.yml, and each translation saved beside its original with a language suffix. Adding a language touches five places: the configuration, the translated pages, the menus, the home page data and, only if you want to reword them, the theme’s interface strings. The language picker, hreflang links and per-language search follow from those without further setup.

Enable a second language

hugo.yml
defaultContentLanguage: en

languages:
  en:
    label: English
    locale: en-US
    weight: 1
    title: Acme DB Docs
    params:
      description: Documentation for the Acme DB time-series database
  zh:
    label: 简体中文
    locale: zh-CN
    weight: 2
    title: Acme DB 文档
    params:
      description: Acme DB 时序数据库文档
      time_format_default: 2006年1月2日
      time_format_blog: 2006年1月2日
  • label is the name in the language picker, written in that language: 简体中文, not Chinese.
  • locale is the standard language tag used for <html lang>, the hreflang alternate links and Open Graph metadata.
  • weight orders the languages, lowest first, in the picker and in its one-click cycle.
  • params overrides site parameters for this language; any key not set here keeps the global value. Date formats usually need a per-language value.

The default language is served without a path prefix (English at /docs/…) and every other language under its own (Chinese at /zh/docs/…). To prefix the default language too, add defaultContentLanguageInSubdir: true; on a live site this changes every URL, so set up redirects at the same time.

Translate the pages

Save each translation beside its original with the language code before the extension. Hugo pairs files that share a base name, so these are one page in two languages:

  • content/docs/
    • install.mdEnglish
    • install.zh.mdChinese
    • _index.md
    • _index.zh.md

Page bundles work the same way, with index.md and index.zh.md in one directory. Inside a bundle, a resource without a language suffix is shared by every language and a suffixed one belongs to that language only. Reference a suffixed resource without its suffix, ![Screenshot](screenshot.webp), and Hugo picks the file for the current language:

  • content/docs/install/
    • index.mdEnglish page
    • index.zh.mdChinese page
    • topology.webpused by both languages
    • screenshot.zh.webpused by the Chinese page only

A bundle that has only index.zh.md needs the .zh. suffix on every resource. Unsuffixed resources belong to the default language, which has no page in that bundle, so the Chinese page would not see them.

Translate what readers see and keep what Hugo uses for structure identical:

  • Translate: title, description, summaries, menu labels, tag names, image alt text, callout bodies and reader-facing shortcode parameters.
  • Keep identical: dates, weight, aliases and anything else that affects routing or order; a different weight puts the page in a different sidebar position in each language.
  • Leave untranslated: commands, configuration keys, file names, URLs, version numbers, product names and shortcode names.

Menus, home page and interface strings

Three things live outside content/ and need one copy per language.

Menus are written under each language:

hugo.yml
languages:
  zh:
    menus:
      main:
        - identifier: docs
          name: 文档
          pageRef: /docs
          weight: 20

Use the same identifier in every language, because the command palette’s quick links and the grouping of search results are matched by it. The full menu syntax is in Navigation and menus.

Home page data is one file per language, data/home/en.yaml and data/home/zh.yaml; a language without a file falls back to en.yaml. See Home and landing pages.

Interface strings, such as the search placeholder and the page-action labels, are already translated: the theme ships complete, native catalogs for 32 locales (ar, az, bg, bn, de, en, es, et, fa, fi, fr, he, hi, hu, it, ja, ko, nl, no, oc, pl, pt-br, ro, ru, sr-cyrl, sr-latn, sv, tr, uk, zh, zh-cn and zh-tw). zh and zh-cn are Simplified Chinese and zh-tw Traditional. To reword a string, create a file of the same name in the site’s own i18n/ with only the keys you change; key names are in the theme’s i18n/en.yaml, and any placeholder such as {{ .Count }} in the original must stay in your version.

i18n/en.yaml
ui_search: Search the docs

Align heading anchors

Hugo derives a heading’s ID from its text, so a translated heading gets a different ID: /docs/install/#prerequisites and /zh/docs/install/#前置条件 are the same section under two anchors. A deep link copied from one language then fails in the other, every cross-reference has to be written twice, and Duo View cannot keep its two panes at the same section. Give the translated heading the original’s ID explicitly:

install.zh.md
## 前置条件 {#prerequisites}

Take the ID from the English page’s rendered HTML rather than guessing it from the heading text, because inline code, badges and shortcodes in a heading change the generated ID. Keep the same headings, in the same order, with the same IDs on both sides; a section that exists in only one language gets its own stable ID that does not collide with the other’s. Adding {#id} when you write a page costs far less than retrofitting it.

To compare one translated pair after a build:

ids() { grep -oE '<h[2-6][^>]* id="?[^" >]+' "$1" | sed -E 's/.*id="?//'; }
diff <(ids public/docs/install/index.html) <(ids public/zh/docs/install/index.html)

To check every pair in CI, the OINK documentation site runs check-doc-translations.mjs, which compares source structure and rendered heading IDs. It is written for that repository’s content sections and EN/ZH file names, so adapt both before running it on another site.

Missing translations and the language picker

The language picker’s icon is itself a link to the next language by weight, wrapping round after the last, so a bilingual site switches with one click. Hovering over it or focusing it opens a menu of every language; on a touch screen a tap switches directly.

The menu always lists every configured language. Choosing one opens the translation of the current page when there is one, and that language’s home page when there is not. The home page beats a 404, but readers may not notice they have moved, so treat “every page has a translation” as a rule to check rather than relying on the fallback.

A missing translation is never filled in with the original

If a page has no Chinese version, the Chinese site has no such page: its sidebar, search index and page order leave it out.

Search engines see only real translations: hreflang lists the current page and its actual translations, never the home page fallback. See canonical URLs and language alternates. Local search also works per language, so a search on a Chinese page finds Chinese content, including unspaced Chinese, mixed-language queries and technical identifiers; see Search.

Duo view

Duo View lets a reader open a page beside one of its translations, which helps anyone checking a translation or reading in a second language. Turn it on with:

hugo.yaml
params:
  ui:
    compare: true

On Docs, Book and blog pages with a published translation on the same site, an Open duo view button appears beside the page actions. Readers can also start Duo View from the command palette, or press B when keyboard navigation is on (try it on this page), and press B again to return. The current page stays on the side where reading starts and the other pane loads the translation; on a page with more than two languages, readers pick the target language from the toolbar menu. Below 900px the view becomes a single pane with a language toggle bar.

The Sync Scroll switch keeps both panes at the same section by matching heading IDs, which is why aligned anchors matter here. When some headings have no counterpart, or the pages share none, it estimates the position between matching headings or from overall progress, and an orange ! beside the switch says which; hover, focus or tap it to read the note. When heading IDs are duplicated or ordered differently between the languages, Sync Scroll turns itself off and both panes scroll independently.

Duo View adds no pages to the build: only when a reader opens it does the browser load the two existing HTML pages, in same-origin frames. Pages without a translation, drafts, home page fallbacks and translations hosted on another domain are left out automatically. To keep one language out entirely, both as the page being read and as the translation shown, set compare: false under languages.<lang>.params.ui.

Before enabling it in production, check two things:

  • Analytics snippets in hooks/head-end.html run again inside each pane unless you guard them with window.OinkComparePane; see Analytics.
  • Your Content Security Policy must allow frame-src 'self', and the server must not send X-Frame-Options: DENY or frame-ancestors 'none'; see Content Security Policy.

Right-to-left languages

Declare the writing direction on the language:

hugo.yml
languages:
  ar:
    label: العربية
    locale: ar
    direction: rtl
    weight: 3

<html dir> follows this setting and the theme adds Bootstrap’s RTL stylesheet. The theme’s own CSS uses logical properties, such as margin-inline-start rather than margin-left, so the layout mirrors by itself. Use logical properties in your own CSS too, or your elements will sit on the wrong side in RTL.

Verify

  1. Build from the site root and confirm that both languages produced output. These paths assume English at / and Chinese at /zh/; the index check applies only with local search on:

    hugo --printPathWarnings
    ls public/index.html public/zh/index.html
    ls public/offline-search-index.*
  2. Check the hreflang links of a translated page against SEO behavior: they list only real translations, each blog pagination page has its own canonical URL, and pages after the first omit language alternates.

    # Replace with a page your site actually generates.
    PAGE=public/zh/docs/getting-started/index.html
    test -f "$PAGE" && grep -o '<link[^>]*hreflang[^>]*>' "$PAGE"
  3. On a translated page, switch language from the picker and confirm that the same document opens; on an untranslated page, confirm that the other language’s home page opens rather than a 404.

  4. Search for the same concept in each language and confirm both return results.

  5. Compare the heading IDs of a translated pair as shown under Align heading anchors.