T5 : Expanding Messages Catalog for localization from arbitrary source

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

T5 : Expanding Messages Catalog for localization from arbitrary source

Michael Capper
Summary: How do I include additional localization-key/values-pairs in my global messages-catalog, originating from some app_<lang>.properties or from a database?

Hi,
I'd like to extend the Hash-Table containing the message-keys and message-values used in my app. The app_en.properties provides the basic global localization data, but I need even more data, which is spead about in other .properties-files or even in a Database. What I'd like to do would be to read in the data somewhere, and pass it on to the Messages(or ResourceBundle) somehow, so when I @Inject the Messages into a page/component or use the 'message:' binding, the extra localization is available at a root level.

Thanks for any help or pointers,
Mike
Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Hannes Heijkenskjöld
Hi

I have recently wondered about the same, and have now built something
that after much trial and error now works.

Basically, I have created a Messages implementation that wraps both a
tapestry Messages object and our own database messages. If a message is
not found in our database, the default Messages object is queried.

To get Tapestry to use my Messages implementation i had to decorate the
ComponentMessagesSource (and ValidationMessagesSource), since it is not
possible to replace the one in Tapestry. I think I read about decorating
services here:
http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and on
the mailing list.

My messages source service doesn't implement the ComponentMessagesSource
interface. I use an interception object for that. Hopefully you can
understand how it works from my example code below:

public class CommonsMessages extends AbstractMessages {

     private Messages fallbackMessages;

     protected String valueForKey(final key) {
        ... logic to get key from db, use fallbackMessages as fallback...
     }

}

-----

public class CommonsMessagesSource {

     public Messages getMessages(Locale locale, Messages fallbackMessages) {
        ... code that creates a CommonsMessages object, with
fallbackMessages ...
     }
}

----

// This is the decorating class
public class CommonsComponentMessagesSourceInterceptor implements
     ComponentMessagesSource {

     private final CommonsMessagesSource service;
     private final ComponentMessagesSource delegate;

     public CommonsComponentMessagesSourceInterceptor
         (CommonsMessagesSource service, ComponentMessagesSource delegate)
     {
         this.service = service;
         this.delegate = delegate;
     }

     public Messages getMessages(ComponentModel componentModel, Locale
locale) {

         return service.getMessages(locale,
             delegate.getMessages(componentModel, locale));
     }

     // Not sure about how I should handle this one
     public void addInvalidationListener(
         InvalidationListener invalidationlistener) {
         delegate.addInvalidationListener(invalidationlistener);
     }
}

---

In AppModule.java:

     // Method for instantiating and configuring the custom messages source
     public static CommonsMessagesSource buildCommonsMessagesSource()
     {
         CommonsMessagesSource messagesSource = new CommonsMessagesSource();
         return messagesSource;
     }

     // Method that decorates the default MessagesSource using the
interceptor class
     @Match("ComponentMessagesSource")
     public static ComponentMessagesSource decorateComponentMessagesSource
          (Object delegate, CommonsMessagesSource service)
      {
          return new CommonsComponentMessagesSourceInterceptor(service,
(ComponentMessagesSource)delegate);
      }

This is how I got it to work, there may be other ways ofcourse. There
might even be better ways :-)

Cheers,
/Hannes

Michael Capper skrev:

> Summary: How do I include additional localization-key/values-pairs in my
> global messages-catalog, originating from some app_<lang>.properties or from
> a database?
>
> Hi,
> I'd like to extend the Hash-Table containing the message-keys and
> message-values used in my app. The app_en.properties provides the basic
> global localization data, but I need even more data, which is spead about in
> other .properties-files or even in a Database. What I'd like to do would be
> to read in the data somewhere, and pass it on to the Messages(or
> ResourceBundle) somehow, so when I @Inject the Messages into a
> page/component or use the 'message:' binding, the extra localization is
> available at a root level.
>
> Thanks for any help or pointers,
> Mike


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Michael Capper
Thanks Hannes!

I tried the route with the Decorator once, and ended up having to write some java-code into the interceptor via the BodyBuilder .... 'twas not nice.

Your example worked great, i only had to pass some more Services (AssetSource to get the URLs, my ProjectContextProvider to get the ResourceBundles for my text) into the build-Method in AppModule, then i could return the value for a Message-Key from the bundles, or if not existant, from the fallbackMessages.

Cheers,
Mike

Hannes Heijkenskjöld wrote
Hi

I have recently wondered about the same, and have now built something
that after much trial and error now works.

Basically, I have created a Messages implementation that wraps both a
tapestry Messages object and our own database messages. If a message is
not found in our database, the default Messages object is queried.

To get Tapestry to use my Messages implementation i had to decorate the
ComponentMessagesSource (and ValidationMessagesSource), since it is not
possible to replace the one in Tapestry. I think I read about decorating
services here:
http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and on
the mailing list.

My messages source service doesn't implement the ComponentMessagesSource
interface. I use an interception object for that. Hopefully you can
understand how it works from my example code below:

public class CommonsMessages extends AbstractMessages {

     private Messages fallbackMessages;

     protected String valueForKey(final key) {
        ... logic to get key from db, use fallbackMessages as fallback...
     }

}

-----

public class CommonsMessagesSource {

     public Messages getMessages(Locale locale, Messages fallbackMessages) {
        ... code that creates a CommonsMessages object, with
fallbackMessages ...
     }
}

----

// This is the decorating class
public class CommonsComponentMessagesSourceInterceptor implements
     ComponentMessagesSource {

     private final CommonsMessagesSource service;
     private final ComponentMessagesSource delegate;

     public CommonsComponentMessagesSourceInterceptor
         (CommonsMessagesSource service, ComponentMessagesSource delegate)
     {
         this.service = service;
         this.delegate = delegate;
     }

     public Messages getMessages(ComponentModel componentModel, Locale
locale) {

         return service.getMessages(locale,
             delegate.getMessages(componentModel, locale));
     }

     // Not sure about how I should handle this one
     public void addInvalidationListener(
         InvalidationListener invalidationlistener) {
         delegate.addInvalidationListener(invalidationlistener);
     }
}

---

In AppModule.java:

     // Method for instantiating and configuring the custom messages source
     public static CommonsMessagesSource buildCommonsMessagesSource()
     {
         CommonsMessagesSource messagesSource = new CommonsMessagesSource();
         return messagesSource;
     }

     // Method that decorates the default MessagesSource using the
interceptor class
     @Match("ComponentMessagesSource")
     public static ComponentMessagesSource decorateComponentMessagesSource
          (Object delegate, CommonsMessagesSource service)
      {
          return new CommonsComponentMessagesSourceInterceptor(service,
(ComponentMessagesSource)delegate);
      }

This is how I got it to work, there may be other ways ofcourse. There
might even be better ways :-)

Cheers,
/Hannes

Michael Capper skrev:
> Summary: How do I include additional localization-key/values-pairs in my
> global messages-catalog, originating from some app_<lang>.properties or from
> a database?
>
> Hi,
> I'd like to extend the Hash-Table containing the message-keys and
> message-values used in my app. The app_en.properties provides the basic
> global localization data, but I need even more data, which is spead about in
> other .properties-files or even in a Database. What I'd like to do would be
> to read in the data somewhere, and pass it on to the Messages(or
> ResourceBundle) somehow, so when I @Inject the Messages into a
> page/component or use the 'message:' binding, the extra localization is
> available at a root level.
>
> Thanks for any help or pointers,
> Mike


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org
Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Hannes Heijkenskjöld
Great!

Glad I could help you. :-)

/Hannes

Michael Capper skrev:

> Thanks Hannes!
>
> I tried the route with the Decorator once, and ended up having to write some
> java-code into the interceptor via the BodyBuilder .... 'twas not nice.
>
> Your example worked great, i only had to pass some more Services
> (AssetSource to get the URLs, my ProjectContextProvider to get the
> ResourceBundles for my text) into the build-Method in AppModule, then i
> could return the value for a Message-Key from the bundles, or if not
> existant, from the fallbackMessages.
>
> Cheers,
> Mike
>
>
> Hannes Heijkenskjöld wrote:
>> Hi
>>
>> I have recently wondered about the same, and have now built something
>> that after much trial and error now works.
>>
>> Basically, I have created a Messages implementation that wraps both a
>> tapestry Messages object and our own database messages. If a message is
>> not found in our database, the default Messages object is queried.
>>
>> To get Tapestry to use my Messages implementation i had to decorate the
>> ComponentMessagesSource (and ValidationMessagesSource), since it is not
>> possible to replace the one in Tapestry. I think I read about decorating
>> services here:
>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and on
>> the mailing list.
>>
>> My messages source service doesn't implement the ComponentMessagesSource
>> interface. I use an interception object for that. Hopefully you can
>> understand how it works from my example code below:
>>
>> public class CommonsMessages extends AbstractMessages {
>>
>>      private Messages fallbackMessages;
>>
>>      protected String valueForKey(final key) {
>> ... logic to get key from db, use fallbackMessages as fallback...
>>      }
>>
>> }
>>
>> -----
>>
>> public class CommonsMessagesSource {
>>
>>      public Messages getMessages(Locale locale, Messages fallbackMessages)
>> {
>>         ... code that creates a CommonsMessages object, with
>> fallbackMessages ...
>>      }
>> }
>>
>> ----
>>
>> // This is the decorating class
>> public class CommonsComponentMessagesSourceInterceptor implements
>>      ComponentMessagesSource {
>>
>>      private final CommonsMessagesSource service;
>>      private final ComponentMessagesSource delegate;
>>
>>      public CommonsComponentMessagesSourceInterceptor
>>          (CommonsMessagesSource service, ComponentMessagesSource delegate)
>>      {
>>          this.service = service;
>>          this.delegate = delegate;
>>      }
>>
>>      public Messages getMessages(ComponentModel componentModel, Locale
>> locale) {
>>
>>          return service.getMessages(locale,
>>              delegate.getMessages(componentModel, locale));
>>      }
>>
>>      // Not sure about how I should handle this one
>>      public void addInvalidationListener(
>>          InvalidationListener invalidationlistener) {
>>          delegate.addInvalidationListener(invalidationlistener);
>>      }
>> }
>>
>> ---
>>
>> In AppModule.java:
>>
>>      // Method for instantiating and configuring the custom messages
>> source
>>      public static CommonsMessagesSource buildCommonsMessagesSource()
>>      {
>>          CommonsMessagesSource messagesSource = new
>> CommonsMessagesSource();
>>          return messagesSource;
>>      }
>>
>>      // Method that decorates the default MessagesSource using the
>> interceptor class
>>      @Match("ComponentMessagesSource")
>>      public static ComponentMessagesSource decorateComponentMessagesSource
>>           (Object delegate, CommonsMessagesSource service)
>>       {
>>           return new CommonsComponentMessagesSourceInterceptor(service,
>> (ComponentMessagesSource)delegate);
>>       }
>>
>> This is how I got it to work, there may be other ways ofcourse. There
>> might even be better ways :-)
>>
>> Cheers,
>> /Hannes
>>
>> Michael Capper skrev:
>>> Summary: How do I include additional localization-key/values-pairs in my
>>> global messages-catalog, originating from some app_<lang>.properties or
>>> from
>>> a database?
>>>
>>> Hi,
>>> I'd like to extend the Hash-Table containing the message-keys and
>>> message-values used in my app. The app_en.properties provides the basic
>>> global localization data, but I need even more data, which is spead about
>>> in
>>> other .properties-files or even in a Database. What I'd like to do would
>>> be
>>> to read in the data somewhere, and pass it on to the Messages(or
>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>> page/component or use the 'message:' binding, the extra localization is
>>> available at a root level.
>>>
>>> Thanks for any help or pointers,
>>> Mike
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Inge Solvoll-2
Hi, Hannes!

I tried your implementation, and it worked right away, I just returned the
value "TESTING TESTING" from my DbMessages class that extends Abstract
Messages.

But, what I need is for the valueForKey(String key) method to be aware of
the languageId/customerId of the currently logged in user. In all my other
pages, I get this through injecting the request and getting my user object
from HttpSession. Alternatively this can be moved to an ASO, but I haven't
taken the time to do that yet.

I've tried injecting HttpServletRequest and ApplicationState on all the
classes listed in the implementation from Hannes, but none of them work
(only null values). How can I make information from the current user session
available to my AbstractMessages implementation?

Regards

Inge

On Wed, Apr 16, 2008 at 10:22 AM, Hannes Heijkenskjöld <
[hidden email]> wrote:

> Great!
>
> Glad I could help you. :-)
>
> /Hannes
>
> Michael Capper skrev:
>
>  Thanks Hannes!
>>
>> I tried the route with the Decorator once, and ended up having to write
>> some
>> java-code into the interceptor via the BodyBuilder .... 'twas not nice.
>>
>> Your example worked great, i only had to pass some more Services
>> (AssetSource to get the URLs, my ProjectContextProvider to get the
>> ResourceBundles for my text) into the build-Method in AppModule, then i
>> could return the value for a Message-Key from the bundles, or if not
>> existant, from the fallbackMessages.
>>
>> Cheers,
>> Mike
>>
>>
>> Hannes Heijkenskjöld wrote:
>>
>>> Hi
>>>
>>> I have recently wondered about the same, and have now built something
>>> that after much trial and error now works.
>>>
>>> Basically, I have created a Messages implementation that wraps both a
>>> tapestry Messages object and our own database messages. If a message is not
>>> found in our database, the default Messages object is queried.
>>>
>>> To get Tapestry to use my Messages implementation i had to decorate the
>>> ComponentMessagesSource (and ValidationMessagesSource), since it is not
>>> possible to replace the one in Tapestry. I think I read about decorating
>>> services here:
>>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and on
>>> the mailing list.
>>>
>>> My messages source service doesn't implement the ComponentMessagesSource
>>> interface. I use an interception object for that. Hopefully you can
>>> understand how it works from my example code below:
>>>
>>> public class CommonsMessages extends AbstractMessages {
>>>
>>>     private Messages fallbackMessages;
>>>
>>>     protected String valueForKey(final key) {
>>>        ... logic to get key from db, use fallbackMessages as fallback...
>>>     }
>>>
>>> }
>>>
>>> -----
>>>
>>> public class CommonsMessagesSource {
>>>
>>>     public Messages getMessages(Locale locale, Messages fallbackMessages)
>>> {
>>>        ... code that creates a CommonsMessages object, with
>>> fallbackMessages ...
>>>     }
>>> }
>>>
>>> ----
>>>
>>> // This is the decorating class
>>> public class CommonsComponentMessagesSourceInterceptor implements
>>>     ComponentMessagesSource {
>>>
>>>     private final CommonsMessagesSource service;
>>>     private final ComponentMessagesSource delegate;
>>>
>>>     public CommonsComponentMessagesSourceInterceptor
>>>         (CommonsMessagesSource service, ComponentMessagesSource delegate)
>>>     {
>>>         this.service = service;
>>>         this.delegate = delegate;
>>>     }
>>>
>>>     public Messages getMessages(ComponentModel componentModel, Locale
>>> locale) {
>>>
>>>         return service.getMessages(locale,
>>>             delegate.getMessages(componentModel, locale));
>>>     }
>>>
>>>     // Not sure about how I should handle this one
>>>     public void addInvalidationListener(
>>>         InvalidationListener invalidationlistener) {
>>>         delegate.addInvalidationListener(invalidationlistener);
>>>     }
>>> }
>>>
>>> ---
>>>
>>> In AppModule.java:
>>>
>>>     // Method for instantiating and configuring the custom messages
>>> source
>>>     public static CommonsMessagesSource buildCommonsMessagesSource()
>>>     {
>>>         CommonsMessagesSource messagesSource = new
>>> CommonsMessagesSource();
>>>         return messagesSource;
>>>     }
>>>
>>>     // Method that decorates the default MessagesSource using the
>>> interceptor class
>>>     @Match("ComponentMessagesSource")
>>>     public static ComponentMessagesSource decorateComponentMessagesSource
>>>          (Object delegate, CommonsMessagesSource service)
>>>      {
>>>          return new CommonsComponentMessagesSourceInterceptor(service,
>>> (ComponentMessagesSource)delegate);
>>>      }
>>>
>>> This is how I got it to work, there may be other ways ofcourse. There
>>> might even be better ways :-)
>>>
>>> Cheers,
>>> /Hannes
>>>
>>> Michael Capper skrev:
>>>
>>>> Summary: How do I include additional localization-key/values-pairs in my
>>>> global messages-catalog, originating from some app_<lang>.properties or
>>>> from
>>>> a database?
>>>>
>>>> Hi,
>>>> I'd like to extend the Hash-Table containing the message-keys and
>>>> message-values used in my app. The app_en.properties provides the basic
>>>> global localization data, but I need even more data, which is spead
>>>> about
>>>> in
>>>> other .properties-files or even in a Database. What I'd like to do would
>>>> be
>>>> to read in the data somewhere, and pass it on to the Messages(or
>>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>>> page/component or use the 'message:' binding, the extra localization is
>>>> available at a root level.
>>>>
>>>> Thanks for any help or pointers,
>>>> Mike
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Hannes Heijkenskjöld
Hi Inge,

glad to have been able to help you!

For your current problem, I can see two immediate solutions.

1. Store your user object as an ASO and inject ApplicationStateManager
into your Messages implementation. Get the user object instance by
calling applicationStateManager.get(UserObject.class).

2. Inject RequestGlobals in your Messages implementation. Then you can
get the request by calling requestGlobals.getHTTPServletRequest().

I hope this helps

/Hannes

Inge Solvoll skrev:

> Hi, Hannes!
>
> I tried your implementation, and it worked right away, I just returned the
> value "TESTING TESTING" from my DbMessages class that extends Abstract
> Messages.
>
> But, what I need is for the valueForKey(String key) method to be aware of
> the languageId/customerId of the currently logged in user. In all my other
> pages, I get this through injecting the request and getting my user object
> from HttpSession. Alternatively this can be moved to an ASO, but I haven't
> taken the time to do that yet.
>
> I've tried injecting HttpServletRequest and ApplicationState on all the
> classes listed in the implementation from Hannes, but none of them work
> (only null values). How can I make information from the current user session
> available to my AbstractMessages implementation?
>
> Regards
>
> Inge
>
> On Wed, Apr 16, 2008 at 10:22 AM, Hannes Heijkenskjöld <
> [hidden email]> wrote:
>
>> Great!
>>
>> Glad I could help you. :-)
>>
>> /Hannes
>>
>> Michael Capper skrev:
>>
>>  Thanks Hannes!
>>> I tried the route with the Decorator once, and ended up having to write
>>> some
>>> java-code into the interceptor via the BodyBuilder .... 'twas not nice.
>>>
>>> Your example worked great, i only had to pass some more Services
>>> (AssetSource to get the URLs, my ProjectContextProvider to get the
>>> ResourceBundles for my text) into the build-Method in AppModule, then i
>>> could return the value for a Message-Key from the bundles, or if not
>>> existant, from the fallbackMessages.
>>>
>>> Cheers,
>>> Mike
>>>
>>>
>>> Hannes Heijkenskjöld wrote:
>>>
>>>> Hi
>>>>
>>>> I have recently wondered about the same, and have now built something
>>>> that after much trial and error now works.
>>>>
>>>> Basically, I have created a Messages implementation that wraps both a
>>>> tapestry Messages object and our own database messages. If a message is not
>>>> found in our database, the default Messages object is queried.
>>>>
>>>> To get Tapestry to use my Messages implementation i had to decorate the
>>>> ComponentMessagesSource (and ValidationMessagesSource), since it is not
>>>> possible to replace the one in Tapestry. I think I read about decorating
>>>> services here:
>>>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and on
>>>> the mailing list.
>>>>
>>>> My messages source service doesn't implement the ComponentMessagesSource
>>>> interface. I use an interception object for that. Hopefully you can
>>>> understand how it works from my example code below:
>>>>
>>>> public class CommonsMessages extends AbstractMessages {
>>>>
>>>>     private Messages fallbackMessages;
>>>>
>>>>     protected String valueForKey(final key) {
>>>>        ... logic to get key from db, use fallbackMessages as fallback...
>>>>     }
>>>>
>>>> }
>>>>
>>>> -----
>>>>
>>>> public class CommonsMessagesSource {
>>>>
>>>>     public Messages getMessages(Locale locale, Messages fallbackMessages)
>>>> {
>>>>        ... code that creates a CommonsMessages object, with
>>>> fallbackMessages ...
>>>>     }
>>>> }
>>>>
>>>> ----
>>>>
>>>> // This is the decorating class
>>>> public class CommonsComponentMessagesSourceInterceptor implements
>>>>     ComponentMessagesSource {
>>>>
>>>>     private final CommonsMessagesSource service;
>>>>     private final ComponentMessagesSource delegate;
>>>>
>>>>     public CommonsComponentMessagesSourceInterceptor
>>>>         (CommonsMessagesSource service, ComponentMessagesSource delegate)
>>>>     {
>>>>         this.service = service;
>>>>         this.delegate = delegate;
>>>>     }
>>>>
>>>>     public Messages getMessages(ComponentModel componentModel, Locale
>>>> locale) {
>>>>
>>>>         return service.getMessages(locale,
>>>>             delegate.getMessages(componentModel, locale));
>>>>     }
>>>>
>>>>     // Not sure about how I should handle this one
>>>>     public void addInvalidationListener(
>>>>         InvalidationListener invalidationlistener) {
>>>>         delegate.addInvalidationListener(invalidationlistener);
>>>>     }
>>>> }
>>>>
>>>> ---
>>>>
>>>> In AppModule.java:
>>>>
>>>>     // Method for instantiating and configuring the custom messages
>>>> source
>>>>     public static CommonsMessagesSource buildCommonsMessagesSource()
>>>>     {
>>>>         CommonsMessagesSource messagesSource = new
>>>> CommonsMessagesSource();
>>>>         return messagesSource;
>>>>     }
>>>>
>>>>     // Method that decorates the default MessagesSource using the
>>>> interceptor class
>>>>     @Match("ComponentMessagesSource")
>>>>     public static ComponentMessagesSource decorateComponentMessagesSource
>>>>          (Object delegate, CommonsMessagesSource service)
>>>>      {
>>>>          return new CommonsComponentMessagesSourceInterceptor(service,
>>>> (ComponentMessagesSource)delegate);
>>>>      }
>>>>
>>>> This is how I got it to work, there may be other ways ofcourse. There
>>>> might even be better ways :-)
>>>>
>>>> Cheers,
>>>> /Hannes
>>>>
>>>> Michael Capper skrev:
>>>>
>>>>> Summary: How do I include additional localization-key/values-pairs in my
>>>>> global messages-catalog, originating from some app_<lang>.properties or
>>>>> from
>>>>> a database?
>>>>>
>>>>> Hi,
>>>>> I'd like to extend the Hash-Table containing the message-keys and
>>>>> message-values used in my app. The app_en.properties provides the basic
>>>>> global localization data, but I need even more data, which is spead
>>>>> about
>>>>> in
>>>>> other .properties-files or even in a Database. What I'd like to do would
>>>>> be
>>>>> to read in the data somewhere, and pass it on to the Messages(or
>>>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>>>> page/component or use the 'message:' binding, the extra localization is
>>>>> available at a root level.
>>>>>
>>>>> Thanks for any help or pointers,
>>>>> Mike
>>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>>
>>>>
>>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Inge Solvoll-2
Ok, thanks, I'll try that! I already tried the following in my class that
extends AbstractMessages:

@Inject
private HttpServletRequest request;

@ApplicationState
private User user;

Which didn't work, both were null. But what your saying is that these
objects are not possible to inject in this scope, but RequestGlobals is, so
the following will work?

@Inject
private RequestGlobals requestGlobals

Regards
Inge

On Mon, Aug 25, 2008 at 3:57 PM, Hannes Heijkenskjöld <
[hidden email]> wrote:

> Hi Inge,
>
> glad to have been able to help you!
>
> For your current problem, I can see two immediate solutions.
>
> 1. Store your user object as an ASO and inject ApplicationStateManager into
> your Messages implementation. Get the user object instance by calling
> applicationStateManager.get(UserObject.class).
>
> 2. Inject RequestGlobals in your Messages implementation. Then you can get
> the request by calling requestGlobals.getHTTPServletRequest().
>
> I hope this helps
>
> /Hannes
>
> Inge Solvoll skrev:
>
>  Hi, Hannes!
>>
>> I tried your implementation, and it worked right away, I just returned the
>> value "TESTING TESTING" from my DbMessages class that extends Abstract
>> Messages.
>>
>> But, what I need is for the valueForKey(String key) method to be aware of
>> the languageId/customerId of the currently logged in user. In all my other
>> pages, I get this through injecting the request and getting my user object
>> from HttpSession. Alternatively this can be moved to an ASO, but I haven't
>> taken the time to do that yet.
>>
>> I've tried injecting HttpServletRequest and ApplicationState on all the
>> classes listed in the implementation from Hannes, but none of them work
>> (only null values). How can I make information from the current user
>> session
>> available to my AbstractMessages implementation?
>>
>> Regards
>>
>> Inge
>>
>> On Wed, Apr 16, 2008 at 10:22 AM, Hannes Heijkenskjöld <
>> [hidden email]> wrote:
>>
>>  Great!
>>>
>>> Glad I could help you. :-)
>>>
>>> /Hannes
>>>
>>> Michael Capper skrev:
>>>
>>>  Thanks Hannes!
>>>
>>>> I tried the route with the Decorator once, and ended up having to write
>>>> some
>>>> java-code into the interceptor via the BodyBuilder .... 'twas not nice.
>>>>
>>>> Your example worked great, i only had to pass some more Services
>>>> (AssetSource to get the URLs, my ProjectContextProvider to get the
>>>> ResourceBundles for my text) into the build-Method in AppModule, then i
>>>> could return the value for a Message-Key from the bundles, or if not
>>>> existant, from the fallbackMessages.
>>>>
>>>> Cheers,
>>>> Mike
>>>>
>>>>
>>>> Hannes Heijkenskjöld wrote:
>>>>
>>>>  Hi
>>>>>
>>>>> I have recently wondered about the same, and have now built something
>>>>> that after much trial and error now works.
>>>>>
>>>>> Basically, I have created a Messages implementation that wraps both a
>>>>> tapestry Messages object and our own database messages. If a message is
>>>>> not
>>>>> found in our database, the default Messages object is queried.
>>>>>
>>>>> To get Tapestry to use my Messages implementation i had to decorate the
>>>>> ComponentMessagesSource (and ValidationMessagesSource), since it is not
>>>>> possible to replace the one in Tapestry. I think I read about
>>>>> decorating
>>>>> services here:
>>>>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and
>>>>> on
>>>>> the mailing list.
>>>>>
>>>>> My messages source service doesn't implement the
>>>>> ComponentMessagesSource
>>>>> interface. I use an interception object for that. Hopefully you can
>>>>> understand how it works from my example code below:
>>>>>
>>>>> public class CommonsMessages extends AbstractMessages {
>>>>>
>>>>>    private Messages fallbackMessages;
>>>>>
>>>>>    protected String valueForKey(final key) {
>>>>>       ... logic to get key from db, use fallbackMessages as fallback...
>>>>>    }
>>>>>
>>>>> }
>>>>>
>>>>> -----
>>>>>
>>>>> public class CommonsMessagesSource {
>>>>>
>>>>>    public Messages getMessages(Locale locale, Messages
>>>>> fallbackMessages)
>>>>> {
>>>>>       ... code that creates a CommonsMessages object, with
>>>>> fallbackMessages ...
>>>>>    }
>>>>> }
>>>>>
>>>>> ----
>>>>>
>>>>> // This is the decorating class
>>>>> public class CommonsComponentMessagesSourceInterceptor implements
>>>>>    ComponentMessagesSource {
>>>>>
>>>>>    private final CommonsMessagesSource service;
>>>>>    private final ComponentMessagesSource delegate;
>>>>>
>>>>>    public CommonsComponentMessagesSourceInterceptor
>>>>>        (CommonsMessagesSource service, ComponentMessagesSource
>>>>> delegate)
>>>>>    {
>>>>>        this.service = service;
>>>>>        this.delegate = delegate;
>>>>>    }
>>>>>
>>>>>    public Messages getMessages(ComponentModel componentModel, Locale
>>>>> locale) {
>>>>>
>>>>>        return service.getMessages(locale,
>>>>>            delegate.getMessages(componentModel, locale));
>>>>>    }
>>>>>
>>>>>    // Not sure about how I should handle this one
>>>>>    public void addInvalidationListener(
>>>>>        InvalidationListener invalidationlistener) {
>>>>>        delegate.addInvalidationListener(invalidationlistener);
>>>>>    }
>>>>> }
>>>>>
>>>>> ---
>>>>>
>>>>> In AppModule.java:
>>>>>
>>>>>    // Method for instantiating and configuring the custom messages
>>>>> source
>>>>>    public static CommonsMessagesSource buildCommonsMessagesSource()
>>>>>    {
>>>>>        CommonsMessagesSource messagesSource = new
>>>>> CommonsMessagesSource();
>>>>>        return messagesSource;
>>>>>    }
>>>>>
>>>>>    // Method that decorates the default MessagesSource using the
>>>>> interceptor class
>>>>>    @Match("ComponentMessagesSource")
>>>>>    public static ComponentMessagesSource
>>>>> decorateComponentMessagesSource
>>>>>         (Object delegate, CommonsMessagesSource service)
>>>>>     {
>>>>>         return new CommonsComponentMessagesSourceInterceptor(service,
>>>>> (ComponentMessagesSource)delegate);
>>>>>     }
>>>>>
>>>>> This is how I got it to work, there may be other ways ofcourse. There
>>>>> might even be better ways :-)
>>>>>
>>>>> Cheers,
>>>>> /Hannes
>>>>>
>>>>> Michael Capper skrev:
>>>>>
>>>>>  Summary: How do I include additional localization-key/values-pairs in
>>>>>> my
>>>>>> global messages-catalog, originating from some app_<lang>.properties
>>>>>> or
>>>>>> from
>>>>>> a database?
>>>>>>
>>>>>> Hi,
>>>>>> I'd like to extend the Hash-Table containing the message-keys and
>>>>>> message-values used in my app. The app_en.properties provides the
>>>>>> basic
>>>>>> global localization data, but I need even more data, which is spead
>>>>>> about
>>>>>> in
>>>>>> other .properties-files or even in a Database. What I'd like to do
>>>>>> would
>>>>>> be
>>>>>> to read in the data somewhere, and pass it on to the Messages(or
>>>>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>>>>> page/component or use the 'message:' binding, the extra localization
>>>>>> is
>>>>>> available at a root level.
>>>>>>
>>>>>> Thanks for any help or pointers,
>>>>>> Mike
>>>>>>
>>>>>>  ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [hidden email]
>>>>> For additional commands, e-mail: [hidden email]
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>  ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Inge Solvoll-2
Same problem with RequestGlobals. Comes up as null when I inject it.

Haven't tried the ApplicationStateManager yet, but in my head, the suggested
code sounds like the equivalent of the @Application state annotation, which
I already tried. Am I wrong?

On Mon, Aug 25, 2008 at 11:40 PM, Inge Solvoll <[hidden email]>wrote:

> Ok, thanks, I'll try that! I already tried the following in my class that
> extends AbstractMessages:
>
> @Inject
> private HttpServletRequest request;
>
> @ApplicationState
> private User user;
>
> Which didn't work, both were null. But what your saying is that these
> objects are not possible to inject in this scope, but RequestGlobals is, so
> the following will work?
>
> @Inject
> private RequestGlobals requestGlobals
>
> Regards
> Inge
>
>
> On Mon, Aug 25, 2008 at 3:57 PM, Hannes Heijkenskjöld <
> [hidden email]> wrote:
>
>> Hi Inge,
>>
>> glad to have been able to help you!
>>
>> For your current problem, I can see two immediate solutions.
>>
>> 1. Store your user object as an ASO and inject ApplicationStateManager
>> into your Messages implementation. Get the user object instance by calling
>> applicationStateManager.get(UserObject.class).
>>
>> 2. Inject RequestGlobals in your Messages implementation. Then you can get
>> the request by calling requestGlobals.getHTTPServletRequest().
>>
>> I hope this helps
>>
>> /Hannes
>>
>> Inge Solvoll skrev:
>>
>>  Hi, Hannes!
>>>
>>> I tried your implementation, and it worked right away, I just returned
>>> the
>>> value "TESTING TESTING" from my DbMessages class that extends Abstract
>>> Messages.
>>>
>>> But, what I need is for the valueForKey(String key) method to be aware of
>>> the languageId/customerId of the currently logged in user. In all my
>>> other
>>> pages, I get this through injecting the request and getting my user
>>> object
>>> from HttpSession. Alternatively this can be moved to an ASO, but I
>>> haven't
>>> taken the time to do that yet.
>>>
>>> I've tried injecting HttpServletRequest and ApplicationState on all the
>>> classes listed in the implementation from Hannes, but none of them work
>>> (only null values). How can I make information from the current user
>>> session
>>> available to my AbstractMessages implementation?
>>>
>>> Regards
>>>
>>> Inge
>>>
>>> On Wed, Apr 16, 2008 at 10:22 AM, Hannes Heijkenskjöld <
>>> [hidden email]> wrote:
>>>
>>>  Great!
>>>>
>>>> Glad I could help you. :-)
>>>>
>>>> /Hannes
>>>>
>>>> Michael Capper skrev:
>>>>
>>>>  Thanks Hannes!
>>>>
>>>>> I tried the route with the Decorator once, and ended up having to write
>>>>> some
>>>>> java-code into the interceptor via the BodyBuilder .... 'twas not nice.
>>>>>
>>>>> Your example worked great, i only had to pass some more Services
>>>>> (AssetSource to get the URLs, my ProjectContextProvider to get the
>>>>> ResourceBundles for my text) into the build-Method in AppModule, then i
>>>>> could return the value for a Message-Key from the bundles, or if not
>>>>> existant, from the fallbackMessages.
>>>>>
>>>>> Cheers,
>>>>> Mike
>>>>>
>>>>>
>>>>> Hannes Heijkenskjöld wrote:
>>>>>
>>>>>  Hi
>>>>>>
>>>>>> I have recently wondered about the same, and have now built something
>>>>>> that after much trial and error now works.
>>>>>>
>>>>>> Basically, I have created a Messages implementation that wraps both a
>>>>>> tapestry Messages object and our own database messages. If a message
>>>>>> is not
>>>>>> found in our database, the default Messages object is queried.
>>>>>>
>>>>>> To get Tapestry to use my Messages implementation i had to decorate
>>>>>> the
>>>>>> ComponentMessagesSource (and ValidationMessagesSource), since it is
>>>>>> not
>>>>>> possible to replace the one in Tapestry. I think I read about
>>>>>> decorating
>>>>>> services here:
>>>>>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and
>>>>>> on
>>>>>> the mailing list.
>>>>>>
>>>>>> My messages source service doesn't implement the
>>>>>> ComponentMessagesSource
>>>>>> interface. I use an interception object for that. Hopefully you can
>>>>>> understand how it works from my example code below:
>>>>>>
>>>>>> public class CommonsMessages extends AbstractMessages {
>>>>>>
>>>>>>    private Messages fallbackMessages;
>>>>>>
>>>>>>    protected String valueForKey(final key) {
>>>>>>       ... logic to get key from db, use fallbackMessages as
>>>>>> fallback...
>>>>>>    }
>>>>>>
>>>>>> }
>>>>>>
>>>>>> -----
>>>>>>
>>>>>> public class CommonsMessagesSource {
>>>>>>
>>>>>>    public Messages getMessages(Locale locale, Messages
>>>>>> fallbackMessages)
>>>>>> {
>>>>>>       ... code that creates a CommonsMessages object, with
>>>>>> fallbackMessages ...
>>>>>>    }
>>>>>> }
>>>>>>
>>>>>> ----
>>>>>>
>>>>>> // This is the decorating class
>>>>>> public class CommonsComponentMessagesSourceInterceptor implements
>>>>>>    ComponentMessagesSource {
>>>>>>
>>>>>>    private final CommonsMessagesSource service;
>>>>>>    private final ComponentMessagesSource delegate;
>>>>>>
>>>>>>    public CommonsComponentMessagesSourceInterceptor
>>>>>>        (CommonsMessagesSource service, ComponentMessagesSource
>>>>>> delegate)
>>>>>>    {
>>>>>>        this.service = service;
>>>>>>        this.delegate = delegate;
>>>>>>    }
>>>>>>
>>>>>>    public Messages getMessages(ComponentModel componentModel, Locale
>>>>>> locale) {
>>>>>>
>>>>>>        return service.getMessages(locale,
>>>>>>            delegate.getMessages(componentModel, locale));
>>>>>>    }
>>>>>>
>>>>>>    // Not sure about how I should handle this one
>>>>>>    public void addInvalidationListener(
>>>>>>        InvalidationListener invalidationlistener) {
>>>>>>        delegate.addInvalidationListener(invalidationlistener);
>>>>>>    }
>>>>>> }
>>>>>>
>>>>>> ---
>>>>>>
>>>>>> In AppModule.java:
>>>>>>
>>>>>>    // Method for instantiating and configuring the custom messages
>>>>>> source
>>>>>>    public static CommonsMessagesSource buildCommonsMessagesSource()
>>>>>>    {
>>>>>>        CommonsMessagesSource messagesSource = new
>>>>>> CommonsMessagesSource();
>>>>>>        return messagesSource;
>>>>>>    }
>>>>>>
>>>>>>    // Method that decorates the default MessagesSource using the
>>>>>> interceptor class
>>>>>>    @Match("ComponentMessagesSource")
>>>>>>    public static ComponentMessagesSource
>>>>>> decorateComponentMessagesSource
>>>>>>         (Object delegate, CommonsMessagesSource service)
>>>>>>     {
>>>>>>         return new CommonsComponentMessagesSourceInterceptor(service,
>>>>>> (ComponentMessagesSource)delegate);
>>>>>>     }
>>>>>>
>>>>>> This is how I got it to work, there may be other ways ofcourse. There
>>>>>> might even be better ways :-)
>>>>>>
>>>>>> Cheers,
>>>>>> /Hannes
>>>>>>
>>>>>> Michael Capper skrev:
>>>>>>
>>>>>>  Summary: How do I include additional localization-key/values-pairs in
>>>>>>> my
>>>>>>> global messages-catalog, originating from some app_<lang>.properties
>>>>>>> or
>>>>>>> from
>>>>>>> a database?
>>>>>>>
>>>>>>> Hi,
>>>>>>> I'd like to extend the Hash-Table containing the message-keys and
>>>>>>> message-values used in my app. The app_en.properties provides the
>>>>>>> basic
>>>>>>> global localization data, but I need even more data, which is spead
>>>>>>> about
>>>>>>> in
>>>>>>> other .properties-files or even in a Database. What I'd like to do
>>>>>>> would
>>>>>>> be
>>>>>>> to read in the data somewhere, and pass it on to the Messages(or
>>>>>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>>>>>> page/component or use the 'message:' binding, the extra localization
>>>>>>> is
>>>>>>> available at a root level.
>>>>>>>
>>>>>>> Thanks for any help or pointers,
>>>>>>> Mike
>>>>>>>
>>>>>>> ---------------------------------------------------------------------
>>>>>> To unsubscribe, e-mail: [hidden email]
>>>>>> For additional commands, e-mail: [hidden email]
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>  ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>>
>>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Hannes Heijkenskjöld
It has been a while since I worked with that code, so I don't remember
the exact reason. But when customizing some parts of tapestry it seems
that ordinary injects don't work. We did manual injects, by providing
getters and setters and doing this in the AppModule:

// Method for instantiating and configuring the custom messages
public static CommonsMessagesSource buildCommonsMessagesSource(
     final ApplicationStateManager asm)
{
     CommonsMessagesSource messagesSource = new
     CommonsMessagesSource();
     messagesSource.setApplicationStateManager(asm);
     return messagesSource;
}

You can do the same with RequestGlobals.

/Hannes


Inge Solvoll skrev:

> Same problem with RequestGlobals. Comes up as null when I inject it.
>
> Haven't tried the ApplicationStateManager yet, but in my head, the suggested
> code sounds like the equivalent of the @Application state annotation, which
> I already tried. Am I wrong?
>
> On Mon, Aug 25, 2008 at 11:40 PM, Inge Solvoll <[hidden email]>wrote:
>
>> Ok, thanks, I'll try that! I already tried the following in my class that
>> extends AbstractMessages:
>>
>> @Inject
>> private HttpServletRequest request;
>>
>> @ApplicationState
>> private User user;
>>
>> Which didn't work, both were null. But what your saying is that these
>> objects are not possible to inject in this scope, but RequestGlobals is, so
>> the following will work?
>>
>> @Inject
>> private RequestGlobals requestGlobals
>>
>> Regards
>> Inge
>>
>>
>> On Mon, Aug 25, 2008 at 3:57 PM, Hannes Heijkenskjöld <
>> [hidden email]> wrote:
>>
>>> Hi Inge,
>>>
>>> glad to have been able to help you!
>>>
>>> For your current problem, I can see two immediate solutions.
>>>
>>> 1. Store your user object as an ASO and inject ApplicationStateManager
>>> into your Messages implementation. Get the user object instance by calling
>>> applicationStateManager.get(UserObject.class).
>>>
>>> 2. Inject RequestGlobals in your Messages implementation. Then you can get
>>> the request by calling requestGlobals.getHTTPServletRequest().
>>>
>>> I hope this helps
>>>
>>> /Hannes
>>>
>>> Inge Solvoll skrev:
>>>
>>>  Hi, Hannes!
>>>> I tried your implementation, and it worked right away, I just returned
>>>> the
>>>> value "TESTING TESTING" from my DbMessages class that extends Abstract
>>>> Messages.
>>>>
>>>> But, what I need is for the valueForKey(String key) method to be aware of
>>>> the languageId/customerId of the currently logged in user. In all my
>>>> other
>>>> pages, I get this through injecting the request and getting my user
>>>> object
>>>> from HttpSession. Alternatively this can be moved to an ASO, but I
>>>> haven't
>>>> taken the time to do that yet.
>>>>
>>>> I've tried injecting HttpServletRequest and ApplicationState on all the
>>>> classes listed in the implementation from Hannes, but none of them work
>>>> (only null values). How can I make information from the current user
>>>> session
>>>> available to my AbstractMessages implementation?
>>>>
>>>> Regards
>>>>
>>>> Inge
>>>>
>>>> On Wed, Apr 16, 2008 at 10:22 AM, Hannes Heijkenskjöld <
>>>> [hidden email]> wrote:
>>>>
>>>>  Great!
>>>>> Glad I could help you. :-)
>>>>>
>>>>> /Hannes
>>>>>
>>>>> Michael Capper skrev:
>>>>>
>>>>>  Thanks Hannes!
>>>>>
>>>>>> I tried the route with the Decorator once, and ended up having to write
>>>>>> some
>>>>>> java-code into the interceptor via the BodyBuilder .... 'twas not nice.
>>>>>>
>>>>>> Your example worked great, i only had to pass some more Services
>>>>>> (AssetSource to get the URLs, my ProjectContextProvider to get the
>>>>>> ResourceBundles for my text) into the build-Method in AppModule, then i
>>>>>> could return the value for a Message-Key from the bundles, or if not
>>>>>> existant, from the fallbackMessages.
>>>>>>
>>>>>> Cheers,
>>>>>> Mike
>>>>>>
>>>>>>
>>>>>> Hannes Heijkenskjöld wrote:
>>>>>>
>>>>>>  Hi
>>>>>>> I have recently wondered about the same, and have now built something
>>>>>>> that after much trial and error now works.
>>>>>>>
>>>>>>> Basically, I have created a Messages implementation that wraps both a
>>>>>>> tapestry Messages object and our own database messages. If a message
>>>>>>> is not
>>>>>>> found in our database, the default Messages object is queried.
>>>>>>>
>>>>>>> To get Tapestry to use my Messages implementation i had to decorate
>>>>>>> the
>>>>>>> ComponentMessagesSource (and ValidationMessagesSource), since it is
>>>>>>> not
>>>>>>> possible to replace the one in Tapestry. I think I read about
>>>>>>> decorating
>>>>>>> services here:
>>>>>>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.html and
>>>>>>> on
>>>>>>> the mailing list.
>>>>>>>
>>>>>>> My messages source service doesn't implement the
>>>>>>> ComponentMessagesSource
>>>>>>> interface. I use an interception object for that. Hopefully you can
>>>>>>> understand how it works from my example code below:
>>>>>>>
>>>>>>> public class CommonsMessages extends AbstractMessages {
>>>>>>>
>>>>>>>    private Messages fallbackMessages;
>>>>>>>
>>>>>>>    protected String valueForKey(final key) {
>>>>>>>       ... logic to get key from db, use fallbackMessages as
>>>>>>> fallback...
>>>>>>>    }
>>>>>>>
>>>>>>> }
>>>>>>>
>>>>>>> -----
>>>>>>>
>>>>>>> public class CommonsMessagesSource {
>>>>>>>
>>>>>>>    public Messages getMessages(Locale locale, Messages
>>>>>>> fallbackMessages)
>>>>>>> {
>>>>>>>       ... code that creates a CommonsMessages object, with
>>>>>>> fallbackMessages ...
>>>>>>>    }
>>>>>>> }
>>>>>>>
>>>>>>> ----
>>>>>>>
>>>>>>> // This is the decorating class
>>>>>>> public class CommonsComponentMessagesSourceInterceptor implements
>>>>>>>    ComponentMessagesSource {
>>>>>>>
>>>>>>>    private final CommonsMessagesSource service;
>>>>>>>    private final ComponentMessagesSource delegate;
>>>>>>>
>>>>>>>    public CommonsComponentMessagesSourceInterceptor
>>>>>>>        (CommonsMessagesSource service, ComponentMessagesSource
>>>>>>> delegate)
>>>>>>>    {
>>>>>>>        this.service = service;
>>>>>>>        this.delegate = delegate;
>>>>>>>    }
>>>>>>>
>>>>>>>    public Messages getMessages(ComponentModel componentModel, Locale
>>>>>>> locale) {
>>>>>>>
>>>>>>>        return service.getMessages(locale,
>>>>>>>            delegate.getMessages(componentModel, locale));
>>>>>>>    }
>>>>>>>
>>>>>>>    // Not sure about how I should handle this one
>>>>>>>    public void addInvalidationListener(
>>>>>>>        InvalidationListener invalidationlistener) {
>>>>>>>        delegate.addInvalidationListener(invalidationlistener);
>>>>>>>    }
>>>>>>> }
>>>>>>>
>>>>>>> ---
>>>>>>>
>>>>>>> In AppModule.java:
>>>>>>>
>>>>>>>    // Method for instantiating and configuring the custom messages
>>>>>>> source
>>>>>>>    public static CommonsMessagesSource buildCommonsMessagesSource()
>>>>>>>    {
>>>>>>>        CommonsMessagesSource messagesSource = new
>>>>>>> CommonsMessagesSource();
>>>>>>>        return messagesSource;
>>>>>>>    }
>>>>>>>
>>>>>>>    // Method that decorates the default MessagesSource using the
>>>>>>> interceptor class
>>>>>>>    @Match("ComponentMessagesSource")
>>>>>>>    public static ComponentMessagesSource
>>>>>>> decorateComponentMessagesSource
>>>>>>>         (Object delegate, CommonsMessagesSource service)
>>>>>>>     {
>>>>>>>         return new CommonsComponentMessagesSourceInterceptor(service,
>>>>>>> (ComponentMessagesSource)delegate);
>>>>>>>     }
>>>>>>>
>>>>>>> This is how I got it to work, there may be other ways ofcourse. There
>>>>>>> might even be better ways :-)
>>>>>>>
>>>>>>> Cheers,
>>>>>>> /Hannes
>>>>>>>
>>>>>>> Michael Capper skrev:
>>>>>>>
>>>>>>>  Summary: How do I include additional localization-key/values-pairs in
>>>>>>>> my
>>>>>>>> global messages-catalog, originating from some app_<lang>.properties
>>>>>>>> or
>>>>>>>> from
>>>>>>>> a database?
>>>>>>>>
>>>>>>>> Hi,
>>>>>>>> I'd like to extend the Hash-Table containing the message-keys and
>>>>>>>> message-values used in my app. The app_en.properties provides the
>>>>>>>> basic
>>>>>>>> global localization data, but I need even more data, which is spead
>>>>>>>> about
>>>>>>>> in
>>>>>>>> other .properties-files or even in a Database. What I'd like to do
>>>>>>>> would
>>>>>>>> be
>>>>>>>> to read in the data somewhere, and pass it on to the Messages(or
>>>>>>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>>>>>>> page/component or use the 'message:' binding, the extra localization
>>>>>>>> is
>>>>>>>> available at a root level.
>>>>>>>>
>>>>>>>> Thanks for any help or pointers,
>>>>>>>> Mike
>>>>>>>>
>>>>>>>> ---------------------------------------------------------------------
>>>>>>> To unsubscribe, e-mail: [hidden email]
>>>>>>> For additional commands, e-mail: [hidden email]
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>  ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: [hidden email]
>>>>> For additional commands, e-mail: [hidden email]
>>>>>
>>>>>
>>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [hidden email]
>>> For additional commands, e-mail: [hidden email]
>>>
>>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Inge Solvoll-2
Ahhh!!

That was the missing part. Thank you so much, Hannes, I could never have
figured this out myself. I still don't fully understand the code I just
wrote :) I nice little bridge between tapestry and my custom messages system
though!

Inge

On Tue, Aug 26, 2008 at 9:04 AM, Hannes Heijkenskjöld <
[hidden email]> wrote:

> It has been a while since I worked with that code, so I don't remember the
> exact reason. But when customizing some parts of tapestry it seems that
> ordinary injects don't work. We did manual injects, by providing getters and
> setters and doing this in the AppModule:
>
> // Method for instantiating and configuring the custom messages
> public static CommonsMessagesSource buildCommonsMessagesSource(
>    final ApplicationStateManager asm)
> {
>    CommonsMessagesSource messagesSource = new
>    CommonsMessagesSource();
>    messagesSource.setApplicationStateManager(asm);
>    return messagesSource;
> }
>
> You can do the same with RequestGlobals.
>
> /Hannes
>
>
> Inge Solvoll skrev:
>
>  Same problem with RequestGlobals. Comes up as null when I inject it.
>>
>> Haven't tried the ApplicationStateManager yet, but in my head, the
>> suggested
>> code sounds like the equivalent of the @Application state annotation,
>> which
>> I already tried. Am I wrong?
>>
>> On Mon, Aug 25, 2008 at 11:40 PM, Inge Solvoll <[hidden email]
>> >wrote:
>>
>>  Ok, thanks, I'll try that! I already tried the following in my class that
>>> extends AbstractMessages:
>>>
>>> @Inject
>>> private HttpServletRequest request;
>>>
>>> @ApplicationState
>>> private User user;
>>>
>>> Which didn't work, both were null. But what your saying is that these
>>> objects are not possible to inject in this scope, but RequestGlobals is,
>>> so
>>> the following will work?
>>>
>>> @Inject
>>> private RequestGlobals requestGlobals
>>>
>>> Regards
>>> Inge
>>>
>>>
>>> On Mon, Aug 25, 2008 at 3:57 PM, Hannes Heijkenskjöld <
>>> [hidden email]> wrote:
>>>
>>>  Hi Inge,
>>>>
>>>> glad to have been able to help you!
>>>>
>>>> For your current problem, I can see two immediate solutions.
>>>>
>>>> 1. Store your user object as an ASO and inject ApplicationStateManager
>>>> into your Messages implementation. Get the user object instance by
>>>> calling
>>>> applicationStateManager.get(UserObject.class).
>>>>
>>>> 2. Inject RequestGlobals in your Messages implementation. Then you can
>>>> get
>>>> the request by calling requestGlobals.getHTTPServletRequest().
>>>>
>>>> I hope this helps
>>>>
>>>> /Hannes
>>>>
>>>> Inge Solvoll skrev:
>>>>
>>>>  Hi, Hannes!
>>>>
>>>>> I tried your implementation, and it worked right away, I just returned
>>>>> the
>>>>> value "TESTING TESTING" from my DbMessages class that extends Abstract
>>>>> Messages.
>>>>>
>>>>> But, what I need is for the valueForKey(String key) method to be aware
>>>>> of
>>>>> the languageId/customerId of the currently logged in user. In all my
>>>>> other
>>>>> pages, I get this through injecting the request and getting my user
>>>>> object
>>>>> from HttpSession. Alternatively this can be moved to an ASO, but I
>>>>> haven't
>>>>> taken the time to do that yet.
>>>>>
>>>>> I've tried injecting HttpServletRequest and ApplicationState on all the
>>>>> classes listed in the implementation from Hannes, but none of them work
>>>>> (only null values). How can I make information from the current user
>>>>> session
>>>>> available to my AbstractMessages implementation?
>>>>>
>>>>> Regards
>>>>>
>>>>> Inge
>>>>>
>>>>> On Wed, Apr 16, 2008 at 10:22 AM, Hannes Heijkenskjöld <
>>>>> [hidden email]> wrote:
>>>>>
>>>>>  Great!
>>>>>
>>>>>> Glad I could help you. :-)
>>>>>>
>>>>>> /Hannes
>>>>>>
>>>>>> Michael Capper skrev:
>>>>>>
>>>>>>  Thanks Hannes!
>>>>>>
>>>>>>  I tried the route with the Decorator once, and ended up having to
>>>>>>> write
>>>>>>> some
>>>>>>> java-code into the interceptor via the BodyBuilder .... 'twas not
>>>>>>> nice.
>>>>>>>
>>>>>>> Your example worked great, i only had to pass some more Services
>>>>>>> (AssetSource to get the URLs, my ProjectContextProvider to get the
>>>>>>> ResourceBundles for my text) into the build-Method in AppModule, then
>>>>>>> i
>>>>>>> could return the value for a Message-Key from the bundles, or if not
>>>>>>> existant, from the fallbackMessages.
>>>>>>>
>>>>>>> Cheers,
>>>>>>> Mike
>>>>>>>
>>>>>>>
>>>>>>> Hannes Heijkenskjöld wrote:
>>>>>>>
>>>>>>>  Hi
>>>>>>>
>>>>>>>> I have recently wondered about the same, and have now built
>>>>>>>> something
>>>>>>>> that after much trial and error now works.
>>>>>>>>
>>>>>>>> Basically, I have created a Messages implementation that wraps both
>>>>>>>> a
>>>>>>>> tapestry Messages object and our own database messages. If a message
>>>>>>>> is not
>>>>>>>> found in our database, the default Messages object is queried.
>>>>>>>>
>>>>>>>> To get Tapestry to use my Messages implementation i had to decorate
>>>>>>>> the
>>>>>>>> ComponentMessagesSource (and ValidationMessagesSource), since it is
>>>>>>>> not
>>>>>>>> possible to replace the one in Tapestry. I think I read about
>>>>>>>> decorating
>>>>>>>> services here:
>>>>>>>> http://tapestry.apache.org/tapestry5/tapestry-ioc/decorator.htmland
>>>>>>>> on
>>>>>>>> the mailing list.
>>>>>>>>
>>>>>>>> My messages source service doesn't implement the
>>>>>>>> ComponentMessagesSource
>>>>>>>> interface. I use an interception object for that. Hopefully you can
>>>>>>>> understand how it works from my example code below:
>>>>>>>>
>>>>>>>> public class CommonsMessages extends AbstractMessages {
>>>>>>>>
>>>>>>>>   private Messages fallbackMessages;
>>>>>>>>
>>>>>>>>   protected String valueForKey(final key) {
>>>>>>>>      ... logic to get key from db, use fallbackMessages as
>>>>>>>> fallback...
>>>>>>>>   }
>>>>>>>>
>>>>>>>> }
>>>>>>>>
>>>>>>>> -----
>>>>>>>>
>>>>>>>> public class CommonsMessagesSource {
>>>>>>>>
>>>>>>>>   public Messages getMessages(Locale locale, Messages
>>>>>>>> fallbackMessages)
>>>>>>>> {
>>>>>>>>      ... code that creates a CommonsMessages object, with
>>>>>>>> fallbackMessages ...
>>>>>>>>   }
>>>>>>>> }
>>>>>>>>
>>>>>>>> ----
>>>>>>>>
>>>>>>>> // This is the decorating class
>>>>>>>> public class CommonsComponentMessagesSourceInterceptor implements
>>>>>>>>   ComponentMessagesSource {
>>>>>>>>
>>>>>>>>   private final CommonsMessagesSource service;
>>>>>>>>   private final ComponentMessagesSource delegate;
>>>>>>>>
>>>>>>>>   public CommonsComponentMessagesSourceInterceptor
>>>>>>>>       (CommonsMessagesSource service, ComponentMessagesSource
>>>>>>>> delegate)
>>>>>>>>   {
>>>>>>>>       this.service = service;
>>>>>>>>       this.delegate = delegate;
>>>>>>>>   }
>>>>>>>>
>>>>>>>>   public Messages getMessages(ComponentModel componentModel, Locale
>>>>>>>> locale) {
>>>>>>>>
>>>>>>>>       return service.getMessages(locale,
>>>>>>>>           delegate.getMessages(componentModel, locale));
>>>>>>>>   }
>>>>>>>>
>>>>>>>>   // Not sure about how I should handle this one
>>>>>>>>   public void addInvalidationListener(
>>>>>>>>       InvalidationListener invalidationlistener) {
>>>>>>>>       delegate.addInvalidationListener(invalidationlistener);
>>>>>>>>   }
>>>>>>>> }
>>>>>>>>
>>>>>>>> ---
>>>>>>>>
>>>>>>>> In AppModule.java:
>>>>>>>>
>>>>>>>>   // Method for instantiating and configuring the custom messages
>>>>>>>> source
>>>>>>>>   public static CommonsMessagesSource buildCommonsMessagesSource()
>>>>>>>>   {
>>>>>>>>       CommonsMessagesSource messagesSource = new
>>>>>>>> CommonsMessagesSource();
>>>>>>>>       return messagesSource;
>>>>>>>>   }
>>>>>>>>
>>>>>>>>   // Method that decorates the default MessagesSource using the
>>>>>>>> interceptor class
>>>>>>>>   @Match("ComponentMessagesSource")
>>>>>>>>   public static ComponentMessagesSource
>>>>>>>> decorateComponentMessagesSource
>>>>>>>>        (Object delegate, CommonsMessagesSource service)
>>>>>>>>    {
>>>>>>>>        return new CommonsComponentMessagesSourceInterceptor(service,
>>>>>>>> (ComponentMessagesSource)delegate);
>>>>>>>>    }
>>>>>>>>
>>>>>>>> This is how I got it to work, there may be other ways ofcourse.
>>>>>>>> There
>>>>>>>> might even be better ways :-)
>>>>>>>>
>>>>>>>> Cheers,
>>>>>>>> /Hannes
>>>>>>>>
>>>>>>>> Michael Capper skrev:
>>>>>>>>
>>>>>>>>  Summary: How do I include additional localization-key/values-pairs
>>>>>>>> in
>>>>>>>>
>>>>>>>>> my
>>>>>>>>> global messages-catalog, originating from some
>>>>>>>>> app_<lang>.properties
>>>>>>>>> or
>>>>>>>>> from
>>>>>>>>> a database?
>>>>>>>>>
>>>>>>>>> Hi,
>>>>>>>>> I'd like to extend the Hash-Table containing the message-keys and
>>>>>>>>> message-values used in my app. The app_en.properties provides the
>>>>>>>>> basic
>>>>>>>>> global localization data, but I need even more data, which is spead
>>>>>>>>> about
>>>>>>>>> in
>>>>>>>>> other .properties-files or even in a Database. What I'd like to do
>>>>>>>>> would
>>>>>>>>> be
>>>>>>>>> to read in the data somewhere, and pass it on to the Messages(or
>>>>>>>>> ResourceBundle) somehow, so when I @Inject the Messages into a
>>>>>>>>> page/component or use the 'message:' binding, the extra
>>>>>>>>> localization
>>>>>>>>> is
>>>>>>>>> available at a root level.
>>>>>>>>>
>>>>>>>>> Thanks for any help or pointers,
>>>>>>>>> Mike
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> ---------------------------------------------------------------------
>>>>>>>>>
>>>>>>>> To unsubscribe, e-mail: [hidden email]
>>>>>>>> For additional commands, e-mail: [hidden email]
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>  ---------------------------------------------------------------------
>>>>>>>>
>>>>>>> To unsubscribe, e-mail: [hidden email]
>>>>>> For additional commands, e-mail: [hidden email]
>>>>>>
>>>>>>
>>>>>>
>>>>>>  ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [hidden email]
>>>> For additional commands, e-mail: [hidden email]
>>>>
>>>>
>>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: T5 : Expanding Messages Catalog for localization from arbitrary source

Thiago H de Paula Figueiredo
In reply to this post by Inge Solvoll-2
Em Mon, 25 Aug 2008 18:40:27 -0300, Inge Solvoll <[hidden email]>  
escreveu:

> Ok, thanks, I'll try that! I already tried the following in my class that
> extends AbstractMessages:
>
> @Inject
> private HttpServletRequest request;
>
> @ApplicationState
> private User user;
>
> Which didn't work, both were null.

These annotations only work in component and page classes. In other  
classes, you have to inject them yourself. ;)

Thiago

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]