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!
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*/
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good job @IMENE FIRAS :) and thank you for accepting the answer :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Great 🙂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.