Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: x/text/cmd/gotext: support extracting literals from templates #51144

jagobagascon opened this issue Feb 11, 2022 · 5 comments


Copy link

jagobagascon commented Feb 11, 2022

I'm working on a simple web app that uses html/template to generate and serve HTML files. The HTML templates include text that we want to internationalize, and I was trying to use x/text and its gotext command tool to do that.

This what I'm trying to do

My goal was to define some kind of function to translate the text:

<button>{{i18n "Sign in"}}</button>

The i18n function just calls printer.Sprintf(key, args...)

funcs["i18n"] = func(key string, args ...interface{}) string {
    return printer.Sprintf(key, args...)

So I had a little bit of hope that gotext tool would extract the literals for me. But it did not work.

    "language": "en",
    "messages": null

This is understandable as there's no direct relation between the template function and the call to Sprintf. So I just created the messages.gotext.json manually and tried to use the gotext tool to generate the catalog with all translations. But that does not work either.

I took a quick look at gotext code, and I think that it is always extracting messages from code, and then removes the ones it thinks are unused before generating the catalog file.

This means that unless I move all the texts to Go, there's no way I can use gotext to generate a catalog for me.

Feature request

It would be nice to add automatic text extraction from templates to gotext. Most web applications are probably going to use templates to render the HTML, and most of the strings that will need translation belong there.

In the meantime, as a workaround (probably also easier to implement), it would be nice to provide a way to generate the catalog without removing unused messages. gotext cannot detect if messages from a template are being translated or not, so let me just generate a catalog with all the messages I have in the messages.gotext.json file.

@gopherbot gopherbot added this to the Unreleased milestone Feb 11, 2022
@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Feb 23, 2022
@seankhliao seankhliao changed the title x/text: support extracting literals from templates x/text/cmd/gotext: support extracting literals from templates Jun 7, 2022
@rsc rsc changed the title x/text/cmd/gotext: support extracting literals from templates proposal: x/text/cmd/gotext: support extracting literals from templates Jun 22, 2022
Copy link

Oh, so this is not supported. That's a shame I was just planning on doing this 😭

Copy link

dav-m85 commented Jul 19, 2022

@nicolasparada you can write an extraction script that'll grab all texts to be translated from your templates, provided you follow always the same syntax. I've gisted such a script here for your inspiration. I wire it with a go:generate statement, on every run, it parses all .html templates from a given dir, extracts all {{.T "to-be-translated}}" invokations, and put them in a json/go pair file usable by go/text.

Copy link

Thanks @dav-m85 😃
I'll take a look

Copy link

tv42 commented Jul 19, 2022

At a quick glance, html/template uses and you should be able to just rummage through it's parse tree.

Copy link

d2ro commented Apr 9, 2023

We also wrote a tool for that:

Edit: renamed package, updated link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Status: Incoming

No branches or pull requests

6 participants