Forums

Home » Liferay Portal » English » 2. Using Liferay » General

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Corné Aussems
i18n in velocity
January 27, 2010 5:40 AM
Answer

Corné Aussems

Rank: Liferay Legend

Posts: 1313

Join Date: October 3, 2006

Recent Posts

I try to get a key translated in velocity template.

#language ("KEY-NAME")

$languageUtil.get($locale, "this-organization-has-x-members","5")<br>
$languageUtil.get($locale, "this-organization-has-x-members")<br>

#language( "this-organization-has-x-members")
#language ( "this-organization-has-x-members","5")


these won't work
Corné Aussems
RESOLVED i18n in velocity
January 27, 2010 5:45 AM
Answer

Corné Aussems

Rank: Liferay Legend

Posts: 1313

Join Date: October 3, 2006

Recent Posts

Ok sorry i found it myself

$languageUtil.format($locale, "this-organization-has-x-members",$users.size())
David García González
RE: RESOLVED i18n in velocity
June 14, 2011 3:53 AM
Answer

David García González

Rank: Regular Member

Posts: 127

Join Date: July 14, 2009

Recent Posts

But what are the resource bundles files where this method reads the translations?

Thanks
Remis Lima Baima
RE: RESOLVED i18n in velocity
October 21, 2011 2:47 AM
Answer

Remis Lima Baima

Rank: New Member

Posts: 8

Join Date: November 5, 2010

Recent Posts

Hi Corné,
Congrats on your Community Excellence Awards. I saw you there :-)
I am trying to send a localized e-mail in my portlet. So I tried all the approaches that you suggested and none of them worked (also listed here: http://blogs.oracle.com/mahipal/entry/how_to_i18n_and_l10n). I get the e-mail but all strings are not-localized. Below my configuration:
portlet.xml:
<resource-bundle>content.Language</resource-bundle>

docroot/WEB-INF/src/content/Language.properties / Language_en_US.properties / Language_en.properties:
dear-webmaster=Dear Webmaster

in my sendMail(...) method:
VelocityEngineUtil.init();
VelocityContext vc = VelocityEngineUtil.getStandardToolsContext();
UnsyncStringWriter body = new UnsyncStringWriter();
VelocityEngineUtil.mergeTemplate("status_mail", ContentUtil.get("status_mail"), vc, body);

in my status_mail.vm template:
#set ($locale = $localeUtil.getDefault()) -> returns "en_US"
#language("dear-webmaster")
$languageUtil.get($locale, "dear-webmaster")
$languageUtil.format($locale, "dear-webmaster", "")

I tried everything and it still is not working (i.e. I only get "dear-webmaster" in my e-mail). Do you (or anyone :-) can give any help here?
Amos Fong
RE: i18n in velocity
October 22, 2011 12:43 AM
Answer

Amos Fong

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1912

Join Date: October 7, 2008

Recent Posts

Since you're creating your own velocity context, I think you need to insert the utils you need manually. If you look at VelocityVariablesImpl.java it shows which utils are inserted.

1velocityContext.put("languageUtil", LanguageUtil.getLanguage());
Remis Lima Baima
RE: i18n in velocity
October 24, 2011 5:58 AM
Answer

Remis Lima Baima

Rank: New Member

Posts: 8

Join Date: November 5, 2010

Recent Posts

Thanks for the answer Amos!
I tried that, but it did not work. I believe that when I call VelocityEngineUtil.getStandardToolsContext() all these variables (i.e. "languageUtil") are automatically added to my velocity context. I will open a LESA ticket as I am short on deadline.
Thanks again.
Corné Aussems
RE: i18n in velocity
October 24, 2011 1:38 PM
Answer

Corné Aussems

Rank: Liferay Legend

Posts: 1313

Join Date: October 3, 2006

Recent Posts

Hi Remis,

Thanks for the congrats, shame we didn't meet.

When looking at WikiPageServiceImpl for instance i think you can see Amos is right;
 1
 2        VelocityContext velocityContext =
 3            VelocityEngineUtil.getWrappedStandardToolsContext();
 4
 5        velocityContext.put("companyId", companyId);
 6        velocityContext.put("contextLine", Diff.CONTEXT_LINE);
 7        velocityContext.put("diffUtil", new DiffUtil());
 8[b]        velocityContext.put("languageUtil", LanguageUtil.getLanguage());[/b]
 9        velocityContext.put("locale", locale);
10        velocityContext.put("sourceResults", diffResults[0]);
11        velocityContext.put("targetResults", diffResults[1]);


But i doubt that your properties are available this way.
Did you try a key from the portals translations files? like "welcome"
Remis Lima Baima
RE: i18n in velocity
October 25, 2011 8:51 AM
Answer

Remis Lima Baima

Rank: New Member

Posts: 8

Join Date: November 5, 2010

Recent Posts

Hi,
Thanks all for the support. Here is the official solution that I got from Liferay Support:
Our LanguageUtil implementation needs to be aware of the portlet's context in order to resolve the locations of the language property files. This is problematic when Velocity is called from within an event listener.
I would recommend globally adding the respective language bundles utilizing a hook plug-in. You can do so by creating a liferay-hook.xml file and inserting the appropriate language-properties elements.

I tested with the language hook and it worked!
Dimitri Tischenko
LanguageUtil
April 9, 2013 8:01 AM
Answer

Dimitri Tischenko

Rank: New Member

Posts: 22

Join Date: November 10, 2011

Recent Posts

Re-reading Liferay Support's answer:

Our LanguageUtil implementation needs to be aware of the portlet's context in order to resolve the locations of the language property files. This is problematic when Velocity is called from within an event listener.
I would recommend globally adding the respective language bundles utilizing a hook plug-in. You can do so by creating a liferay-hook.xml file and inserting the appropriate language-properties elements.


How is the method
1LanguageUtil.get(Locale locale, String key)

is supposed to work, then? In this case, LanguageUtil is not aware of any portlet context.