maximilien/i18n4go

Add support for Go's text/template templates.

Closed this issue · 6 comments

mfzl commented

Since Go is also used for developing web applications and generating HTML, I think this will be a worth while addition to workflows using this package.

  • One option is to support in extractor so it looks for things like this:
    {{T "Sample string"}}
  • Other is to parse HTML files and wrap text nodes with T. This IMO is unnecessarily complex, since text/template is not only for generating HTML so what constitutes as a string will be different for different templates.

If it is already present this issue can be closed, I couldn't find any information on it in README.

Hi, @faxal,

Great suggestion. Can you please include a complete example of what you mean by:

"""
One option is to support in extractor so it looks for things like this:
{{T "Sample string"}}
"""

Do you mean template HTML that contain strings would have these strings extracted? Or template Golang code that have strings in them?

Please clarify and perhaps cut and paste a complete simple hello world example. I could use that for testing as well.

Thanks,

@maximilien

mfzl commented

Do you mean template HTML that contain strings would have these strings extracted? Or template Golang code that have strings in them?

I meant templates in separate files that is in text/template format.

Example: hello.html This assumes that user has already annotated the strings with predefined identifiers, goal is to only do extractions to generate translation files. Benefit is that text/template/parse could be used to parse these strings correctly.

<html>
   <body>
      {{T "String that needs to be extracted"}}
      <h4>Plurals</h4>
      {{TN "Apple", "Apples", 2}}
   </body>
</html>

Test case
For a test case I think this should suffice for now.

      {{T "Strings that needs to be extracted"}}
      {{TN "Apple", "Apples", 2}}
      {{TN "%d Apple", "%d Apples", 3}}

Above should result in: test.pot

#: reference
msgid "Strings that needs to be extracted"
msgstr ""

#: reference
msgid "Apple"
msgid_plural "Apples"
msgstr[0] ""
msgstr[1] ""

#: reference
msgid "%d Apple"
msgid_plural "%d Apples"
msgstr[0] ""
msgstr[1] ""



Apologies if I am failing to explain this, English is not my native language.

http://play.golang.org/p/I8nFoOxPKa

@faxal,

OK, so it's clear now. I was not sure but nothing like an example to make things clear.

The problem with supporting what you are suggesting is that I would need to have parsing support for these templates. For Golang templates, that's possible, but for HTML templates that's harder since I don't believe there are Golang parsing (natively in language libraries) for these templates. If you know of some then do let me know?

I can investigate adding support for Golang templates if that's useful for you? Note finally that Golang's template is what we are using for the actual substitution code once i18n4go converts your code to be i18n4go ready.

Let me know. Best,

@maximilien

mfzl commented

I understand that parsing HTML and trying to understand what constitute as a string is complex without any annotation. This is what I meant in the shared play. I personally don't need it.

I'm fine with the tool being working in the suggested way (with Go templates), I'm already doing something similar. But I don't have anything for the .go source files and I want to use these tools for both source files and template files.

As for HTML as you said there are none in standard library. But there is one in the extended libraries
golang.org/x/net/html

Thank you for considering this. I can help you with testing and development, with your guidance.

Hi, @faxal,

Let's do this. First try to use the tool in your current project as you see fit for .go files. If you have any problems open separate issue.

Once successful, let's revisit this current issue. The first step would be to have a nice example templated HTML file and modify the extract-string command with ability to parse HTML files and see if the parsed tree for your example file can be navigated to find the strings that need to be extracted.

If this first step can be achieved then chances are the rest will be possible. The next key unknown is whether it will be easy to modify the parsed tree and generate new code. That's what the rewrite-package does now.

I'll keep this issue open and wait for you to get back when ready.

Best,

@maximilien

This issue is staled for 30 days