Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 438152d

Browse files
committed
add env.locale() fix jooby-project#249
1 parent ab76400 commit 438152d

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.jooby.hbs;
2+
3+
import org.jooby.Results;
4+
import org.jooby.test.ServerFeature;
5+
import org.junit.Test;
6+
7+
public class HbsFeature extends ServerFeature {
8+
9+
{
10+
use(new Hbs());
11+
12+
get("/", req -> Results.html("org/jooby/hbs/index").put("model", req.param("model").value()));
13+
}
14+
15+
@Test
16+
public void hbs() throws Exception {
17+
request()
18+
.get("/?model=jooby")
19+
.expect("<html><title>/org/jooby/hbs/index.html:org/jooby/hbs/index</title><body>jooby</body></html>");
20+
}
21+
22+
}

jooby/src/main/java/org/jooby/Env.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.common.base.Preconditions.checkArgument;
2222
import static java.util.Objects.requireNonNull;
2323

24+
import java.util.Locale;
2425
import java.util.Optional;
2526
import java.util.function.Predicate;
2627

@@ -69,14 +70,26 @@ interface Builder {
6970
* @param config A config instance.
7071
* @return A new environment.
7172
*/
72-
Env build(Config config);
73+
default Env build(final Config config) {
74+
return build(config, Locale.getDefault());
75+
}
7376

77+
/**
78+
* Build a new environment from a {@link Config} object. The environment is created from the
79+
* <code>application.env</code> property. If such property is missing, env's name must be:
80+
* <code>dev</code>.
81+
*
82+
* @param config A config instance.
83+
* @param locale App locale.
84+
* @return A new environment.
85+
*/
86+
Env build(Config config, Locale locale);
7487
}
7588

7689
/**
7790
* Default builder.
7891
*/
79-
Env.Builder DEFAULT = config -> {
92+
Env.Builder DEFAULT = (config, locale) -> {
8093
requireNonNull(config, "A config is required.");
8194
String name = config.hasPath("application.env") ? config.getString("application.env") : "dev";
8295
return new Env() {
@@ -91,6 +104,11 @@ public Config config() {
91104
return config;
92105
}
93106

107+
@Override
108+
public Locale locale() {
109+
return locale;
110+
}
111+
94112
@Override
95113
public String toString() {
96114
return name();
@@ -108,6 +126,8 @@ public String toString() {
108126
*/
109127
Config config();
110128

129+
Locale locale();
130+
111131
/**
112132
* Returns a string with all substitutions (the <code>${foo.bar}</code> syntax,
113133
* see <a href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
@@ -134,7 +154,8 @@ default String resolve(final String text) {
134154
* @param endDelimiter End delimiter.
135155
* @return A processed string.
136156
*/
137-
default String resolve(final String text, final String startDelimiter, final String endDelimiter) {
157+
default String resolve(final String text, final String startDelimiter,
158+
final String endDelimiter) {
138159
return resolve(text, config(), startDelimiter, endDelimiter);
139160
}
140161

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,13 +3092,13 @@ private Injector bootstrap() throws Exception {
30923092
.orElseGet(
30933093
() -> ConfigFactory.parseResources("application.conf")));
30943094

3095-
Env env = this.env.build(config);
3095+
final Locale locale = LocaleUtils.toLocale(config.getString("application.lang"));
3096+
3097+
Env env = this.env.build(config, locale);
30963098
String envname = env.name();
30973099

30983100
final Charset charset = Charset.forName(config.getString("application.charset"));
30993101

3100-
final Locale locale = LocaleUtils.toLocale(config.getString("application.lang"));
3101-
31023102
String dateFormat = config.getString("application.dateFormat");
31033103
ZoneId zoneId = ZoneId.of(config.getString("application.tz"));
31043104
DateTimeFormatter dateTimeFormatter = DateTimeFormatter

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ public void customEnv() throws Exception {
661661
expect(env.name()).andReturn("dev").times(2);
662662

663663
Env.Builder builder = unit.get(Env.Builder.class);
664-
expect(builder.build(isA(Config.class))).andReturn(env);
664+
expect(builder.build(isA(Config.class), isA(Locale.class))).andReturn(env);
665665

666666
Binder binder = unit.get(Binder.class);
667667

0 commit comments

Comments
 (0)