Baseof template not using languageCode set in hugo.toml

Hello,
This is what my hugo.toml file looks like:

# ----------------------------------------------------------------------------
# General configuration
# ----------------------------------------------------------------------------
title        = "New Hugo Site"
languageCode = "hu"
theme        = "mytheme"

I need Hugo to apply the languageCode from hugo.toml, and default to English when no language code is specified. I tried this in the baseof.html file:

<html lang="{{ .Site.LanguageCode | default "en" }}">

It appears that the languageCode defined in hugo.toml is not being applied, as the language code remains set to en.

Can anyone offer suggestions into what might be causing this problem and how to address it?

Thank you!

Please share the entire site configuration file.

I want en-US not the default en.

# ----------------------------------------------------------------------------
# General configuration
# ----------------------------------------------------------------------------
title        = "New Hugo Site"
baseURL      = "localhost"
languageCode = "en-US"
theme        = "basic"
# The number of posts per page
paginate     = 2

pygmentsUseClasses     = true

# Fall back to English if no translations are available
defaultContentLanguage = "en"

# Disabled languages
# e.g. ["hu", "es", "de"]
disableLanguages       = []

# ----------------------------------------------------------------------------
# Default site params
# ----------------------------------------------------------------------------
[params]
  author      = "Your Name"
  description = "New Hugo Site"
  copyright   = "Copyright {currentYear} Zoltan. All rights reserved."
  images      = ["site-social.png"]
  twittercard = true
  opengraph   = true

# ----------------------------------------------------------------------------
# Languages
# ----------------------------------------------------------------------------
[languages]

  # English
  # --------------------------------------------------------------------------
  [languages.en]
    weight = 1
    languageName = "English"

    # Menu
    [[languages.en.menu.main]]
      name   = "Home"
      url    = "/"
      weight = 1
    
    [[languages.en.menu.main]]
      name   = "About Us"
      url    = "/about/"
      identifier = "about"
      weight = 2
    
    [[languages.en.menu.main]]
      name   = "Blog"
      url    = "/posts/"
      weight = 3
    

  # Hungarian
  # --------------------------------------------------------------------------
  # Override general configuration
  [languages.hu]
    title        = "Új Hugo Weboldal"
    languageCode = "hu"
    languageName = "Hungarian"
    weight       = 2
  
    # Override default site params
    [languages.hu.params]
      description  = "Új Hugo Weboldal"
      copyright    = "Copyright {currentYear}  Zoltan. Minden jog fenntartva."

    [[languages.hu.menu.main]]
      name   = "Címlap"
      url    = "/"
      weight = 1

    [[languages.hu.menu.main]]
      name   = "Rólunk"
      url    = "/about/"
      identifier = "about"
      weight = 2

    [[languages.hu.menu.main]]
      name   = "Blog"
      url    = "/posts/"
      weight = 3

# ----------------------------------------------------------------------------
# Posts
# ----------------------------------------------------------------------------
# Permalinks
[permalinks]
  posts = "posts/:year/:month/:slug"
  year  = "/posts/:slug/"
  month = "/posts/:slug/"

# Taxonomies
[taxonomies]
  year     = "year"
  month    = "month"
  tag      = "tags"
  category = "categories"

# Related content
[related]
  threshold    = 80.0
  includeNewer = false
  toLower      = false

  [[related.indices]]
    name   = "keywords"
    weight = 100.0
  
  [[related.indices]]
    name   = "date"
    weight = 10.0

# ----------------------------------------------------------------------------
# Security
# ----------------------------------------------------------------------------
[security]
  [security.exec]
    allow = ['^go$', '^npx$', '^postcss$', '^babel$']

Set a languageCode for each language.

1 Like

Thank you. It works.

As for the baseof template, is it still necessary to use the default "en" in? :

<html lang="{{ .Site.LanguageCode | default "en" }}">

or just having <html lang="{{ .Site.LanguageCode }}"> suffice? I’m asking this question because my site was set up using an older Hugo release from around two years ago, which used config.toml instead of hugo.toml. I thought the default behavior might now be managed automatically.

.Site.LanguageCode always returns something other than an empty string:
https://gohugo.io/methods/site/language/#methods

LanguageCode
(string) The language code from the site configuration. Falls back to Lang if not defined.

In older versions .Site.LanguageCode did not fall back to anything. I’m not sure when we fixed that, but it was within the last couple of years.

1 Like

Thank you. I’ve noticed that the Ananke theme, which I understand is a good reference for Hugo templating, uses the following:

<html lang="{{ site.LanguageCode | default site.Language.Lang }}">

Interestingly, it uses just site rather than .Site like in my case. So, could I use just site without that dot context? Also, a default value is specified. I think the example from Ananke might be more correct, am I right?

1 Like

At some point in time it was more correct but, as I mentioned, .Site.Language.LanguageCode now falls back to .Site.Language.Lang if you do not set languageCode in your site configuration. Or, to put it another way, these are equivalent:

{{ .Site.Language.LanguageCode }}
{{ .Site.Language.LanguageCode | default .Site.Language.Lang }}

Although you can use .Site.LanguageCode instead of .Site.Language.LanguageCode, I would use the second one. The first one is not documented and may be removed in a future release.

Finally, as @nfriedli referenced, in almost all cases you can use the global site function to get to the desired Site object. For your use case, these are equivalent:

{{ .Site.Language.LanguageCode }}
{{ site.Language.LanguageCode }}
2 Likes

Thank you so much for the clarification. I appreciate your time.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.