Closed kpopovic closed 7 years ago
I don't know how internationalization is handled in Thymeleaf, but typically in other template-engines you just put a ResourceBundle
(with locale) in the template context, and call ${bundle.getString("KEY")}
.
Hello, please see link http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#a-multi-language-welcome
Using th:text and externalizing text
http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html#getLocale-- Returns the preferred Locale that the client will accept content in, based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns the default locale for the server.
WebContext ctx = new WebContext(request, response, servletContext, request.getLocale()); templateEngine.process("home", ctx, response.getWriter());
ThymeleafTemplateEngine implementation: https://github.com/perwendel/spark-template-engines/blob/master/spark-template-thymeleaf/src/main/java/spark/template/thymeleaf/ThymeleafTemplateEngine.java
// current render method - refactor it (should not have influence on existing projects)
public String render(ModelAndView modelAndView) {
return render(modelAndView, Locale.getDefault());
}
// proposal to add new method with Locale parameter
public String render(ModelAndView modelAndView, Locale locale) {
Object model = modelAndView.getModel();
if (model instanceof Map) {
// Context context = new Context(); // missing LOCALE object in constructor
// see here: http://www.thymeleaf.org/apidocs/thymeleaf/3.0.2.RELEASE/
Context context = new Context(locale);
context.setVariables((Map<String, Object>) model);
return templateEngine.process(modelAndView.getViewName(), context);
} else {
throw new IllegalArgumentException("modelAndView.getModel() must return a java.util.Map");
}
}
Looks good, feel free to submit a PR!
Hello, I will do PR, make adaptations and do JUnit test(s) as a proof.
TemplateEngine - it is an abstract class, if I add:
public abstract String render(ModelAndView modelAndView, Locale locale)
; // It will break other template engines. They will have to implement this method. The question is if Locale can be somehow 'usefully used' in other engines, e.g. Jade,...
Proposal:
public String render(ModelAndView modelAndView, Locale locale)
into ThymeleafTemplateEngine.java ?private static final String DEFAULT_TEMPLATE_MODE = "XHTML";
This parameter can be removed becase it is not used anywhere in the class.Best Regards, Kresimir
Is it ok just to implement public String render(ModelAndView modelAndView, Locale locale) into ThymeleafTemplateEngine.java ?
Yes, I would say so, this is (afaik) not something most template engines support.
ThymeleafTemplateEngine - private static final String DEFAULT_TEMPLATE_MODE = "XHTML"; This parameter can be removed becase it is not used anywhere in the class.
Cleanup appreciated !
Hello, does ThymeleafTemplateEngine supports internationationalization ?
.../templates/sample_en.properties - it works .../templates/samplehr.properties - Croatian - doesn't work .../templates/sample.properties - any other language ?
Best Regards, Kresimir