On Tue, Nov 2, 2021 at 12:55 AM JumpStart <
[email protected]> wrote:
> Hi all,
>
Hello!
I'd try using one of the methods of FieldTranslatorSource, starting
with createTranslator(ComponentResources componentResources, String
translatorName);. It's exactly the one used by the "translate" binding. The
ComponentResources should be the one of the field (i.e. you'll need
to @InjectComponent private Component price1; and pass
price1.getComponentResources() to FieldTranslatorSource.createTranslator().
>
> Does anyone have an example of a variable translate in a TextField? I want
> to switch between translators for different currencies because they have
> different numbers of decimal places.
>
> <t:textfield t:id="price1" value="paymentAgreement.price1"
> t:mixins="touchspin" touchspin.max="999.99"
> touchspin.forceStepDivisibility="'none'"
> touchspin.decimals="prop:currencyDecimalPlaces"
> translate="prop:moneyAmountTranslator”/>
>
> Unfortunately I get error:
> org.apache.tapestry5.ioc.util.UnknownValueException: Could not find a
> coercion from type com.goxpro.xpro.web.translators.MoneyAmountTranslator to
> type org.apache.tapestry5.FieldTranslator.
>
> The background is that class MoneyAmountTranslator implements
> Translator<BigDecimal>. It’s contributed to TranslatorAlternatesSource in
> AppModule:
>
> @SuppressWarnings("rawtypes")
> public static void
> contributeTranslatorAlternatesSource(MappedConfiguration<String,
> Translator> configuration,
> ThreadLocale threadLocale) {
> ...
> configuration.add("moneyamount_0", new
> MoneyAmountTranslator("moneyamount_0", 0, threadLocale, false));
> configuration.add("moneyamount_2", new
> MoneyAmountTranslator("moneyamount_2", 2, threadLocale, false));
> configuration.add("moneyamount_3", new
> MoneyAmountTranslator("moneyamount_3", 3, threadLocale, false));
> }
>
> In the page/component’s Java, I choose the translator...
>
> @Property
> private Translator moneyAmountTranslator;
> ...
> Map<String, Translator> translatorAlternates =
> translatorAlternatesSource.getTranslatorAlternates();
>
> switch (currencyDecimalPlaces) {
> case 0:
> moneyAmountTranslator = (MoneyAmountTranslator)
> translatorAlternates.get("moneyamount_0");
> break;
> case 2:
> moneyAmountTranslator = (MoneyAmountTranslator)
> translatorAlternates.get("moneyamount_2");
> break;
> case 3:
> moneyAmountTranslator = (MoneyAmountTranslator)
> translatorAlternates.get("moneyamount_3");
> break;
> default:
> throw new
> IllegalStateException(paymentAgreement.toString());
> }
>
> But I am having difficulty wrapping it in a FieldTranslator.
>
> Strangely, this works…
>
> <t:textfield t:id="price1" value="paymentAgreement.price1"
> t:mixins="touchspin" touchspin.max="999.99"
> touchspin.forceStepDivisibility="'none'"
> touchspin.decimals="prop:currencyDecimalPlaces"
> translate=“moneyamount_0”/>
>
> But this doesn't…
>
> @Property
> private String moneyAmountTranslatorStr;
> …
> moneyAmountTranslatorStr = “moneyamount_0”;
>
> <t:textfield t:id="price1" value="paymentAgreement.price1"
> t:mixins="touchspin" touchspin.max="999.99"
> touchspin.forceStepDivisibility="'none'"
> touchspin.decimals="prop:currencyDecimalPlaces"
> translate="prop:moneyAmountTranslatorStr”/>
>
> Cheers,
>
> Geoff
>
>
>
>
>
--
Thiago