Hugo: Support layouts per site/language

Created on 18 Apr 2017  ยท  2Comments  ยท  Source: gohugoio/hugo

See http://stackoverflow.com/questions/43149074/jekyll-or-hugo-theme-with-multilanguage-support

Hugo has already a good story for multiple languages, but it could fairly easily be made even more powerful by allowing template overrides per site/language.

Doing large portions of localisation via i18n files can be cumbersome, so to be able to create language-specific layout files/partials/shortcodes would be great.

A simple sample would be:

โ”œโ”€โ”€ _default
โ”‚ย ย  โ”œโ”€โ”€ baseof.html
โ”‚ย ย  โ”œโ”€โ”€ list.html
โ”‚ย ย  โ””โ”€โ”€ single.html
โ”œโ”€โ”€ _fr
โ”‚ย ย  โ”œโ”€โ”€ _default
โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ list.html
โ”‚ย ย  โ””โ”€โ”€ index.html
โ””โ”€โ”€ index.html

In the imaginary site above we have a base template set that is used for all sites/languages, but then we override some of the templates for the French language (fr). This would also work for themes.

I have prefixed the language with "_" to avoid (hopefully) any section collisions.

Enhancement

Most helpful comment

I've been wishing for exactly this as I've been reworking my site into Hugo. It'd be especially helpful for laying pages out according to different typographic conventions (I'm doing English/Japanese, and CVs are very different across the two).

I thought though, that the simpler way might be to use something like:

โ”œโ”€โ”€ _default
โ”‚   โ”œโ”€โ”€ baseof.html
โ”‚   โ”œโ”€โ”€ list.html
โ”‚   โ”œโ”€โ”€ list.ja.html
โ”‚   โ””โ”€โ”€ single.html
โ”‚   โ””โ”€โ”€ single.ja.html
โ””โ”€โ”€ index.html
โ””โ”€โ”€ index.ja.html

Using filename.ja.html or filename-ja.html, etc. That way, the translations sit side-by-side with each other in the same folder, it matches the translated content system, and presumably you could just double the template search paths (eg. from https://gohugo.io/templates/content/#single-page), like so:

  • /layouts/TYPE/LAYOUT.LANG.html
  • /layouts/TYPE/LAYOUT.html
  • /layouts/SECTION/LAYOUT.LANG.html
  • /layouts/SECTION/LAYOUT.html
  • /layouts/TYPE/single.LANG.html
  • /layouts/TYPE/single.html
  • /layouts/SECTION/single.LANG.html
  • /layouts/SECTION/single.html
  • /layouts/_default/single.LANG.html
  • /layouts/_default/single.html
  • /themes/THEME/layouts/TYPE/LAYOUT.LANG.html
  • /themes/THEME/layouts/TYPE/LAYOUT.html
  • /themes/THEME/layouts/SECTION/LAYOUT.LANG.html
  • /themes/THEME/layouts/SECTION/LAYOUT.html
  • /themes/THEME/layouts/TYPE/single.LANG.html
  • /themes/THEME/layouts/TYPE/single.html
  • /themes/THEME/layouts/SECTION/single.LANG.html
  • /themes/THEME/layouts/SECTION/single.html
  • /themes/THEME/layouts/_default/single.LANG.html
  • /themes/THEME/layouts/_default/single.html

Additionally, this shouldn't really disrupt any pre-existing sites, and be relatively easy to add in I think.

All 2 comments

I've been wishing for exactly this as I've been reworking my site into Hugo. It'd be especially helpful for laying pages out according to different typographic conventions (I'm doing English/Japanese, and CVs are very different across the two).

I thought though, that the simpler way might be to use something like:

โ”œโ”€โ”€ _default
โ”‚   โ”œโ”€โ”€ baseof.html
โ”‚   โ”œโ”€โ”€ list.html
โ”‚   โ”œโ”€โ”€ list.ja.html
โ”‚   โ””โ”€โ”€ single.html
โ”‚   โ””โ”€โ”€ single.ja.html
โ””โ”€โ”€ index.html
โ””โ”€โ”€ index.ja.html

Using filename.ja.html or filename-ja.html, etc. That way, the translations sit side-by-side with each other in the same folder, it matches the translated content system, and presumably you could just double the template search paths (eg. from https://gohugo.io/templates/content/#single-page), like so:

  • /layouts/TYPE/LAYOUT.LANG.html
  • /layouts/TYPE/LAYOUT.html
  • /layouts/SECTION/LAYOUT.LANG.html
  • /layouts/SECTION/LAYOUT.html
  • /layouts/TYPE/single.LANG.html
  • /layouts/TYPE/single.html
  • /layouts/SECTION/single.LANG.html
  • /layouts/SECTION/single.html
  • /layouts/_default/single.LANG.html
  • /layouts/_default/single.html
  • /themes/THEME/layouts/TYPE/LAYOUT.LANG.html
  • /themes/THEME/layouts/TYPE/LAYOUT.html
  • /themes/THEME/layouts/SECTION/LAYOUT.LANG.html
  • /themes/THEME/layouts/SECTION/LAYOUT.html
  • /themes/THEME/layouts/TYPE/single.LANG.html
  • /themes/THEME/layouts/TYPE/single.html
  • /themes/THEME/layouts/SECTION/single.LANG.html
  • /themes/THEME/layouts/SECTION/single.html
  • /themes/THEME/layouts/_default/single.LANG.html
  • /themes/THEME/layouts/_default/single.html

Additionally, this shouldn't really disrupt any pre-existing sites, and be relatively easy to add in I think.

@ngsctt I agree, that is much simpler than what I suggested.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bep picture bep  ยท  3Comments

crash-dive picture crash-dive  ยท  3Comments

geddski picture geddski  ยท  3Comments

moorereason picture moorereason  ยท  3Comments

MunifTanjim picture MunifTanjim  ยท  3Comments