You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Preetam Palwe <pr...@aftek.com> on 2009/03/13 12:00:13 UTC
Does velocity engine supports I18N ?
Hello all
I am implementing a template based email library using velocity
templates. Now I want to use I18N in it.
Can anyone tell me whether velocity engine supports I18N?
After some googling I found that there are velocity tools for this
purpose. But I think one can use velocity tools in a web based
application. I have a non-web (without any jsp/servlet/struts container)
application under development.
If velocity tools is the solution then one should configure the tools in
order to use in code?
Any help will be really very appreciated.
Thanks in advance
~pp
Re: Does velocity engine supports I18N ?
Posted by Nathan Bubna <nb...@gmail.com>.
The ResourceTool is a "generic" tool. It is meant for offline use.
Just drop the generic jar in your app, and put an instance of the
ResourceTool in your context and it will look for and use a
resources.properties in the root of the classpath (unless you call a
method that specifies a different bundle). If you want to change the
default bundle, call setDefaultBundle. Pretty straightforward.
The MultiViewsTool currently has no support for "offline" use. In its
current incarnation, Servlet classes are required. If you'd like to
change that, patches are welcome. :) It's a tool whose original
contributor has gone away, so it doesn't get much attention.
On Sun, Mar 15, 2009 at 7:33 PM, Preetam Palwe <pr...@aftek.com> wrote:
> Thanks Nathan
>
> I want to use ResourceTool / MultiviewsTool from VelocityTools in offline
> mode (plan java classes no servlets , struts)
> Could you please help me to understand how can I use velocity tools in
> offline mode.
>
>
> -----Original Message-----
> From: Nathan Bubna [mailto:nbubna@gmail.com]
> Sent: Saturday, March 14, 2009 10:19 PM
> To: Velocity Users List
> Subject: Re: Does velocity engine supports I18N ?
>
> On Sat, Mar 14, 2009 at 7:02 AM, Caleb Jones <ca...@gmail.com> wrote:
>> I've done velocity work with I18n and 33 languages. I've always just
> passed
>> in my I18n object from Java into the velocity context then called
>> $!{i18n}.tr('Text to translate') in the template. That way there's only
> one
>> template to maintain.
>
> This method can be simplified further using the ResourceTool from
> VelocityTools.
>
>> Of course, this assumes that the view has the same layout and information
>> for every locale.
>>
>> On Sat, Mar 14, 2009 at 6:08 AM, Byron Foster <by...@base2.cc> wrote:
>>
>>> Couldn't you always maintain your template files in UTF-8, then always
>>> merge in UTF-8 and send out the email in UTF-8. Unless I'm not
>>> understanding the problem. I'm not sure what a I18N tool would do for
> you,
>>> unless this is more of an issue of date and number formatting.
>>>
>>>
>>> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>>>
>>> Precisely I am trying to use Velocity engine as a offline tool to send
>>>> emails.
>>>> I have an option or workaround to keep Locale specific velocity
> templates
>>>> like "body.vm.de" , "body.vm.en_us" etc.
>>>> But I was wondering whether there is an inbuilt support in velocity for
>>>> I18N?
>>>>
>>>> Jian, If you could please share the your parser code which enables
>>>> Velocity
>>>> I18N in offline mode to the mailing list, it would be great!
>>>> I had read about "Multiviews" in velocity tools but looks like I can't
> use
>>>> it in offline mode.
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>>> For additional commands, e-mail: user-help@velocity.apache.org
>>>
>>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
RE: Does velocity engine supports I18N ?
Posted by Preetam Palwe <pr...@aftek.com>.
Thanks Nathan
I want to use ResourceTool / MultiviewsTool from VelocityTools in offline
mode (plan java classes no servlets , struts)
Could you please help me to understand how can I use velocity tools in
offline mode.
-----Original Message-----
From: Nathan Bubna [mailto:nbubna@gmail.com]
Sent: Saturday, March 14, 2009 10:19 PM
To: Velocity Users List
Subject: Re: Does velocity engine supports I18N ?
On Sat, Mar 14, 2009 at 7:02 AM, Caleb Jones <ca...@gmail.com> wrote:
> I've done velocity work with I18n and 33 languages. I've always just
passed
> in my I18n object from Java into the velocity context then called
> $!{i18n}.tr('Text to translate') in the template. That way there's only
one
> template to maintain.
This method can be simplified further using the ResourceTool from
VelocityTools.
> Of course, this assumes that the view has the same layout and information
> for every locale.
>
> On Sat, Mar 14, 2009 at 6:08 AM, Byron Foster <by...@base2.cc> wrote:
>
>> Couldn't you always maintain your template files in UTF-8, then always
>> merge in UTF-8 and send out the email in UTF-8. Unless I'm not
>> understanding the problem. I'm not sure what a I18N tool would do for
you,
>> unless this is more of an issue of date and number formatting.
>>
>>
>> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>>
>> Precisely I am trying to use Velocity engine as a offline tool to send
>>> emails.
>>> I have an option or workaround to keep Locale specific velocity
templates
>>> like "body.vm.de" , "body.vm.en_us" etc.
>>> But I was wondering whether there is an inbuilt support in velocity for
>>> I18N?
>>>
>>> Jian, If you could please share the your parser code which enables
>>> Velocity
>>> I18N in offline mode to the mailing list, it would be great!
>>> I had read about "Multiviews" in velocity tools but looks like I can't
use
>>> it in offline mode.
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Does velocity engine supports I18N ?
Posted by Nathan Bubna <nb...@gmail.com>.
On Sat, Mar 14, 2009 at 7:02 AM, Caleb Jones <ca...@gmail.com> wrote:
> I've done velocity work with I18n and 33 languages. I've always just passed
> in my I18n object from Java into the velocity context then called
> $!{i18n}.tr('Text to translate') in the template. That way there's only one
> template to maintain.
This method can be simplified further using the ResourceTool from VelocityTools.
> Of course, this assumes that the view has the same layout and information
> for every locale.
>
> On Sat, Mar 14, 2009 at 6:08 AM, Byron Foster <by...@base2.cc> wrote:
>
>> Couldn't you always maintain your template files in UTF-8, then always
>> merge in UTF-8 and send out the email in UTF-8. Unless I'm not
>> understanding the problem. I'm not sure what a I18N tool would do for you,
>> unless this is more of an issue of date and number formatting.
>>
>>
>> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>>
>> Precisely I am trying to use Velocity engine as a offline tool to send
>>> emails.
>>> I have an option or workaround to keep Locale specific velocity templates
>>> like "body.vm.de" , "body.vm.en_us" etc.
>>> But I was wondering whether there is an inbuilt support in velocity for
>>> I18N?
>>>
>>> Jian, If you could please share the your parser code which enables
>>> Velocity
>>> I18N in offline mode to the mailing list, it would be great!
>>> I had read about "Multiviews" in velocity tools but looks like I can't use
>>> it in offline mode.
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Does velocity engine supports I18N ?
Posted by Caleb Jones <ca...@gmail.com>.
I've done velocity work with I18n and 33 languages. I've always just passed
in my I18n object from Java into the velocity context then called
$!{i18n}.tr('Text to translate') in the template. That way there's only one
template to maintain.
Of course, this assumes that the view has the same layout and information
for every locale.
On Sat, Mar 14, 2009 at 6:08 AM, Byron Foster <by...@base2.cc> wrote:
> Couldn't you always maintain your template files in UTF-8, then always
> merge in UTF-8 and send out the email in UTF-8. Unless I'm not
> understanding the problem. I'm not sure what a I18N tool would do for you,
> unless this is more of an issue of date and number formatting.
>
>
> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>
> Precisely I am trying to use Velocity engine as a offline tool to send
>> emails.
>> I have an option or workaround to keep Locale specific velocity templates
>> like "body.vm.de" , "body.vm.en_us" etc.
>> But I was wondering whether there is an inbuilt support in velocity for
>> I18N?
>>
>> Jian, If you could please share the your parser code which enables
>> Velocity
>> I18N in offline mode to the mailing list, it would be great!
>> I had read about "Multiviews" in velocity tools but looks like I can't use
>> it in offline mode.
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
Re: Does velocity engine supports I18N ?
Posted by jian chen <ch...@gmail.com>.
Hey, Preetam,
Sorry I couldn't help much on Locale based templating, as I am not familiar
with that myself. But it seems to me that your home-made version of the
Locale usage is ok.
Good luck!
Jian
On Mon, Mar 16, 2009 at 1:42 AM, Preetam Palwe <pr...@aftek.com> wrote:
> Thanks Jian for the code snippet!
>
> Well I have written the similar code and its working!
>
> But the question is still open :-)
>
> I guess I should rephrase what I exactly want to achive.
>
> I have written a Java code to send email using Java Mail APIs. I have a
> email template (body.vm) which I use to frame the HTML body of my mails.
> I pass context and Locale to one of my methods which returns be email body
> using Velocity template.
> Here is the code for the same
>
> private String getEmailBody(String templateFile, Map context, Locale
> locale)
> {
> Template velocityTemplate =
> Velocity.getTemplate(templateFile,"UTF-8");
> StringWriter writer = new StringWriter();
> VelocityContext ctx = new VelocityContext(context);
> velocityTemplate.merge(ctx, writer);
> return writer.toString();
> }
>
> Now in above code Locale object is not getting used. The purpose of Locale
> object is: Depending upon the locale generate the email body.
> E.g. email text in English would be different than that of in German!
>
> Currently I have used a workaround for this as below
>
>
> private String getEmailBody(String templateFile, Map context, Locale
> locale)
> {
> if (locale == null)
> {
> locale = Locale.getDefault();
> }
>
> // get the templateFile as per the locale
> // e.g. body.vm.en_us or body.vm.de etc
>
> templateFile = templateFile + "." +
> locale.toString().toLowerCase();
>
> Template velocityTemplate =
> Velocity.getTemplate(templateFile,"UTF-8");
> StringWriter writer = new StringWriter();
> VelocityContext ctx = new VelocityContext(context);
> velocityTemplate.merge(ctx, writer);
> return writer.toString();
> }
>
> I have defined locale specific vm files and loaded a appropriate vm file at
> runtime using Locale!
>
> But
> I found this solution "unnecessary" as there are VelocityTools like
> ResourceTool / MultiviewsTool which could be used for the same purpose.
> But I am unaware of how to use these tools in offline mode i.e. in java
> environment without servlets/jsps/struts etc
>
> Any help in this regards would be really appreciated
>
> Thanks
> ~PP
>
>
>
>
>
>
> -----Original Message-----
> From: jian chen [mailto:chenjian1227@gmail.com]
> Sent: Monday, March 16, 2009 12:34 PM
> To: Velocity Users List
> Subject: Re: Does velocity engine supports I18N ?
>
> Hi, Preetam,
>
> Here is my sample code to use Velocity offline:
>
> import java.io.File;
> import java.io.OutputStreamWriter;
> import java.io.StringWriter;
>
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpServletResponse;
>
> import org.apache.velocity.app.VelocityEngine;
> import org.apache.velocity.context.Context;
> import org.apache.velocity.runtime.RuntimeConstants;
>
> import util.Util;
>
> public class VelocityParser
> {
> private static VelocityEngine ve;
>
> static
> {
> try
> {
> File templateDir = new File("mytempdir");
>
> ve = new VelocityEngine();
>
> // setting up file resource loader
> ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
> templateDir.getCanonicalPath());
>
> // Jian: this is very important, otherwise, there will be out of
> memory error when a template
> // with #foreach loop is called and the #foreach loop iterates
> through million of times
> ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
> "true");
> //
> ve.setProperty("file.resource.loader.modificationCheckInterval", "5");
>
> // velocimacro configuration
> ve.setProperty(RuntimeConstants.VM_LIBRARY, "macro/spring.vm");
>
> // this setting is such that changing inline velocity macro will
> take effect
> // WITHOUT restarting the web server!
>
> ve.setProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
> "true");
>
> ve.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, "true");
>
> ve.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, "0");
> ve.setProperty(RuntimeConstants.PARSER_POOL_SIZE, "1");
>
> // comment this out if we want to enable logging
> ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
> "org.apache.velocity.runtime.log.NullLogSystem");
>
> ve.setProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE,
> "false");
> ve.setProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE,
> "false");
> ve.setProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE,
> "false");
>
> ve.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, "true");
>
> ve.setProperty(RuntimeConstants.PARSE_DIRECTIVE_MAXDEPTH, "10");
>
> // Jian: this is very important, otherwise, there will be out of
> memory error when a template
> // with #foreach loop is called and the #foreach loop iterates
> through million of times
> ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
> "true");
>
> ve.init();
> }
> catch (Exception e)
> {
> throw new RuntimeException(e);
> }
> }
>
> public static String evaluate(File templateFile, Context ctx) throws
> Exception
> {
> return evaluate(Util.readFileAsString(templateFile, "UTF-8"), ctx);
> }
>
> public static String evaluate(String inputStr, Context ctx) throws
> Exception
> {
> StringWriter sw = new StringWriter();
> ve.evaluate(ctx, sw, "velolog", inputStr);
> return sw.toString();
> }
> }
>
> It is part of my code as I have something more relating to servlet too. So,
> you might want to modify and adapt to your purpose.
>
> Cheers,
>
> Jian
> http://www.JiansNet.com
>
>
>
> On Sun, Mar 15, 2009 at 6:42 PM, Preetam Palwe <pr...@aftek.com> wrote:
>
> > Thanks Byron
> >
> > Yes I have all UTF-8 encodded templates!
> > My problem is I am to passing java.util.Locale with me using this I want
> to
> > load proper velocity template.
> > One simple way is as I mentioned earlier:
> >
> > if (locale == null)
> > {
> > locale = Locale.getDefault();
> > }
> >
> > String templateFile = "xyz.vm"+ "." + locale.toString().toLowerCase();
> >
> > // Use templateFile e.g. xyz.vm.en_us or xyz.vm.de
> >
> >
> > Now I was thinking whether I can use Velocity tools for the same. (Please
> > note: I am in offline mode: no servlets, struts etc)
> >
> >
> >
> > Thanks
> >
> >
> >
> >
> > -----Original Message-----
> > From: Byron Foster [mailto:byron@base2.cc]
> > Sent: Saturday, March 14, 2009 6:39 PM
> > To: Velocity Users List
> > Subject: Re: Does velocity engine supports I18N ?
> >
> > Couldn't you always maintain your template files in UTF-8, then always
> > merge in UTF-8 and send out the email in UTF-8. Unless I'm not
> > understanding the problem. I'm not sure what a I18N tool would do for
> > you, unless this is more of an issue of date and number formatting.
> >
> >
> > On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
> >
> > > Precisely I am trying to use Velocity engine as a offline tool to send
> > > emails.
> > > I have an option or workaround to keep Locale specific velocity
> > > templates
> > > like "body.vm.de" , "body.vm.en_us" etc.
> > > But I was wondering whether there is an inbuilt support in velocity
> > > for
> > > I18N?
> > >
> > > Jian, If you could please share the your parser code which enables
> > > Velocity
> > > I18N in offline mode to the mailing list, it would be great!
> > > I had read about "Multiviews" in velocity tools but looks like I
> > > can't use
> > > it in offline mode.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> > For additional commands, e-mail: user-help@velocity.apache.org
> >
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> > For additional commands, e-mail: user-help@velocity.apache.org
> >
> >
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
RE: Does velocity engine supports I18N ?
Posted by Preetam Palwe <pr...@aftek.com>.
Thanks Jian for the code snippet!
Well I have written the similar code and its working!
But the question is still open :-)
I guess I should rephrase what I exactly want to achive.
I have written a Java code to send email using Java Mail APIs. I have a
email template (body.vm) which I use to frame the HTML body of my mails.
I pass context and Locale to one of my methods which returns be email body
using Velocity template.
Here is the code for the same
private String getEmailBody(String templateFile, Map context, Locale
locale)
{
Template velocityTemplate =
Velocity.getTemplate(templateFile,"UTF-8");
StringWriter writer = new StringWriter();
VelocityContext ctx = new VelocityContext(context);
velocityTemplate.merge(ctx, writer);
return writer.toString();
}
Now in above code Locale object is not getting used. The purpose of Locale
object is: Depending upon the locale generate the email body.
E.g. email text in English would be different than that of in German!
Currently I have used a workaround for this as below
private String getEmailBody(String templateFile, Map context, Locale
locale)
{
if (locale == null)
{
locale = Locale.getDefault();
}
// get the templateFile as per the locale
// e.g. body.vm.en_us or body.vm.de etc
templateFile = templateFile + "." +
locale.toString().toLowerCase();
Template velocityTemplate =
Velocity.getTemplate(templateFile,"UTF-8");
StringWriter writer = new StringWriter();
VelocityContext ctx = new VelocityContext(context);
velocityTemplate.merge(ctx, writer);
return writer.toString();
}
I have defined locale specific vm files and loaded a appropriate vm file at
runtime using Locale!
But
I found this solution "unnecessary" as there are VelocityTools like
ResourceTool / MultiviewsTool which could be used for the same purpose.
But I am unaware of how to use these tools in offline mode i.e. in java
environment without servlets/jsps/struts etc
Any help in this regards would be really appreciated
Thanks
~PP
-----Original Message-----
From: jian chen [mailto:chenjian1227@gmail.com]
Sent: Monday, March 16, 2009 12:34 PM
To: Velocity Users List
Subject: Re: Does velocity engine supports I18N ?
Hi, Preetam,
Here is my sample code to use Velocity offline:
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants;
import util.Util;
public class VelocityParser
{
private static VelocityEngine ve;
static
{
try
{
File templateDir = new File("mytempdir");
ve = new VelocityEngine();
// setting up file resource loader
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
templateDir.getCanonicalPath());
// Jian: this is very important, otherwise, there will be out of
memory error when a template
// with #foreach loop is called and the #foreach loop iterates
through million of times
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
"true");
//
ve.setProperty("file.resource.loader.modificationCheckInterval", "5");
// velocimacro configuration
ve.setProperty(RuntimeConstants.VM_LIBRARY, "macro/spring.vm");
// this setting is such that changing inline velocity macro will
take effect
// WITHOUT restarting the web server!
ve.setProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
"true");
ve.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, "true");
ve.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, "0");
ve.setProperty(RuntimeConstants.PARSER_POOL_SIZE, "1");
// comment this out if we want to enable logging
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.NullLogSystem");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE,
"false");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE,
"false");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE,
"false");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, "true");
ve.setProperty(RuntimeConstants.PARSE_DIRECTIVE_MAXDEPTH, "10");
// Jian: this is very important, otherwise, there will be out of
memory error when a template
// with #foreach loop is called and the #foreach loop iterates
through million of times
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
"true");
ve.init();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static String evaluate(File templateFile, Context ctx) throws
Exception
{
return evaluate(Util.readFileAsString(templateFile, "UTF-8"), ctx);
}
public static String evaluate(String inputStr, Context ctx) throws
Exception
{
StringWriter sw = new StringWriter();
ve.evaluate(ctx, sw, "velolog", inputStr);
return sw.toString();
}
}
It is part of my code as I have something more relating to servlet too. So,
you might want to modify and adapt to your purpose.
Cheers,
Jian
http://www.JiansNet.com
On Sun, Mar 15, 2009 at 6:42 PM, Preetam Palwe <pr...@aftek.com> wrote:
> Thanks Byron
>
> Yes I have all UTF-8 encodded templates!
> My problem is I am to passing java.util.Locale with me using this I want
to
> load proper velocity template.
> One simple way is as I mentioned earlier:
>
> if (locale == null)
> {
> locale = Locale.getDefault();
> }
>
> String templateFile = "xyz.vm"+ "." + locale.toString().toLowerCase();
>
> // Use templateFile e.g. xyz.vm.en_us or xyz.vm.de
>
>
> Now I was thinking whether I can use Velocity tools for the same. (Please
> note: I am in offline mode: no servlets, struts etc)
>
>
>
> Thanks
>
>
>
>
> -----Original Message-----
> From: Byron Foster [mailto:byron@base2.cc]
> Sent: Saturday, March 14, 2009 6:39 PM
> To: Velocity Users List
> Subject: Re: Does velocity engine supports I18N ?
>
> Couldn't you always maintain your template files in UTF-8, then always
> merge in UTF-8 and send out the email in UTF-8. Unless I'm not
> understanding the problem. I'm not sure what a I18N tool would do for
> you, unless this is more of an issue of date and number formatting.
>
>
> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>
> > Precisely I am trying to use Velocity engine as a offline tool to send
> > emails.
> > I have an option or workaround to keep Locale specific velocity
> > templates
> > like "body.vm.de" , "body.vm.en_us" etc.
> > But I was wondering whether there is an inbuilt support in velocity
> > for
> > I18N?
> >
> > Jian, If you could please share the your parser code which enables
> > Velocity
> > I18N in offline mode to the mailing list, it would be great!
> > I had read about "Multiviews" in velocity tools but looks like I
> > can't use
> > it in offline mode.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Does velocity engine supports I18N ?
Posted by jian chen <ch...@gmail.com>.
Hi, Preetam,
Here is my sample code to use Velocity offline:
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants;
import util.Util;
public class VelocityParser
{
private static VelocityEngine ve;
static
{
try
{
File templateDir = new File("mytempdir");
ve = new VelocityEngine();
// setting up file resource loader
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
templateDir.getCanonicalPath());
// Jian: this is very important, otherwise, there will be out of
memory error when a template
// with #foreach loop is called and the #foreach loop iterates
through million of times
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
"true");
//
ve.setProperty("file.resource.loader.modificationCheckInterval", "5");
// velocimacro configuration
ve.setProperty(RuntimeConstants.VM_LIBRARY, "macro/spring.vm");
// this setting is such that changing inline velocity macro will
take effect
// WITHOUT restarting the web server!
ve.setProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL,
"true");
ve.setProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, "true");
ve.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, "0");
ve.setProperty(RuntimeConstants.PARSER_POOL_SIZE, "1");
// comment this out if we want to enable logging
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.NullLogSystem");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_ERROR_STACKTRACE,
"false");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_INFO_STACKTRACE,
"false");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_WARN_STACKTRACE,
"false");
ve.setProperty(RuntimeConstants.RUNTIME_LOG_REFERENCE_LOG_INVALID, "true");
ve.setProperty(RuntimeConstants.PARSE_DIRECTIVE_MAXDEPTH, "10");
// Jian: this is very important, otherwise, there will be out of
memory error when a template
// with #foreach loop is called and the #foreach loop iterates
through million of times
ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE,
"true");
ve.init();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static String evaluate(File templateFile, Context ctx) throws
Exception
{
return evaluate(Util.readFileAsString(templateFile, "UTF-8"), ctx);
}
public static String evaluate(String inputStr, Context ctx) throws
Exception
{
StringWriter sw = new StringWriter();
ve.evaluate(ctx, sw, "velolog", inputStr);
return sw.toString();
}
}
It is part of my code as I have something more relating to servlet too. So,
you might want to modify and adapt to your purpose.
Cheers,
Jian
http://www.JiansNet.com
On Sun, Mar 15, 2009 at 6:42 PM, Preetam Palwe <pr...@aftek.com> wrote:
> Thanks Byron
>
> Yes I have all UTF-8 encodded templates!
> My problem is I am to passing java.util.Locale with me using this I want to
> load proper velocity template.
> One simple way is as I mentioned earlier:
>
> if (locale == null)
> {
> locale = Locale.getDefault();
> }
>
> String templateFile = "xyz.vm"+ "." + locale.toString().toLowerCase();
>
> // Use templateFile e.g. xyz.vm.en_us or xyz.vm.de
>
>
> Now I was thinking whether I can use Velocity tools for the same. (Please
> note: I am in offline mode: no servlets, struts etc)
>
>
>
> Thanks
>
>
>
>
> -----Original Message-----
> From: Byron Foster [mailto:byron@base2.cc]
> Sent: Saturday, March 14, 2009 6:39 PM
> To: Velocity Users List
> Subject: Re: Does velocity engine supports I18N ?
>
> Couldn't you always maintain your template files in UTF-8, then always
> merge in UTF-8 and send out the email in UTF-8. Unless I'm not
> understanding the problem. I'm not sure what a I18N tool would do for
> you, unless this is more of an issue of date and number formatting.
>
>
> On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
>
> > Precisely I am trying to use Velocity engine as a offline tool to send
> > emails.
> > I have an option or workaround to keep Locale specific velocity
> > templates
> > like "body.vm.de" , "body.vm.en_us" etc.
> > But I was wondering whether there is an inbuilt support in velocity
> > for
> > I18N?
> >
> > Jian, If you could please share the your parser code which enables
> > Velocity
> > I18N in offline mode to the mailing list, it would be great!
> > I had read about "Multiviews" in velocity tools but looks like I
> > can't use
> > it in offline mode.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>
RE: Does velocity engine supports I18N ?
Posted by Preetam Palwe <pr...@aftek.com>.
Thanks Byron
Yes I have all UTF-8 encodded templates!
My problem is I am to passing java.util.Locale with me using this I want to
load proper velocity template.
One simple way is as I mentioned earlier:
if (locale == null)
{
locale = Locale.getDefault();
}
String templateFile = "xyz.vm"+ "." + locale.toString().toLowerCase();
// Use templateFile e.g. xyz.vm.en_us or xyz.vm.de
Now I was thinking whether I can use Velocity tools for the same. (Please
note: I am in offline mode: no servlets, struts etc)
Thanks
-----Original Message-----
From: Byron Foster [mailto:byron@base2.cc]
Sent: Saturday, March 14, 2009 6:39 PM
To: Velocity Users List
Subject: Re: Does velocity engine supports I18N ?
Couldn't you always maintain your template files in UTF-8, then always
merge in UTF-8 and send out the email in UTF-8. Unless I'm not
understanding the problem. I'm not sure what a I18N tool would do for
you, unless this is more of an issue of date and number formatting.
On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
> Precisely I am trying to use Velocity engine as a offline tool to send
> emails.
> I have an option or workaround to keep Locale specific velocity
> templates
> like "body.vm.de" , "body.vm.en_us" etc.
> But I was wondering whether there is an inbuilt support in velocity
> for
> I18N?
>
> Jian, If you could please share the your parser code which enables
> Velocity
> I18N in offline mode to the mailing list, it would be great!
> I had read about "Multiviews" in velocity tools but looks like I
> can't use
> it in offline mode.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Does velocity engine supports I18N ?
Posted by Byron Foster <by...@base2.cc>.
Couldn't you always maintain your template files in UTF-8, then always
merge in UTF-8 and send out the email in UTF-8. Unless I'm not
understanding the problem. I'm not sure what a I18N tool would do for
you, unless this is more of an issue of date and number formatting.
On Mar 14, 2009, at 0:35 , Preetam Palwe wrote:
> Precisely I am trying to use Velocity engine as a offline tool to send
> emails.
> I have an option or workaround to keep Locale specific velocity
> templates
> like "body.vm.de" , "body.vm.en_us" etc.
> But I was wondering whether there is an inbuilt support in velocity
> for
> I18N?
>
> Jian, If you could please share the your parser code which enables
> Velocity
> I18N in offline mode to the mailing list, it would be great!
> I had read about "Multiviews" in velocity tools but looks like I
> can't use
> it in offline mode.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
RE: Does velocity engine supports I18N ?
Posted by Preetam Palwe <pr...@aftek.com>.
Thanks Jian !
Precisely I am trying to use Velocity engine as a offline tool to send
emails.
I have an option or workaround to keep Locale specific velocity templates
like "body.vm.de" , "body.vm.en_us" etc.
But I was wondering whether there is an inbuilt support in velocity for
I18N?
Jian, If you could please share the your parser code which enables Velocity
I18N in offline mode to the mailing list, it would be great!
I had read about "Multiviews" in velocity tools but looks like I can't use
it in offline mode.
Thanks for help!
~pp
-----Original Message-----
From: jian chen [mailto:chenjian1227@gmail.com]
Sent: Friday, March 13, 2009 10:07 PM
To: Velocity Users List
Subject: Re: Does velocity engine supports I18N ?
I have developed a website for Chinese audience and using Velocity template
engine. It definitely supports different encoding for sure.
I think if you use Velocity to send out emails, it should be ok. However, I
could see the problem with the java email library you are using, that may or
may not support well the UTF-8 encoding for example. I ran into that problem
lately trying to send out emails and I used Apache commons email library,
but for some reason, the email contained garbage text.
I wrote a parser around Velocity to use it as an offline tool to generate
the template. If you are interested, I could share it with this mailing
list.
Jian
http://www.JiansNet.com
On Fri, Mar 13, 2009 at 4:00 AM, Preetam Palwe <pr...@aftek.com> wrote:
> Hello all
>
> I am implementing a template based email library using velocity
> templates. Now I want to use I18N in it.
> Can anyone tell me whether velocity engine supports I18N?
> After some googling I found that there are velocity tools for this
> purpose. But I think one can use velocity tools in a web based
> application. I have a non-web (without any jsp/servlet/struts container)
> application under development.
>
> If velocity tools is the solution then one should configure the tools in
> order to use in code?
>
>
> Any help will be really very appreciated.
>
> Thanks in advance
> ~pp
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
For additional commands, e-mail: user-help@velocity.apache.org
Re: Does velocity engine supports I18N ?
Posted by jian chen <ch...@gmail.com>.
I have developed a website for Chinese audience and using Velocity template
engine. It definitely supports different encoding for sure.
I think if you use Velocity to send out emails, it should be ok. However, I
could see the problem with the java email library you are using, that may or
may not support well the UTF-8 encoding for example. I ran into that problem
lately trying to send out emails and I used Apache commons email library,
but for some reason, the email contained garbage text.
I wrote a parser around Velocity to use it as an offline tool to generate
the template. If you are interested, I could share it with this mailing
list.
Jian
http://www.JiansNet.com
On Fri, Mar 13, 2009 at 4:00 AM, Preetam Palwe <pr...@aftek.com> wrote:
> Hello all
>
> I am implementing a template based email library using velocity
> templates. Now I want to use I18N in it.
> Can anyone tell me whether velocity engine supports I18N?
> After some googling I found that there are velocity tools for this
> purpose. But I think one can use velocity tools in a web based
> application. I have a non-web (without any jsp/servlet/struts container)
> application under development.
>
> If velocity tools is the solution then one should configure the tools in
> order to use in code?
>
>
> Any help will be really very appreciated.
>
> Thanks in advance
> ~pp
>
>
>
>