Allow {% blocktrans %} translation in runtime #88

Closed
seriyps opened this Issue Aug 28, 2013 · 7 comments

Comments

Projects
None yet
2 participants
Member

seriyps commented Aug 28, 2013

Like in Django.

In Django {% blocktrans %} can contain only simple {{ var }} substitutions, so, as I understand, they can be replaced relatively easy and fast in runtime.

Also, i18n_manager should extract blocktrans phrases to .po files as well as trans phrases.
As a good point for compatibility with python's .po files, it may replace {{ var }} with %(var)s

Owner

kaos commented Nov 28, 2013

I'm not too familiar with the i18n features of ErlyDTL.

From what I gather about i18n from our readme files, is that it ought to be supported.

Could you please add some examples as to what doesn't work as expected.

Member

seriyps commented Nov 28, 2013

Template

<p>
{%blocktrans%}Hello, {{User}}!{%endblocktrans%}
</p>

Should contribute smth like

msgid "Hello, %(user)s!"
msgstr ""

to .pot file.

Next, when I want to render this template, say, in Russian language, I want to do

Fun = fun(Key) -> gettext:key2str(Key, "ru") end,  % will return "Привет, %(user)s!"
erlydtl:compile("my_template.dtl", my_template, []),
my_template:render([{name, <<"Ivan">>], {translation_fun, Fun}]).

and output should be

<p>
Привет, Ivan!
</p>
Member

seriyps commented Nov 28, 2013

Ah, and for now, as I remember, the only way to translate 'blocktrans' content is compile-time translation like

erlydtl:compile("my_template.dtl", my_template,
                [{blocktrans_fun, BlockFun}, {blocktrans_locales, ["en", "ru"]}]),

which is not flexible enough.

Owner

kaos commented Nov 28, 2013

Excellent!

Just what I was asking for, thank you!

Will look into it "soonish".

Ah, right. Yes, there's some blocktrans support.

Member

seriyps commented Nov 28, 2013

Small note: in erlydtl, AFAIR, {%blocktrans%} may contain arbitrary tags like {%if%} {%for%} and others (see https://github.com/erlydtl/erlydtl/blob/master/src/erlydtl_parser.yrl#L358).
But Django supports only substitution {{var}} inside blocktrans (see https://docs.djangoproject.com/en/1.6/topics/i18n/translation/#blocktrans-template-tag) and this is a good idea: the only thing it should do in runtime is substitute %(name)s with name variable value, which is not so complex/expensive.

Member

seriyps commented Nov 28, 2013

Also, looks like nested blocktrans blocks

{%blocktrans%}
  Hello
  {%blocktrans%}
   crazy
  {%endblocktrans%}
  world
{%endblocktrans%}

are syntactically correct now: https://github.com/erlydtl/erlydtl/blob/98f89cb0cd3fd02a660caad317338721cb1ae114/src/erlydtl_parser.yrl#L216

1> erlydtl:compile(<<"{%blocktrans%}Hello{%blocktrans%}crazy{%endblocktrans%}world{%endblocktrans%}">>, my_tpl, []).
{ok,my_tpl}
2> my_tpl:render([]).
{ok,[[<<"Hello">>,[<<"crazy">>],<<"world">>]]}

Which is not allowed in Django

>>> from django import template
>>> template.Template("{% load i18n %}{%blocktrans%}Hello{%blocktrans%}crazy{%endblocktrans%}world{%endblocktrans%}")
TemplateSyntaxError: 'blocktrans' doesn't allow other block tags (seen u'blocktrans') inside it
Owner

kaos commented Nov 28, 2013

Thank you for those extra examples.

Although the issue is around these two lines: https://github.com/erlydtl/erlydtl/blob/98f89cb0cd3fd02a660caad317338721cb1ae114/src/erlydtl_parser.yrl#L358

But indeed. I think a revise of the blocktrans implementation is in order, it's not on the top of my list of things to do at the moment however..

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 12, 2014

@seriyps seriyps Restrict `blocktrans` content to only strings and plain variables. #88
This matches Django behaviour.
714127e

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 14, 2014

@seriyps seriyps README updated. Some unit-tests added #88. 5769cc5

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 14, 2014

@seriyps seriyps Extract phrases from blocktrans. #88 94a75ef

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 15, 2014

@seriyps seriyps Restrict `blocktrans` content to only strings and plain variables. #88
This matches Django behaviour.
6539729

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 15, 2014

@seriyps seriyps README updated. Some unit-tests added #88. 39678f9

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 15, 2014

@seriyps seriyps Extract phrases from blocktrans. #88 40a7ad2

@seriyps seriyps added a commit to seriyps/erlydtl that referenced this issue Feb 15, 2014

@seriyps seriyps Unittests to restrict complex content in blocktrans. #88 54d673a

@kaos kaos modified the milestone: 0.9.0, 1.0 Feb 17, 2014

kaos added the fixed label Feb 17, 2014

kaos closed this in 766e37f Feb 17, 2014

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