Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

internationalization doesn't work in my velocity template

Edited

Hi, Community,

in my Jira plugin, I have created a servlet module that renders a Velocity file.

the problem is that internationalization doesn't work only in this file which is rendered by servlet, however, it works the rest of velocity files created by other modules.

when I have for example:

<h1>$i18n.getText("key. Title")</h1>

it renders ' $i18n.getText("key.title") ' in the interface.

I added my velocity file in atlassian.plugin.xml like that:

<web-resource name="gedSearcher" i18n-name-key="ged-searcher.name" key="ged-searcher">
<description key="ged-searcher.description">The gedSearcher Plugin</description>
<resource name="ged searcher" type="velocity" location="templates/GEDSearcher.vm"/>
</web-resource>

but it still doesn't work, could anyone help me please to solve this problem!

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 07, 2021

Hi @IMENE FIRAS can you share the code of your servlet class? How do you render velocity template?

hi @Martin Bayer _MoroSystems_ s_r_o__  ,

here is my servelt's code, and i'm renedering the file GEDSearcher.vm that can't be internationalized

public class GedSearcher extends HttpServlet {

private static final Logger log = LoggerFactory.getLogger(GedSearcher.class);
VelocityTemplatingEngine vte = ComponentAccessor.getComponent(VelocityTemplatingEngine.class);

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
vte.render(file("/templates/GEDSearcher.vm")).asPlainText(resp.getWriter());
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*CODE*/

}

}

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 08, 2021 • edited

Hi @IMENE FIRAS I think the problem is you do not provide i18n service/component in the context. Most of the modules like Action, Webpanel are already prepared and i18n is injected so it can be used easily.

With Servlet, you need to inject for example I18nResolver (com.atlassian.sal.api.message.I18nResolver) to your servlet and you need to provide in a context (Map) when rendering file.

//pseudo code only
map = ["i18n", I18nResolver]
vte.render(file("/templates/GEDSearcher.vm")).applying(map).asPlainText(resp.getWriter())

Let me know if you need more help :) 

Like IMENE FIRAS likes this

Hello @Martin Bayer _MoroSystems_ s_r_o__ ,

thanks for your useful answer, it allowed me to solve my problem and actually everything works well :D

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 09, 2021

Good job @IMENE FIRAS :) and thank you for accepting the answer :)

@Martin Bayer _MoroSystems_ s_r_o__ , in fact iam still having a problem,

 

As u told me i have injected the i18nresolver in my servlet:

@ComponentImport
private final I18nResolver i18n = ComponentAccessor.getOSGiComponentInstanceOfType(I18nResolver.class);

 

When i use the method 'getText' like this  : 

resp.getWriter().write(i18n.getText("my_key")) 

it works well and brings the internitionalized  text.

 

But when i have tried to render all the internitionalized text in my velocity template by using map like this :

Map<String,String> map = i18n.getAllTranslationsForPrefix("prefix_key");

Map<String,Object> mapObj= new HashMap<>(map);

vte.render(file("/templates/GEDSearcher.vm")).applying(mapObj).resp.getWriter());

it didn't work, and it's still rendring ' $i18n.getText("my_key") ' in the interface.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 10, 2021

Hi @IMENE FIRAS , I probably didn't describe it precisely. You need to give i18n  to the context, so you can use $i18n variable in your template. It should be something like:

Map<String,Object> context= new HashMap<>;
context.put("i18n", i18n)
vte.render(file("/templates/GEDSearcher.vm")).applying(context).resp.getWriter());

Now you should be able to use $i18n variable in your velocity template GEDSearcher.vm

Like imene.firas likes this

@Martin Bayer _MoroSystems_ s_r_o__  it's finally working !! thank you very much Mr.Bayen

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 10, 2021

Great 🙂

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events