Thanks to visit codestin.com
Credit goes to github.com

Skip to content

langs/i18n: Improve default content language fallback #14243

@jmooring

Description

@jmooring

With #14225 we now look for:

  1. A translation table matching the current language's LanguageCode (new behavior), falling back to...
  2. A translation table matching the current language's key, falling back to...
  3. A translation table matching the defaultContentLanguage

That means that you can now do this, which is great:

defaultContentLanguage = 'en'

[languages.en]
languageCode = 'en-US'
weight = 1

[languages.de]
languageCode = 'de-DE'
weight = 2
i18n/
├── de-de.toml
└── en-us.toml

But for the last fallback (item 3 in the list above) to work, you must also have an en.toml file in the i18n directory, which isn't great. We should fallback to a translation table matching the LanguageCode of the defaultContentLanguage, then to a translation table matching the defaultContentLanguage.

Integration test:

func TestDefaultContentLanguageFallback14243(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
disableKinds = ['page','rss','section','sitemap','taxonomy','term']
defaultContentLanguage = 'es'
defaultContentLanguageInSubdir = true

[languages.es]
locale = 'es-AR'
weight = 1

[languages.pt]
locale = 'pt-BR'
weight = 2
-- layouts/home.html --
{{ T "foo"}}|
-- i18n/es-ar.toml --
foo = 'foo es-ar'
-- i18n/es.toml --
foo = 'foo es'
-- i18n/pt-br.toml --
foo = 'foo pt-br'
-- i18n/pt.toml --
foo = 'foo pt'
`

	b := hugolib.Test(t, files)
	b.AssertFileContent("public/pt/index.html", "foo pt-br|")

	files = strings.ReplaceAll(files, "i18n/pt-br.toml", "unused-a.toml")

	b = hugolib.Test(t, files)
	b.AssertFileContent("public/pt/index.html", "foo pt|")

	files = strings.ReplaceAll(files, "i18n/pt.toml", "unused-b.toml")

	b = hugolib.Test(t, files)
	b.AssertFileContent("public/pt/index.html", "foo es-ar|")

	files = strings.ReplaceAll(files, "i18n/es-ar.toml", "unused-c.toml")

	b = hugolib.Test(t, files)
	b.AssertFileContent("public/pt/index.html", "foo es|")
}

cc: @chalin

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions