You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by JumpStart <ge...@gmail.com> on 2021/11/02 03:55:32 UTC

Variable translate in TextField

Hi all,

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





Re: Variable translate in TextField

Posted by JumpStart <ge...@gmail.com>.
Thank you Thiago. I had no idea that I could use @InjectComponent into a Component, and I had no idea that use @InjectComponent of the same element into both a TextField and a Component without conflict, so thank you for the suggestion. It worked! Do you know if either is mentioned in the T5 doco?

@InjectComponent(‘price1”)
private TextField price1Field;

@InjectComponent(‘price1”)
private Component price1Component;

Cheers,

Geoff

> On 4 Nov 2021, at 4:32 am, Thiago H. de Paula Figueiredo <th...@gmail.com> wrote:
> 
> On Tue, Nov 2, 2021 at 12:55 AM JumpStart <
> geoff.callender.jumpstart@gmail.com <ma...@gmail.com>> 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


Re: Variable translate in TextField

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, Nov 2, 2021 at 12:55 AM JumpStart <
geoff.callender.jumpstart@gmail.com> 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