You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Adonique Pineda <ad...@tooltwist.com.INVALID> on 2022/08/09 03:19:24 UTC

Variable translator for money

I would like to provide a variable translator to TextField depending on the
currency of the user. For example:

       <t:textfield value=*"stockItem.priceAsBigDecimal"* translate=
*"prop:moneyAmountTranslator"* />

In AppModule I have two money translators defined:

    configuration.add("moneyamount_0", *new* MoneyAmountTranslator(
"moneyamount_0", 0, threadLocale, *false*));

    configuration.add("moneyamount_2", *new* MoneyAmountTranslator(
"moneyamount_2", 2, threadLocale, *false*));

However, I can't figure out how to make one available to the translate
parameter of the TextField as a variable. How can
getMoneyAmountTranslator() retrieve one of the above translators? Can they
be injected?

Cheers,
Adonique

Re: Variable translator for money

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Hello!

I'd try @Inject'ing FieldTranslatorSource and use its FieldTranslator
createTranslator(ComponentResources componentResources, String
translatorName) method. The type of the validate parameter of
TextField and any other AbstractTextField subclass isn't Translator,
but FieldTranslator. FieldTranslatorSource.createTranslator() is
exactly what's being used indirectly when you have a
translate="something" in your template (look at
TranslateBindingFactory if you're curious :) ).

On Tue, Aug 9, 2022 at 10:28 AM Adonique Pineda
<ad...@tooltwist.com.invalid> wrote:
>
> I would like to provide a variable translator to TextField depending on the
> currency of the user. For example:
>
>        <t:textfield value=*"stockItem.priceAsBigDecimal"* translate=
> *"prop:moneyAmountTranslator"* />
>
> In AppModule I have two money translators defined:
>
>     configuration.add("moneyamount_0", *new* MoneyAmountTranslator(
> "moneyamount_0", 0, threadLocale, *false*));
>
>     configuration.add("moneyamount_2", *new* MoneyAmountTranslator(
> "moneyamount_2", 2, threadLocale, *false*));
>
> However, I can't figure out how to make one available to the translate
> parameter of the TextField as a variable. How can
> getMoneyAmountTranslator() retrieve one of the above translators? Can they
> be injected?
>
> Cheers,
> Adonique



-- 
Thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org