internationalization doesn't work in my velocity template

IMENE FIRAS September 5, 2021

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.
September 7, 2021

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

IMENE FIRAS September 8, 2021

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.
September 8, 2021

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
IMENE FIRAS September 9, 2021

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.
September 9, 2021

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

imene.firas September 10, 2021

@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.
September 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
imene.firas September 10, 2021

@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.
September 10, 2021

Great 🙂

Suggest an answer

Log in or Sign up to answer