Rythm use transformers and nested tags with @i18n tag

161 Views Asked by At

First of all I'm very happy with Rythm! Excellent work for something that is free.

Recently I have begun internationalization of my templates with Rythm and some things seem more cumbersome than needed. I'm wondering if there is any better way of doing what I'm trying to do.

1. Chain tag onto @i18n()

This does not work:

  1. @i18n("about.text").nl2br()
  2. @i18n("about.text").mytransformer()

The workaround for this is:

@assign(newvar){@i18n("about.text")}
@newvar.nl2br()

This works but is is not pretty.

2. @i18n() escaped in javascript

If I have a section

<script>
  var s = '@description';
</script>

then Rythm will nicely escape any ' or " in that description. However when I use:

<script>
  var s = '@i18n("description")';
</script>

escaping is not done. I also tried doing:

var s = '@i18n("description").escape("js")';

and

var s = '@escapeJS(){@i18n("description")}';

but both do not work (see above). What does work again is using

@assign(desc){@i18n("description")}
...
var s = '@desc';

3. Use of tag inside @i18n() as argument

Sometimes I need a link inside a translated string like so:

about.text=See my profile here <a href="{0}">{1}</a>

I would like to use this in the template as follows:

@i18n("about.txt",@genlink("person",person.getId()),person)

Note: person here is an template argument of type Person. @genlink is a convenience template(or tag) to generate a link using a lookup. The solution I currently use is:

@assign(lnk){<a href='@genlink("person",person.getId())'>@person</a>}
@i18n("about.txt",lnk)

Note that the language resource has changed to: about.text=See my profile here {0} This is probably the better way to write the resource string anyway, but it would be nice if I could get rid of the @assign() somehow and write this:

@i18n("about.text","<a href='@genlink("person",person.getId())'>@person</a>")

Edit:

I tried your suggestions and was only partially successful.

  1. Chain tag onto @i18n()

doing @("about.text".i18n()) works whereas doing @("about.text".i18n().nl2br()) doesn't work and complains about a missing parameter for @i18n(). If I add the missing parameter like so: @("about.txt".i18n("").nl2br()) it complains that nl2br() is not defined for String

What did work for me was: @s().i18n("about.txt").nl2br()

Even weirder so, when I run your fiddle on Chrome it works perfectly. When I run it on Mac/Safari I get the same error as I just described: see screenshot: Rythm Safari screenshot

  1. @i18n() escaped in javascript

Works as you explained!

  1. Use of tag inside @i18n() as argument

understood. The current solution with @assign() is fine for one-offs. Using @def() is a nicer generic solution.

1

There are 1 best solutions below

0
Gelin Luo On
  1. Chain tag onto @i18n()

Try to use .i18n() transformer instead of @i18n() tag.

Say change @i18n("about.text").nl2br() to @("about.text".i18n().nl2br())

Note you need the () pair to enclose the entire expression if you feed into a string literal like "about.text", however if you do the same thing for a variable then that () can be opt out, e.g @foo.i18n().nl2br()

  1. @i18n() escaped in javascript

Again, use .i18n() transformer

  1. Use of tag inside @i18n() as argument

Tag processing is very hard to put into another tag or transformer. In your case I recommend you to use inline tag

The demonstration of all above three points could be found at http://fiddle.rythmengine.org/#/editor/0c426d5332334db3870b6bd8c0806e66