Skip to content

Jooby 2.x Pebble Module i18n support #2006

Closed

Description

@FKernler

According to the Pebble documentation, i18n is handled via the evaluate() method:
Every PebbleTemplate is assigned a default locale from the PebbleEngine. At the point of evaluation, this locale can be changed with an argument to the evaluate(...) method of the individual template.

However, currently the Jooby Pebble module does not pass any locale information to this method:

  public String render(Context ctx, ModelAndView modelAndView) throws Exception {
    PebbleTemplate template = engine.getTemplate(modelAndView.getView());
    Writer writer = new StringWriter();
    Map<String, Object> model = new HashMap<>(ctx.getAttributes());
    model.putAll(modelAndView.getModel());
    template.evaluate(writer, model);
    return writer.toString();
  }```

It would be nice to have built-in support for this, e.g. via a setLocale() method on the request context or similar.

Activity

  1. imeszaros commented on Sep 22, 2020

    @imeszaros
    Member

    Could this be useful for other template engines too? Freemarker has a similar setting. In that case a Locale property could be added to ModelAndView.

    Otherwise maybe a Function<Context, Locale> to PebbleModule.

  2. jknack commented on Sep 23, 2020

    @jknack
    Member

    The best is to add a locale to ModelAndView

  3. imeszaros commented on Sep 23, 2020

    @imeszaros
    Member

    ThymeleafTemplateEngine already does it similarly:

    Locale locale = (Locale) model.getOrDefault("locale", Locale.getDefault());

    I was thinking on a dedicated property in ModelAndView (getLocale() + setLocale(...)), it would make it more obvious to the user how to specify the locale.

    What do you think, should it be a new property or should PebbleTemplateEngine (and FreemarkerTemplateEngine) fetch it from the map like in ThymeleafTemplateEngine?

  4. jknack commented on Sep 23, 2020

    @jknack
    Member

    your suggestion is lot better: getter/setter on ModelAndView.

    In 1.x there was some crazy (but think useful) locale parsing using language tags from Accept-Language header. We could probably add that back? Still would like to be more clean and simply than it was in 1.x

    If we add that to Context. Then a template engine should get the first non-null locale in order: modelandview, context, fallback to application default

    too much?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions