Skip to content

Commit 157d6d8

Browse files
committed
I18N support in jooby. fix #755
1 parent 1dcca6c commit 157d6d8

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

jooby-ftl/src/main/java/org/jooby/internal/ftl/Engine.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.StringWriter;
2525
import java.nio.charset.Charset;
2626
import java.util.HashMap;
27+
import java.util.Locale;
2728
import java.util.Map;
2829

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

66+
// Locale:
67+
Locale locale = (Locale) hash.getOrDefault("locale", ctx.locale());
68+
hash.putIfAbsent("locale", locale);
69+
6570
// locals
66-
hash.putAll(ctx.locals());
71+
Map<String, Object> locals = ctx.locals();
72+
hash.putAll(locals);
6773

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

81+
// Locale:
82+
template.setLocale(locale);
83+
7584
// output
7685
template.process(model, writer);
7786
ctx.type(MediaType.html)
@@ -97,5 +106,4 @@ public String toString() {
97106
return name();
98107
}
99108

100-
101109
}

jooby-hbs/src/main/java/org/jooby/internal/hbs/HbsEngine.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import static java.util.Objects.requireNonNull;
2222

23+
import java.util.Locale;
2324
import java.util.Map;
2425

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

56+
// Locale:
57+
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
58+
locals.put("locale", locale);
59+
5560
com.github.jknack.handlebars.Context context = com.github.jknack.handlebars.Context
5661
.newBuilder(view.model())
5762
// merge request locals (req+sessions locals)

jooby-jade/src/main/java/org/jooby/jade/Engine.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.FileNotFoundException;
2222
import java.util.HashMap;
23+
import java.util.Locale;
2324
import java.util.Map;
2425

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

4647
JadeTemplate template = jadeConfiguration.getTemplate(name);
4748

49+
Map<String, Object> locals = ctx.locals();
50+
4851
Map<String, Object> hash = new HashMap<>();
4952

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

56+
// Locale:
57+
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
58+
hash.put("locale", locale);
59+
5360
// locals & model
54-
hash.putAll(ctx.locals());
61+
hash.putAll(locals);
5562
hash.putAll(view.model());
5663

5764
String output = jadeConfiguration.renderTemplate(template, hash);

jooby-pebble/src/main/java/org/jooby/pebble/PebbleRenderer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.StringWriter;
2323
import java.io.Writer;
2424
import java.util.HashMap;
25+
import java.util.Locale;
2526
import java.util.Map;
2627

2728
import org.jooby.MediaType;
@@ -44,18 +45,26 @@ public PebbleRenderer(final PebbleEngine pebble) {
4445
public void render(final View view, final Renderer.Context ctx) throws Exception {
4546
String vname = view.name();
4647
try {
48+
Map<String, Object> locals = ctx.locals();
49+
4750
PebbleTemplate template = pebble.getTemplate(vname);
4851
Writer writer = new StringWriter();
4952
Map<String, Object> model = new HashMap<>();
53+
5054
// push locals
51-
model.putAll(ctx.locals());
55+
model.putAll(locals);
5256
model.putIfAbsent("_vname", vname);
5357

58+
// Locale:
59+
Locale locale = (Locale) locals.getOrDefault("locale", ctx.locale());
60+
model.putIfAbsent("locale", locale);
61+
5462
// put model
5563
model.putAll(view.model());
5664

5765
// render and send
58-
template.evaluate(writer, model);
66+
template.evaluate(writer, model, locale);
67+
5968
ctx.type(MediaType.html)
6069
.send(writer.toString());
6170
} catch (LoaderException x) {

jooby-pebble/src/test/java/org/jooby/pebble/Issue725.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.FileNotFoundException;
88
import java.io.StringWriter;
99
import java.util.HashMap;
10+
import java.util.Locale;
1011
import java.util.Map;
1112

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

3740
Map model = unit.constructor(HashMap.class).build();
3841
model.putAll(locals);
3942
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
43+
expect(model.putIfAbsent("locale", locale)).andReturn(null);
4044
model.putAll(vmodel);
4145

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

4852
Renderer.Context ctx = unit.get(Renderer.Context.class);
4953
expect(ctx.locals()).andReturn(locals);
54+
expect(ctx.locale()).andReturn(locale);
5055
expect(ctx.type(MediaType.html)).andReturn(ctx);
5156
ctx.send(writer.toString());
5257

5358
PebbleTemplate template = unit.mock(PebbleTemplate.class);
54-
template.evaluate(writer, model);
59+
template.evaluate(writer, model, locale);
5560
LoaderException x = new LoaderException(null, "template not found");
5661
expectLastCall().andThrow(x);
5762

jooby-pebble/src/test/java/org/jooby/pebble/PebbleRendererTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.io.StringWriter;
77
import java.util.HashMap;
8+
import java.util.Locale;
89
import java.util.Map;
910

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

3437
Map model = unit.constructor(HashMap.class).build();
3538
model.putAll(locals);
3639
expect(model.putIfAbsent("_vname", "vname")).andReturn(null);
40+
expect(model.putIfAbsent("locale", locale)).andReturn(null);
3741
model.putAll(vmodel);
3842

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

4549
Renderer.Context ctx = unit.get(Renderer.Context.class);
50+
expect(ctx.locale()).andReturn(locale);
4651
expect(ctx.locals()).andReturn(locals);
4752
expect(ctx.type(MediaType.html)).andReturn(ctx);
4853
ctx.send(writer.toString());
4954

5055
PebbleTemplate template = unit.mock(PebbleTemplate.class);
51-
template.evaluate(writer, model);
56+
template.evaluate(writer, model, locale);
5257

5358
PebbleEngine pebble = unit.get(PebbleEngine.class);
5459
expect(pebble.getTemplate("vname")).andReturn(template);

jooby-thymeleaf/src/main/java/org/jooby/thymeleaf/ThlEngine.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.jooby.thymeleaf;
2020

2121
import java.io.FileNotFoundException;
22+
import java.util.Locale;
2223
import java.util.Map;
2324

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

50+
// Locale:
51+
Locale locale = (Locale) vars.getOrDefault("locale", ctx.locale());
52+
4953
Map model = view.model();
5054
vars.forEach(model::putIfAbsent);
5155
model.putIfAbsent("xss", new Thlxss(env));
5256

53-
IContext thlctx = new org.thymeleaf.context.Context(ctx.locale(), model);
57+
IContext thlctx = new org.thymeleaf.context.Context(locale, model);
5458
String output = this.engine.process(vname, thlctx);
5559

5660
ctx.type(MediaType.html)

0 commit comments

Comments
 (0)