Hugo

Is it possible to use translations in assets?

Hello,

Is it possible to use i18n translations in assets?
This is how I process my JS.

{{ $scriptJS := resources.Get "js/script.js" | resources.ExecuteAsTemplate "js/script.js" . | resources.Minify | fingerprint }}

And I’m using translations in an usual way:

{{T "translations_key"}}

I thought my assets would get translations when using ExecuteAsTemplate but I’m only getting my JS file with English translations.
I either got lost in documentation or it’s not possible to use translations in assets.

You can use resources as template. But I have seen it only with SCSS files up to now. Check this out:

@pkollitsch yeah I’m already using this to inject config values to my JS files. But I can’t seem to get translations working in the way I was hoping to.

I’m guessing a little here, but my best guess would be that this is a caching/naming issue.

When you do resources.ExecuteAsTemplate "js/script.js"`` we create a resource with that relative URL:js/script.js`.

Which would effectively be shared between the languages.

Try:

{{ $scriptJS := resources.Get "js/script.js" | resources.ExecuteAsTemplate  (printf "%s/js/script.js" $.Site.Lang ) . | resources.Minify | fingerprint }}


@bep tried putting language prefix but still no success. I get JS files for every languages but only with one language translations. Will try to do more testing but I suspect this has something to do with how i18n works?

Hmm. Can you create a GitHub issue and I’ll look at it.

https://github.com/gohugoio/hugo/issues/6331