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:[email protected]] 
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 <[email protected]> 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:[email protected]]
> 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: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>



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

Reply via email to