You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...)
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.
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.
The text was updated successfully, but these errors were encountered:
seankhliao
changed the title
x/text: support extracting literals from templates
x/text/cmd/gotext: support extracting literals from templates
Jun 7, 2022
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
@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.
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 usex/text
and itsgotext
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:
The
i18n
function just callsprinter.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.This is understandable as there's no direct relation between the template function and the call to
Sprintf
. So I just created themessages.gotext.json
manually and tried to use thegotext
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 themessages.gotext.json
file.The text was updated successfully, but these errors were encountered: