Is there a way to use string localisations in content pages?

Hi,

Is there any way i could use {{ i18n “custom_key” }} in the _index.md/_index.html file in the content directory? I have already created two files as such i18n/en.yaml i18n/es.yaml

Create a shortcode.

layouts/shortcodes/T.html

{{ T (.Get 0) }}

markdown

{{< T custom_key >}}
1 Like

Thank you, That worked!

Change the shortcode to this instead:

{{- T (.Get 0) -}}

That will trim trailing white space (edge case).

1 Like

Thank you… I am facing another issue…

when I build the project… I only see english translations but not spanish. I have the following directory structure

i18n/en.toml
[KeyTA]
other = "Hello World"
i18n/es.toml
[KeyTB]
other = "Hola World"

content/homepage.html

---
title: Something
---
{{ <T KeyTA >}}
{{ <T KeyTB >}}
Current Build: (With translations)
public/en/homepage.html

Expected: (With translations)
public/en/homepage.html
public/es/homepage.html

Is this the right way of doing it… ? I recall this is possible in middleman but middleman is dead slow when building 100’s of pages.

What does your site configuration look like?

baseURL = 'http://localhost:1313'
languageCode = 'en-us'
title = 'Project'
disableKinds = ["home", "section", "taxonomy", "term", "RSS", "sitemap", "robotsTXT", "404"]

defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[Languages]
  [Languages.en]
    languageName = "English"
    weight = 1

  [Languages.es]
    languageName = "Spanish"
    weight = 2

I am expecting only pages to be rendered so I have disabled all other types.

You also disabled home.

Is home mandatory?

What does your content directory look like?

content/page1.html
content/page2.html
content/pageN.html

Removing “home” from disableKinds array seemed to generate “es” folder but there are no translated content of page1 or page2 or page3 (no pages generated)

only the en folder contains the page1, page2, page3 (All pages loading the translations from en.toml)

content

content/
├── homepage.es.md
└── homepage.md

public

public/
├── en/
│   └── homepage/
│       └── index.html
├── es/
│   └── homepage/
│       └── index.html
└── index.html

If you want this instead:

public/
├── en/
│   └── homepage.html
├── es/
│   └── homepage.html
└── index.html

Do this in your site configuration:

uglyURLs = true

oh… I was trying to avoid this implementation. I don’t want the content to be duplicated because I basically have buttons, links and forms in these pages. I wanted to avoid duplication of html code.

!! My expectation is to generate !!

en/homepage.html
es/homepage.html

!! from !!

content/homepage.html

Does Hugo has this capability?

Yes, but why are placing buttons and forms in content pages, instead of templates?

because they are not common. Each page is different and no html is common. Hence I cannot move them to templates.

for example pageA.html can have

<button>{{ <T submit_button> }}</button>

and pageB.html has

<button>{{ <T move_to_step_n> }}</button>

To be honest… Every page is unique in its own way but requires two HTML’s generated (en & es) from a single content file

config.toml
baseURL = 'http://example.org/'
languageCode = 'en-us'
title = 'Project'
disableKinds = ["home", "section", "taxonomy", "term", "RSS", "sitemap", "robotsTXT", "404"]

defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true

#------------------------------------------------------------------------------
# Languages
#------------------------------------------------------------------------------

[languages.en]
languageName = 'English'
weight = 1

[languages.es]
languageName = 'Spanish'
weight = 2

#------------------------------------------------------------------------------
# Content mounts
#------------------------------------------------------------------------------

# EN content
[[module.mounts]]
source = 'content'
target = 'content'
lang = 'en'

# ES content
[[module.mounts]]
source = 'content'
target = 'content'
lang = 'es'

#------------------------------------------------------------------------------
# Fill in the missing translations
#------------------------------------------------------------------------------

# This fills in the gaps in ES content with EN content
[[module.mounts]]
source = 'content'
target = 'content'
lang = 'es'

content

content/
└── homepage.html

rendered

public/
├── en/
│   └── homepage/
│       └── index.html
├── es/
│   └── homepage/
│       └── index.html
└── index.html

Should these be identical? It seem to be repeated twice? Is that the reason why its not working for me?

Yes, they are two different things.

It doesn’t seem to be working for me. Should content/homepage.html be content/en/homepage.html ?

Here’s a working example:

git clone --single-branch -b hugo-forum-topic-41617 https://github.com/jmooring/hugo-testing hugo-forum-topic-41617
cd hugo-forum-topic-41617
hugo server