akimo.dev

Use Disqus on multilingual Hugo site

The answer

window.disqus_config = function() {
	this.language = '{{ .Lang }}';
}

Why use window.disqus_config?

According to the Disqus' help page, we can override the language by adding this.

var disqus_config = function () {
	this.language = "ja";
};

So basically the following JavaScript works.

var disqus_config = function () {
	this.language = '{{ .Lang }}';
};

However, the variable disqus_config doesn't work with --minify option because Hugo changes the name.

To solve it, you can use a window object instead.

var disqus_config = function () {
	window.disqus_config = '{{ .Lang }}';
};

Reference:

Global object - MDN Web Docs Glossary: Definitions of Web-related terms | MDN
developer.mozilla.org

By the way...

Hugo has a Disqus template, and it was using var disqus_config so I sent a pull request. I was happy because it's my first contribution to OSS.

Change `disqus_config` to `window.disqus_config` by akimon658 · Pull Request #9550 · gohugoio/hugo
github.com