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

Open
jagobagascon opened this issue Feb 11, 2022 · 0 comments
Open
Labels
Milestone

Comments

@jagobagascon
Copy link

@jagobagascon 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 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Proposals
Incoming
Development

No branches or pull requests

2 participants