You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Bill Boland <bo...@attbi.com> on 2002/05/02 08:08:39 UTC

Parsing the results of a reference

I'm trying to find a way to have Velocity "parse" the value of a
reference. I've looked through the documentation and archives...maybe
I've missed something...but it seems like something that might be
useful. At least I'm finding a use for this.

Example:

I have a tool (named "msg") that I've placed into the context in a
servlet that gets messages from a ResourceBundle (using the current
locale in the user's session). It is often used like this:

   <TD>$msg.get( "name" )</TD>
   <TD><input type="text" name="name" value="$user.name" ></TD>

In my case, I want to insert the user's name (a personalized greeting)
into a phrase that also comes from the ResourceBundle:

   $msg.get( "greeting" )

If in my English resource property file used by the ResourceBundle, the
"greeting" value is:

   greeting=How are you, $user.name?

I would get:

   How are you, $user.name?

But I would like to get:

   How are you, John?

where $user.name evaluates to "John" in the current context.

This is treating the result of the reference $msg.get( "greeting" ) as a
template itself that I would like to parse. I know I could add
additional parameters and have the $msg.get() perform the standard
Message parsing (using {0}, {1}, {2}, ...) but this seems redundant
since I have a great parser (Velocity) ready to do the work. It also
alleviates the need for knowing which resource require what parameter.
Well...I hope my simple example makes sense.

Is there a solution already to this kind of problem?

I have toyed with adding tool to the context that holds a reference to
the context itself and then will use the Velocity.evaluate() method to
evaluate the string within the current context (example:
$velocity.evaluate( $msg.get( "greeting" ) ) ) but I'm having a bit of a
problem with it and thought I better stop and ask this question before I
commit more time...only to find that it has been solve already OR is
dangerous or, etc.

Any help or ideas are appreciated.

bill



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Parsing the results of a reference

Posted by Bill Boland <bo...@attbi.com>.
Thanks, Claude. I'll look into alternative loaders as well. But having a
string result be parsed would allow this to be most flexible to any
resource that provides the templates. Of course, there may be
performance/caching benefits using a traditional loader.

bill

-----Original Message-----
From: Claude Brisson [mailto:claude@savoirweb.com] 
Sent: Thursday, May 02, 2002 3:46 AM
To: Velocity Users List
Subject: Re: Parsing the results of a reference

Why don't you actually use templates for your localized strings ?
They don't have to be files, they can reside in a database, using the
DataSourceResourceLoader.
The localization itself could be done at initialization time via the
"resource.templatecolumn" property.
Then, you can just use #parse('greetings').

CloD

----- Original Message ----- 
From: "Bill Boland" <bo...@attbi.com>
To: <ve...@jakarta.apache.org>
Sent: jeudi 2 mai 2002 08:08
Subject: Parsing the results of a reference


> I'm trying to find a way to have Velocity "parse" the value of a
> reference. I've looked through the documentation and archives...maybe
> I've missed something...but it seems like something that might be
> useful. At least I'm finding a use for this.
> 
> Example:
> 
> I have a tool (named "msg") that I've placed into the context in a
> servlet that gets messages from a ResourceBundle (using the current
> locale in the user's session). It is often used like this:
> 
>    <TD>$msg.get( "name" )</TD>
>    <TD><input type="text" name="name" value="$user.name" ></TD>
> 
> In my case, I want to insert the user's name (a personalized greeting)
> into a phrase that also comes from the ResourceBundle:
> 
>    $msg.get( "greeting" )
> 
> If in my English resource property file used by the ResourceBundle,
the
> "greeting" value is:
> 
>    greeting=How are you, $user.name?
> 
> I would get:
> 
>    How are you, $user.name?
> 
> But I would like to get:
> 
>    How are you, John?
> 
> where $user.name evaluates to "John" in the current context.
> 
> This is treating the result of the reference $msg.get( "greeting" ) as
a
> template itself that I would like to parse. I know I could add
> additional parameters and have the $msg.get() perform the standard
> Message parsing (using {0}, {1}, {2}, ...) but this seems redundant
> since I have a great parser (Velocity) ready to do the work. It also
> alleviates the need for knowing which resource require what parameter.
> Well...I hope my simple example makes sense.
> 
> Is there a solution already to this kind of problem?
> 
> I have toyed with adding tool to the context that holds a reference to
> the context itself and then will use the Velocity.evaluate() method to
> evaluate the string within the current context (example:
> $velocity.evaluate( $msg.get( "greeting" ) ) ) but I'm having a bit of
a
> problem with it and thought I better stop and ask this question before
I
> commit more time...only to find that it has been solve already OR is
> dangerous or, etc.
> 
> Any help or ideas are appreciated.
> 
> bill
> 
> 
> 
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
> 
> 


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Parsing the results of a reference

Posted by Claude Brisson <cl...@savoirweb.com>.
Why don't you actually use templates for your localized strings ?
They don't have to be files, they can reside in a database, using the DataSourceResourceLoader.
The localization itself could be done at initialization time via the "resource.templatecolumn" property.
Then, you can just use #parse('greetings').

CloD

----- Original Message ----- 
From: "Bill Boland" <bo...@attbi.com>
To: <ve...@jakarta.apache.org>
Sent: jeudi 2 mai 2002 08:08
Subject: Parsing the results of a reference


> I'm trying to find a way to have Velocity "parse" the value of a
> reference. I've looked through the documentation and archives...maybe
> I've missed something...but it seems like something that might be
> useful. At least I'm finding a use for this.
> 
> Example:
> 
> I have a tool (named "msg") that I've placed into the context in a
> servlet that gets messages from a ResourceBundle (using the current
> locale in the user's session). It is often used like this:
> 
>    <TD>$msg.get( "name" )</TD>
>    <TD><input type="text" name="name" value="$user.name" ></TD>
> 
> In my case, I want to insert the user's name (a personalized greeting)
> into a phrase that also comes from the ResourceBundle:
> 
>    $msg.get( "greeting" )
> 
> If in my English resource property file used by the ResourceBundle, the
> "greeting" value is:
> 
>    greeting=How are you, $user.name?
> 
> I would get:
> 
>    How are you, $user.name?
> 
> But I would like to get:
> 
>    How are you, John?
> 
> where $user.name evaluates to "John" in the current context.
> 
> This is treating the result of the reference $msg.get( "greeting" ) as a
> template itself that I would like to parse. I know I could add
> additional parameters and have the $msg.get() perform the standard
> Message parsing (using {0}, {1}, {2}, ...) but this seems redundant
> since I have a great parser (Velocity) ready to do the work. It also
> alleviates the need for knowing which resource require what parameter.
> Well...I hope my simple example makes sense.
> 
> Is there a solution already to this kind of problem?
> 
> I have toyed with adding tool to the context that holds a reference to
> the context itself and then will use the Velocity.evaluate() method to
> evaluate the string within the current context (example:
> $velocity.evaluate( $msg.get( "greeting" ) ) ) but I'm having a bit of a
> problem with it and thought I better stop and ask this question before I
> commit more time...only to find that it has been solve already OR is
> dangerous or, etc.
> 
> Any help or ideas are appreciated.
> 
> bill
> 
> 
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: Parsing the results of a reference

Posted by Bill Boland <bo...@attbi.com>.
Thanks, Geir. I'll work on this tool some more and if I can't get past
the exception I keep getting I'll post the code and learn from the
masters. At least you've validated that this is a good approach.

Thanks for your time.

Bill
 

-----Original Message-----
From: Geir Magnusson Jr. [mailto:geirm@optonline.net] 
Sent: Thursday, May 02, 2002 3:53 AM
To: velocity-user@jakarta.apache.org
Subject: Re: Parsing the results of a reference

On 5/2/02 2:08 AM, "Bill Boland" <bo...@attbi.com> wrote:

[SNIP]

> I have toyed with adding tool to the context that holds a reference to
> the context itself and then will use the Velocity.evaluate() method to
> evaluate the string within the current context (example:
> $velocity.evaluate( $msg.get( "greeting" ) ) ) but I'm having a bit of
a
> problem with it and thought I better stop and ask this question before
I
> commit more time...only to find that it has been solve already OR is
> dangerous or, etc.

Yep - I think that's the best way to do it...  Just make a tool that
does
the Velocity.evaluate().

What's the problem you are having?


-- 
Geir Magnusson Jr.
geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Parsing the results of a reference

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 5/2/02 2:08 AM, "Bill Boland" <bo...@attbi.com> wrote:

[SNIP]

> I have toyed with adding tool to the context that holds a reference to
> the context itself and then will use the Velocity.evaluate() method to
> evaluate the string within the current context (example:
> $velocity.evaluate( $msg.get( "greeting" ) ) ) but I'm having a bit of a
> problem with it and thought I better stop and ask this question before I
> commit more time...only to find that it has been solve already OR is
> dangerous or, etc.

Yep - I think that's the best way to do it...  Just make a tool that does
the Velocity.evaluate().

What's the problem you are having?


-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
Be a giant.  Take giant steps.  Do giant things...


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>