You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Alex Kotchnev <ak...@gmail.com> on 2012/08/23 16:11:22 UTC

Named translators

I would like to contribute a Translator for Long values that would allow me
to translate between byte counts and human readable file sizes (e.g.
5000bytes ~ 5MB). Since around 5.2 , the Translator configuration switched
to a Mapped configuration where each translator is contributed for a given
type. In my case, I just want to be able to use the translator I created in
text fields' "translate" property, so that I don't have to create the
translator on each page I need it.

E.g. I want to be able to use:

<t:textField value="..." translate="fileSize" />

When I attempt to contribute my FileSizeTranslator using the approach
below, I get a warning that I'm overriding the numeric translator from a
tapestry core module, which is not something I want to do.

    @SuppressWarnings("rawtypes")
    public static void
contributeTranslatorSource(MappedConfiguration<Class, Translator>
configuration) {
        configuration.add(Long.class, new FileSizeTranslator());
    }

Can anyone point me to how I can accomplish this ?

Cheers,

Alex K

Re: Named translators

Posted by Lance Java <la...@googlemail.com>.
Sorry, I was wrong... you can do it!!

AppModule.java
---------------
public void contributeTranslatorAlternatesSource(MappedConfiguration<String,
Translator> config) {
   configuration.add("fileSize", new FileSizeTranslator()); 
}



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Named-translators-tp5715756p5715783.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Named translators

Posted by Lance Java <la...@googlemail.com>.
I realise that it's a hack but unfortunately tapestry is using
MappedConfiguration<Class, Translator> so that there can only be one
translator mapped per class.

Perhaps this is another argument to consider adding a
MultiMappedConfiguration
http://tapestry.1045711.n5.nabble.com/MultiMapConfiguration-td5714003.html



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Named-translators-tp5715756p5715781.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Named translators

Posted by Alex Kotchnev <ak...@gmail.com>.
Lance,
   this approach definitely works; however, I was looking for a way to
declare this field translator somewhere globally as I use it in a few
different places.

Cheers,

Alex K

On Thu, Aug 23, 2012 at 11:20 AM, Lance Java <la...@googlemail.com>wrote:

> This might work:
>
> TML:
> <t:textField t:id="myField" value="..." translate="prop:myTranslator" />
>
> JAVA:
> @InjectComponent
> private Field myField;
>
> @Environmental
> private FormSupport formSupport;
>
> @Inject
> private Message messages;
>
> public FieldTranslator getMyTranslator() {
>    Translator translator = new FileSizeTranslator(...);
>    MessageFormatter formatter = messages.getFormatter(translator.getName()
> +
> "-message");
>    return new FieldTranslatorImpl(myField, translator, formatter,
> formSupport);
> }
>
>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Named-translators-tp5715756p5715764.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Named translators

Posted by Lance Java <la...@googlemail.com>.
This might work:

TML:
<t:textField t:id="myField" value="..." translate="prop:myTranslator" /> 

JAVA:
@InjectComponent
private Field myField;

@Environmental
private FormSupport formSupport;

@Inject
private Message messages;

public FieldTranslator getMyTranslator() {
   Translator translator = new FileSizeTranslator(...);
   MessageFormatter formatter = messages.getFormatter(translator.getName() +
"-message");
   return new FieldTranslatorImpl(myField, translator, formatter,
formSupport);
}



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Named-translators-tp5715756p5715764.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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