How to specify the build dir for specific language?

I’m using the congo theme with two languages: en and zh-tw. In the latest update, the theme changes the language code from zh-tw to zh-Hant.

To accommodate the latest change, I’m thinking of changing all zh-tw to zh-Hant and specifying the baseURL parameters on both languages like so:

# languages.zh-Hant.toml
baseURL = "https://blog.tomy.me/zh-tw/"
languageCode = "zh-Hant"
languageName = "πŸ‡ΉπŸ‡Ό δΈ­ζ–‡ (台灣)"
languageDirection = "ltr"
weight = 1
hasCJKLanguage = true
...
# languages.en.toml
baseURL = "https://blog.tomy.me/en/"
languageCode = "en"
languageName = "πŸ‡ΊπŸ‡Έ English (US)"
languageDirection = "ltr"
weight = 2
hasCJKLanguage = false
...

Just so that it will still serve my website as follows as it always has been:

  • English site: https://blog.tomy.me/en/
  • Chinese site: https://blog.tomy.me/zh-tw/

This works fine on local with hugo server:


                   | ZH-HANT | EN   
-------------------+---------+------
  Pages            |     261 |  48  
  Paginator pages  |      24 |   0  
  Non-page files   |     326 |  21  
  Static files     |       8 |   8  
  Processed images |    2207 | 100  
  Aliases          |      73 |   8  
  Cleaned          |     100 | 100  

Built in 9320 ms
Environment: "development"
Serving pages from disk
Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender
Web Server is available at http://localhost:1313/zh-tw/ (bind address 127.0.0.1) zh-hant
Web Server is available at http://localhost:1314/en/ (bind address 127.0.0.1) en
Press Ctrl+C to stop

But when I attempt to build it, the built site looks something like this:

public
β”œβ”€β”€ en
β”‚   β”œβ”€β”€ css
β”‚   β”œβ”€β”€ img
β”‚   β”œβ”€β”€ index.html
β”‚   β”œβ”€β”€ index.json
β”‚   β”œβ”€β”€ index.xml
β”‚   β”œβ”€β”€ js
β”‚   β”œβ”€β”€ lib
β”‚   β”œβ”€β”€ posts
β”‚   └── ...(omitted)
└── zh-hant
    β”œβ”€β”€ css
    β”œβ”€β”€ img
    β”œβ”€β”€ index.html
    β”œβ”€β”€ index.json
    β”œβ”€β”€ index.xml
    β”œβ”€β”€ js
    β”œβ”€β”€ lib
    β”œβ”€β”€ posts
    └── ...(omitted)

The Chinese site is being built at /zh-Hant :confused:

When I inspect the built pages, I notice the baseURL parameter is in effect that most anchor to static assets are correct, such as <script src=/zh-tw/js/main.bundle.min.f64817246a3814fe8ac69eaa744f58dafd97df187e7f28274423d3de185d4625.js></script>.

I can hotfix this issue by just simply renaming the zh-Hant to zh-tw, but the question is: How should I configure Hugo to build it as I wish?

1 Like

I don’t understand why this is a problem. Can’t you just create your own i18n files for zh-tw?

A project with a different baseURL for each language is a multi-site setup (multiple hosts either physical or virtual), where each top level directory is the root of the respective sites. I don’t think you want a multi-site setup.

Reading through the discussion on the issue regarding this change, the major benefit to making such a change would be getting proper support on formatting dates, numbers, and such. For instance, a post date would be formatted as β€œ2024εΉ΄9月30ζ—₯” instead of β€œSeptember 30, 2024”. The language code has to be zh_Hant as that’s what Hugo supports, which is derived from here.

I noticed the β€œmulti-site setup” you were referring to. In the past, all static assets were stored at the root level, and now they are duplicated in each language directory.

So, the question remains: Is there a way to configure a custom path where each language is built? (Either in single-site or multi-site)

With a multilingual single-host site, the language prefix (the first segment of each URL path) will be the language key, with the exception of the default language if defaultContentLanguageInSubdir is false.

For example:

defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true

[languages.en]
languageCode = 'en-US' # This has no effect on URL
weight = 1

[languages.de]
languageCode = 'de-DE' # This has no effect on URL
weight = 2

The de site will be served from https://example.org/de/. You cannot override the language prefix. See Issue 9404.

If I were you I would serve the files from https://example.org/zh-hant/, and create a 301 redirect rule to rewrite requests made to https://example.org/zh-tw/.

2 Likes

Thanks for the suggestion! Will consider all options later on.