Share front matter between translations - hugo

I have a Hugo site with translations per file, using page bundles.
So the About page looks like this:
- about
- about-image.jpg
- index.en.md
- index.nl.md
- index.fr.md
The problem is that I have to repeat the non-i18n front matter in all of the .md files.
For example, date, tags, et cetera.
Is there a way in hugo to define the (basic) front matter once, and have translations only contain overrides needed for i18n?

You can define the default language in the config file of the site.
Then, you set the strings that do not need to be translated only into the translation file of the default language, and you do not define them in the other files.
As the translations are missing, Hugo will default to the default language (if defined in the config file).
https://gohugo.io/content-management/multilingual/#missing-translations

Hugo now brings with it front-matter cascade param which you can use to make descendant pages or even translation pages like
cascade:
- _target:
kind: page
lang: en
path: /blog/**
background: yosemite.jpg
- _target:
kind: section
background: goldenbridge.jpg
title: Blog
Learn more here https://gohugo.io/content-management/front-matter#front-matter-cascade

Related

Set a localized slug also for the pages belonging to default language in Hugo

I have two languages defined on the Hugo site. By default, Hugo doesn't add a language prefix to the pages that belong to the default language.
In my case, I would also like to prefix the pages for the default language with the language prefix. And that would also require that pages of the default language to be generated into subfolder in public.
I would also like to avoid using aliases in markdown files.
Is this possible?
For prefix to the default language you need to add in your config.toml file one option:
defaultContentLanguageInSubdir = true
Render the default content language in subdir, e.g. content/en/. The site root / will then redirect to /en/.
Information from here - Configuration File Documentation
Output examples:
http://localhost:1313/about/ - without the option
http://localhost:1313/eu/about/ - with the option
But I do not know if it will satisfied your required about "subfolder in public".

How to use the same layout in a different directory?

I have a site using the hugo-coder theme, which has a layouts/posts folder that specifies that anything in the "posts" folder will have a blog post format.
I would like to have two different blogs in two different subdirectories, using the same layout. Is there a way to tell Hugo that the content/blog1 directory should use the same settings and layout as the content/posts directory without copying themes/hugo-coder/layouts/posts into layouts/blog1? Ideally I would avoid using symlinks, because, while convenient, I've had a decent amount of software throw weird errors when I use symlinks, so I avoid them when it's possible.
You can set the layout or type field to posts in the frontmatter of your _index.md file in content/blog1.
See this docs page for more info.
Edit: Alternatively, you could create an archetype for blog1 that automatically sets the value to posts in the frontmatter of individual posts in that section, assuming you're using hugo new blog1/postname.md to create posts for that section.
Double edit: The first suggestion didn't work. You could also create subsections within content/posts/blog1 and set the permalinks of posts in that subsection to use the last section only. That should remove the need to explicitly set the type in post frontmatter every time because each post would already have a type of posts.
In config.toml:
[permalinks]
posts = "/:sections[last]/:slug/"
You can use a partial in your templates. If you do that you WILL need the single and list file in the layouts/blog directory, but it could be an empty file referencing the partial. The layouts/posts/single.html and the layouts/blog/single.html both will then look like this:
{{ partial "singleblog.html" . }}
Compeletely DRY... and without much complexity.

Hugo change layout

I have a following structure in my Hugo & Doks project:
- content
- docs
- working.md
- working-as-well.md
- get-started2
- working.md
...
- get-started.md
...
- layouts
- get-started2
- single.html
- get-started.html
Content placed in docs and get-started2 has customized layout and works as expected. Great!
However, I have huge pain to set-up custom layout for get-started.md. I cannot do it like with get-started2 - place it into a folder. Client requests to have get started URL as:
www.SomeProject.com/get-started
so it must not be placed in folder (like get-started2)
www.SomeProject.com/get-started2/working
Do you have any hints please?
You have at least three options:
Use front matter to set the "type":
In content/get-started.md set type: mytype in front matter.
The layout will be located at (for example) layout/mytype/single.html.
Use front matter to set the "layout":
In content/get-started.md set layout: mylayout in front matter.
The layout will be located at (for example) layout/_default/mylayout.html
Or use a subdirectory, but make it the section index:
The file is located at content/get-started/_index.md
The layout will be located at (for example) layout/get-started/list.html
Notice that the template name has changed from "single" to "list", but you can still write your layout in exactly the same way.
I use "(for example)" because Hugo has an enormous list of directories/filenames it searches through to find its templates, and if an earlier matching template is found, the custom layout gets ignored.

How can I insert an emoji on config.toml in Hugo rendered website?

I want to add a cloud emoji on my webpage's footer, but apparently the config.toml file doesn't support emojis
[params]
footerText = ":cloud: David Jorquera - 2020"
...is rendered as text.
How can this be done?
Why that doesn't work?
Hugo uses the emojify function to parse templates for emojis substitution. But it doesn't apply such functions to the configuration.
To do so, you would have needed to pass your variable to the function in the template using {{ .footerText | emojify }} as documented in this other question.
Use directly UTF-8 characters in configuration
Your TOML configuration file can be considered as an UTF-8 document.
As such, you can directly add the emoji in the config value:
[params]
footerText = "☁️ David Jorquera – 2020"
This sample uses an emoji and a dash between your name and the year.

Is it possible for an entry to have two URL in Expression Engine, and translate template names?

I'm currently making a bilingual Expression Engine 2.5.2 website. I'm using this technique to create the two langues, which works perfectly.
I have created a {country_code} global variable in the two index.php files which allows me to detect the current language.
Using this technique, I have no problems to get language-relative data when accessing an entry. My only concern is that I apparently have to privilege a language-specific "clean" URL.
Example entry:
{entry_id} = 123
{title} = My test article
{title_permalink} = my-test-article
{name_fr} = Mon article
{name_en} = My article
If I request http://www.example.com/index.php/en/blog/articles/my-test-article, I expect to to find, in english, "My article" using the template articles in the blog template group.
Everything is fine, but the french translation is accessible when requesting http://www.example.com/index.php/fr/blog/articles/my-test-article. The correct translation of the URL should be http://www.example.com/index.php/fr/blogue/articles/mon-article-test.
Anyone encountered a problem like this? Any solutions via extensions or modules?
I believe the Transcribe module solves this by both providing the ability to translate template group and template names, and having you create a separate entry for each language and piece of content in your site (hence, you have two separate URL titles). But that means buying into their entire methodology for a multi-lingual site.
Myself, I usually just stick to using the entry_id instead of the url_title, and live with the template names being in the primary language.
The best way I found to achieve this is by embedding templates with segment translations, duplicating template groups and duplicating channels.
In the blog/articles template:
{embed="shared/.head" segment_2_translation="blogue" segment_3_translation="articles"}
In the blogue/articles template:
{embed="shared/.head" segment_2_translation="blog" segment_3_translation="articles"}
In shared/.head template:
[...] {if lang == "fr"}English{if:else}Français{/if} [...]
And then you can create a Articles (FR) and a Articles (EN) channels, and each will have their unique URL titles. You can also add a relationship custom field for each channel to associate an entry with it's translation.
It feels messy, but it is the only way I could make it work without modules, plugins or whatnot.

Resources