HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Diagrams
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Syntax highlighting
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Local file templates
    • Internal templates
    • Render hooks
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Variables
    • Overview
    • File variables
    • Git variables
    • Menu entry variables
    • Page variables
    • Pages variables
    • Shortcode variables
    • Site variables
    • Taxonomy variables
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
FUNCTIONS LANG FUNCTIONS

lang.Translate

Translates a string using the translation tables in the i18n directory.

Syntax

lang.Translate KEY [CONTEXT]

Returns

string

Aliases

T
i18n

The lang.Translate function returns the value associated with given key as defined in the translation table for the current language.

If the key is not found in the translation table for the current language, the lang.Translate function falls back to the translation table for the defaultContentLanguage.

If the key is not found in the translation table for the defaultContentLanguage, the lang.Translate function returns an empty string.

To list missing and fallback translations, use the --printI18nWarnings flag when building your site.

To render placeholders for missing and fallback translations, set enableMissingTranslationPlaceholders to true in your site configuration.

Let’s say your multilingual site supports two languages, English and Polish. Create a translation table for each language in the i18n directory.

i18n/
├── en.toml
└── pl.toml

The translation tables can contain both:

  • Simple translations
  • Translations with pluralization

The Unicode CLDR Plural Rules chart describes the pluralization categories for each language.

The English translation table:

i18n/en.
     
day:
  one: day
  other: days
day_with_count:
  one: '{{ . }} day'
  other: '{{ . }} days'
privacy: privacy
security: security
privacy = 'privacy'
security = 'security'
[day]
  one = 'day'
  other = 'days'
[day_with_count]
  one = '{{ . }} day'
  other = '{{ . }} days'
{
   "day": {
      "one": "day",
      "other": "days"
   },
   "day_with_count": {
      "one": "{{ . }} day",
      "other": "{{ . }} days"
   },
   "privacy": "privacy",
   "security": "security"
}

The Polish translation table:

i18n/pl.
     
day:
  few: miesiące
  many: miesięcy
  one: miesiąc
  other: miesiąca
day_with_count:
  few: '{{ . }} miesiące'
  many: '{{ . }} miesięcy'
  one: '{{ . }} miesiąc'
  other: '{{ . }} miesiąca'
privacy: prywatność
security: bezpieczeństwo
privacy = 'prywatność'
security = 'bezpieczeństwo'
[day]
  few = 'miesiące'
  many = 'miesięcy'
  one = 'miesiąc'
  other = 'miesiąca'
[day_with_count]
  few = '{{ . }} miesiące'
  many = '{{ . }} miesięcy'
  one = '{{ . }} miesiąc'
  other = '{{ . }} miesiąca'
{
   "day": {
      "few": "miesiące",
      "many": "miesięcy",
      "one": "miesiąc",
      "other": "miesiąca"
   },
   "day_with_count": {
      "few": "{{ . }} miesiące",
      "many": "{{ . }} miesięcy",
      "one": "{{ . }} miesiąc",
      "other": "{{ . }} miesiąca"
   },
   "privacy": "prywatność",
   "security": "bezpieczeństwo"
}

The examples below use the T alias for brevity.

When viewing the English language site:

{{ T "privacy" }} → privacy
{{ T "security" }} → security

{{ T "day" 0 }} → days
{{ T "day" 1 }} → day
{{ T "day" 2 }} → days
{{ T "day" 5 }} → days

{{ T "day_with_count" 0 }} → 0 days
{{ T "day_with_count" 1 }} → 1 day
{{ T "day_with_count" 2 }} → 2 days
{{ T "day_with_count" 5 }} → 5 days

When viewing the Polish language site:

{{ T "privacy" }} → prywatność
{{ T "security" }} → bezpieczeństwo

{{ T "day" 0 }} → miesięcy
{{ T "day" 1 }} → miesiąc
{{ T "day" 2 }} → miesiące
{{ T "day" 5 }} → miesięcy

{{ T "day_with_count" 0 }} → 0 miesięcy
{{ T "day_with_count" 1 }} → 1 miesiąc
{{ T "day_with_count" 2 }} → 2 miesiące
{{ T "day_with_count" 5 }} → 5 miesięcy

In the pluralization examples above, we passed an integer in context (the second argument). You can also pass a map in context, providing a count key to control pluralization.

Translation table:

i18n/en.
     
age:
  one: '{{ .name }} is {{ .count }} year old.'
  other: '{{ .name }} is {{ .count }} years old.'
[age]
  one = '{{ .name }} is {{ .count }} year old.'
  other = '{{ .name }} is {{ .count }} years old.'
{
   "age": {
      "one": "{{ .name }} is {{ .count }} year old.",
      "other": "{{ .name }} is {{ .count }} years old."
   }
}

Template code:

{{ T "age" (dict "name" "Will" "count" 1) }} → Will is 1 year old.
{{ T "age" (dict "name" "John" "count" 3) }} → John is 3 years old.

In this section

  • lang.FormatAccounting
  • lang.FormatCurrency
  • lang.FormatNumber
  • lang.FormatNumberCustom
  • lang.FormatPercent
  • lang.Merge
  • lang.Translate
Last updated: December 15, 2023: Disable TOC for lang.Translate (743497ed)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Content management
  • Templates
  • Functions
  • Methods
  • Quick reference
  • Variables
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance