Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18N support in jooby. fix #755 #790

Merged
merged 1 commit into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions jooby-ftl/src/main/java/org/jooby/internal/ftl/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.jooby.MediaType;
Expand Down Expand Up @@ -62,8 +63,13 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
hash.put("_vpath", template.getName());
hash.put("xss", xss);

// Locale:
Locale locale = (Locale) hash.getOrDefault("locale", ctx.locale());
hash.putIfAbsent("locale", locale);

// locals
hash.putAll(ctx.locals());
Map<String, Object> locals = ctx.locals();
hash.putAll(locals);

// model
hash.putAll(view.model());
Expand All @@ -72,6 +78,9 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
// TODO: remove string writer
StringWriter writer = new StringWriter();

// Locale:
template.setLocale(locale);

// output
template.process(model, writer);
ctx.type(MediaType.html)
Expand All @@ -97,5 +106,4 @@ public String toString() {
return name();
}


}
5 changes: 5 additions & 0 deletions jooby-hbs/src/main/java/org/jooby/internal/hbs/HbsEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static java.util.Objects.requireNonNull;

import java.util.Locale;
import java.util.Map;

import org.jooby.MediaType;
Expand Down Expand Up @@ -52,6 +53,10 @@ public void render(final View view, final Renderer.Context ctx) throws Exception
locals.putIfAbsent("_vname", vname);
locals.putIfAbsent("_vpath", source.filename());

// Locale:
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
locals.put("locale", locale);

com.github.jknack.handlebars.Context context = com.github.jknack.handlebars.Context
.newBuilder(view.model())
// merge request locals (req+sessions locals)
Expand Down
9 changes: 8 additions & 1 deletion jooby-jade/src/main/java/org/jooby/jade/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.jooby.MediaType;
Expand All @@ -45,13 +46,19 @@ public void render(final View view, final Context ctx) throws FileNotFoundExcept

JadeTemplate template = jadeConfiguration.getTemplate(name);

Map<String, Object> locals = ctx.locals();

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

hash.put("_vname", view.name());
hash.put("_vpath", name);

// Locale:
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
hash.put("locale", locale);

// locals & model
hash.putAll(ctx.locals());
hash.putAll(locals);
hash.putAll(view.model());

String output = jadeConfiguration.renderTemplate(template, hash);
Expand Down
13 changes: 11 additions & 2 deletions jooby-pebble/src/main/java/org/jooby/pebble/PebbleRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.jooby.MediaType;
Expand All @@ -44,18 +45,26 @@ public PebbleRenderer(final PebbleEngine pebble) {
public void render(final View view, final Renderer.Context ctx) throws Exception {
String vname = view.name();
try {
Map<String, Object> locals = ctx.locals();

PebbleTemplate template = pebble.getTemplate(vname);
Writer writer = new StringWriter();
Map<String, Object> model = new HashMap<>();

// push locals
model.putAll(ctx.locals());
model.putAll(locals);
model.putIfAbsent("_vname", vname);

// Locale:
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
model.putIfAbsent("locale", locale);

// put model
model.putAll(view.model());

// render and send
template.evaluate(writer, model);
template.evaluate(writer, model, locale);

ctx.type(MediaType.html)
.send(writer.toString());
} catch (LoaderException x) {
Expand Down
7 changes: 6 additions & 1 deletion jooby-pebble/src/test/java/org/jooby/pebble/Issue725.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.FileNotFoundException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.jooby.MediaType;
Expand All @@ -31,12 +32,15 @@ public class Issue725 {
public void templateNotFound() throws Exception {
new MockUnit(PebbleEngine.class, View.class, Renderer.Context.class)
.expect(unit -> {
Locale locale = Locale.ENGLISH;
Map vmodel = unit.mock(Map.class);
Map<String, Object> locals = unit.mock(Map.class);
expect(locals.getOrDefault("locale", locale)).andReturn(locale);

Map model = unit.constructor(HashMap.class).build();
model.putAll(locals);
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
expect(model.putIfAbsent("locale", locale)).andReturn(null);
model.putAll(vmodel);

View view = unit.get(View.class);
Expand All @@ -47,11 +51,12 @@ public void templateNotFound() throws Exception {

Renderer.Context ctx = unit.get(Renderer.Context.class);
expect(ctx.locals()).andReturn(locals);
expect(ctx.locale()).andReturn(locale);
expect(ctx.type(MediaType.html)).andReturn(ctx);
ctx.send(writer.toString());

PebbleTemplate template = unit.mock(PebbleTemplate.class);
template.evaluate(writer, model);
template.evaluate(writer, model, locale);
LoaderException x = new LoaderException(null, "template not found");
expectLastCall().andThrow(x);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.jooby.MediaType;
Expand All @@ -28,12 +29,15 @@ public class PebbleRendererTest {
public void render() throws Exception {
new MockUnit(PebbleEngine.class, View.class, Renderer.Context.class)
.expect(unit -> {
Locale locale = Locale.UK;
Map vmodel = unit.mock(Map.class);
Map<String, Object> locals = unit.mock(Map.class);
expect(locals.getOrDefault("locale", locale)).andReturn(locale);

Map model = unit.constructor(HashMap.class).build();
model.putAll(locals);
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
expect(model.putIfAbsent("locale", locale)).andReturn(null);
model.putAll(vmodel);

View view = unit.get(View.class);
Expand All @@ -43,12 +47,13 @@ public void render() throws Exception {
StringWriter writer = unit.constructor(StringWriter.class).build();

Renderer.Context ctx = unit.get(Renderer.Context.class);
expect(ctx.locale()).andReturn(locale);
expect(ctx.locals()).andReturn(locals);
expect(ctx.type(MediaType.html)).andReturn(ctx);
ctx.send(writer.toString());

PebbleTemplate template = unit.mock(PebbleTemplate.class);
template.evaluate(writer, model);
template.evaluate(writer, model, locale);

PebbleEngine pebble = unit.get(PebbleEngine.class);
expect(pebble.getTemplate("vname")).andReturn(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.jooby.thymeleaf;

import java.io.FileNotFoundException;
import java.util.Locale;
import java.util.Map;

import org.jooby.Env;
Expand Down Expand Up @@ -46,11 +47,14 @@ public void render(final View view, final Context ctx) throws FileNotFoundExcept
Map<String, Object> vars = ctx.locals();
vars.putIfAbsent("_vname", vname);

// Locale:
Locale locale = (Locale) vars.getOrDefault("locale", ctx.locale());

Map model = view.model();
vars.forEach(model::putIfAbsent);
model.putIfAbsent("xss", new Thlxss(env));

IContext thlctx = new org.thymeleaf.context.Context(ctx.locale(), model);
IContext thlctx = new org.thymeleaf.context.Context(locale, model);
String output = this.engine.process(vname, thlctx);

ctx.type(MediaType.html)
Expand Down