Permalink
Browse files

Introduced CurrentLocaleResolver component allowing to override imple…

…mentation for guessing current locale in MessagesRouter

It may be useful if, for instance, request.getLocale() is not *always* used to guess current user's locale
(for instance if the user's locale is part of the user's data)
  • Loading branch information...
fcamblor committed Oct 26, 2015
1 parent 3689c6b commit 29946921ea7463cc032975900e20513fbbc7cb4d
@@ -0,0 +1,13 @@
package restx.i18n;
import restx.RestxRequest;
import restx.StdRequest;
import java.util.Locale;
/**
* @author fcamblor
*/
public interface CurrentLocaleResolver {
Locale guessLocale(RestxRequest request);
}
@@ -2,6 +2,7 @@
import restx.AppSettings;
import restx.RestxContext;
import restx.RestxRequest;
import restx.factory.Module;
import restx.factory.Provides;
@@ -25,6 +26,16 @@ public Messages messages(AppSettings appSettings) {
}
}
@Provides @Named("CurrentLocaleResolver")
public CurrentLocaleResolver currentLocaleResolver(){
return new CurrentLocaleResolver() {
@Override
public Locale guessLocale(RestxRequest request) {
return request.getLocale();
}
};
}
@Provides @Named("ROOT")
public SupportedLocale rootSupportedLocale() {
return new SupportedLocale(Locale.ROOT);
@@ -23,21 +23,24 @@
@Component
public class MessagesRouter extends RestxRouter {
public MessagesRouter(@Named("Messages") Messages messages,
@Named("restx.i18n.labelsJsTemplate") String labelsJsTemplate) {
super("MessagesRouter", new JsonLabelsRoute(messages), new JsLabelsRoute(messages, labelsJsTemplate));
@Named("restx.i18n.labelsJsTemplate") String labelsJsTemplate,
@Named("CurrentLocaleResolver") CurrentLocaleResolver currentLocaleResolver) {
super("MessagesRouter", new JsonLabelsRoute(messages, currentLocaleResolver), new JsLabelsRoute(messages, labelsJsTemplate, currentLocaleResolver));
}
private static class JsonLabelsRoute extends StdRoute {
private final Messages messages;
private CurrentLocaleResolver currentLocaleResolver;
public JsonLabelsRoute(Messages messages) {
public JsonLabelsRoute(Messages messages, CurrentLocaleResolver currentLocaleResolver) {
super("labels.json", new StdRestxRequestMatcher("GET", "/i18n/labels.json"));
this.messages = messages;
this.currentLocaleResolver = currentLocaleResolver;
}
@Override
public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException {
Iterable<Entry<String, String>> entries = messages.entries(req.getLocale());
Iterable<Entry<String, String>> entries = messages.entries(currentLocaleResolver.guessLocale(req));
handleETagFor(req, resp, entries);
resp.setStatus(HttpStatus.OK);
resp.setContentType("application/json");
@@ -50,12 +53,14 @@ public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp
private static class JsLabelsRoute extends StdRoute {
private final Messages messages;
private final CurrentLocaleResolver currentLocaleResolver;
private final String labelsJsBefore;
private final String labelsJsAfter;
public JsLabelsRoute(Messages messages, String labelsJsTemplate) {
public JsLabelsRoute(Messages messages, String labelsJsTemplate, CurrentLocaleResolver currentLocaleResolver) {
super("labels.js", new StdRestxRequestMatcher("GET", "/i18n/labels.js"));
this.messages = messages;
this.currentLocaleResolver = currentLocaleResolver;
int i = labelsJsTemplate.indexOf("{LABELS}");
if (i == -1) {
throw new IllegalArgumentException("invalid labels js template. It must have {LABELS} token inside." +
@@ -67,7 +72,7 @@ public JsLabelsRoute(Messages messages, String labelsJsTemplate) {
@Override
public void handle(RestxRequestMatch match, RestxRequest req, RestxResponse resp, RestxContext ctx) throws IOException {
Iterable<Entry<String, String>> entries = messages.entries(req.getLocale());
Iterable<Entry<String, String>> entries = messages.entries(currentLocaleResolver.guessLocale(req));
handleETagFor(req, resp, entries);
resp.setStatus(HttpStatus.OK);

0 comments on commit 2994692

Please sign in to comment.