@Test public void testResourceBundleCache() throws Exception { Action.ActionResult result = Action.action(new Invocation() { @Override public Result invoke() throws Throwable { return controller.getBundleResource("Messages_fr", null); } }).invoke(); assertThat(toString(result)) .contains("welcome=bonjour") .contains("lang=français"); assertThat(result).hasHeader(HeaderNames.ETAG); final String etag = result.getResult().getHeaders().get(HeaderNames.ETAG); result = Action.action(new Invocation() { @Override public Result invoke() throws Throwable { return controller.getBundleResource("Messages_fr", etag); } }).with(new FakeContext().setHeader(HeaderNames.IF_NONE_MATCH, etag)).invoke(); assertThat(result).hasStatus(Status.NOT_MODIFIED); result = Action.action(new Invocation() { @Override public Result invoke() throws Throwable { return controller.getBundleResource("Messages_fr", etag + "-modified"); } }).with(new FakeContext().setHeader(HeaderNames.IF_NONE_MATCH, etag + "-modified")).invoke(); assertThat(result).hasStatus(Status.OK); }
@Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") public Result getBundleResource(@PathParameter("file") String file, @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch) { if (Strings.isNullOrEmpty(file)) { return notFound().as(MimeTypes.TEXT); } Locale locale = InternationalizationService.DEFAULT_LOCALE; if (file.contains("_")) { locale = Locale.forLanguageTag(file.substring(file.indexOf('_') + 1).replace("_", "-")); } String etag = service.etag(locale); if (ifNoneMatch != null && ifNoneMatch.equals(etag)) { return new Result(Status.NOT_MODIFIED); } Collection<ResourceBundle> bundles = service.bundles(locale); if (bundles.isEmpty()) { return notFound().as(MimeTypes.TEXT); } else { StringBuilder builder = new StringBuilder(); for (ResourceBundle bundle : bundles) { for (String key : bundle.keySet()) { builder.append(key).append("=").append(bundle.getString(key)).append("\n"); } } return ok(builder.toString()).as(MimeTypes.TEXT).with(HeaderNames.ETAG, etag); } }
I18nController extends DefaultController { @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") public Result getBundleResource(@PathParameter("file") String file, @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch) { if (Strings.isNullOrEmpty(file)) { return notFound().as(MimeTypes.TEXT); } Locale locale = InternationalizationService.DEFAULT_LOCALE; if (file.contains("_")) { locale = Locale.forLanguageTag(file.substring(file.indexOf('_') + 1).replace("_", "-")); } String etag = service.etag(locale); if (ifNoneMatch != null && ifNoneMatch.equals(etag)) { return new Result(Status.NOT_MODIFIED); } Collection<ResourceBundle> bundles = service.bundles(locale); if (bundles.isEmpty()) { return notFound().as(MimeTypes.TEXT); } else { StringBuilder builder = new StringBuilder(); for (ResourceBundle bundle : bundles) { for (String key : bundle.keySet()) { builder.append(key).append("=").append(bundle.getString(key)).append("\n"); } } return ok(builder.toString()).as(MimeTypes.TEXT).with(HeaderNames.ETAG, etag); } } }
I18nController extends DefaultController { @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") public Result getBundleResource(@PathParameter("file") String file, @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch) { if (Strings.isNullOrEmpty(file)) { return notFound().as(MimeTypes.TEXT); } Locale locale = InternationalizationService.DEFAULT_LOCALE; if (file.contains("_")) { locale = Locale.forLanguageTag(file.substring(file.indexOf('_') + 1).replace("_", "-")); } String etag = service.etag(locale); if (ifNoneMatch != null && ifNoneMatch.equals(etag)) { return new Result(Status.NOT_MODIFIED); } Collection<ResourceBundle> bundles = service.bundles(locale); if (bundles.isEmpty()) { return notFound().as(MimeTypes.TEXT); } else { StringBuilder builder = new StringBuilder(); for (ResourceBundle bundle : bundles) { for (String key : bundle.keySet()) { builder.append(key).append("=").append(bundle.getString(key)).append("\n"); } } return ok(builder.toString()).as(MimeTypes.TEXT).with(HeaderNames.ETAG, etag); } }  }
I18nController extends DefaultController { @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") public Result getBundleResource(@PathParameter("file") String file, @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch) { if (Strings.isNullOrEmpty(file)) { return notFound().as(MimeTypes.TEXT); } Locale locale = InternationalizationService.DEFAULT_LOCALE; if (file.contains("_")) { locale = Locale.forLanguageTag(file.substring(file.indexOf('_') + 1).replace("_", "-")); } String etag = service.etag(locale); if (ifNoneMatch != null && ifNoneMatch.equals(etag)) { return new Result(Status.NOT_MODIFIED); } Collection<ResourceBundle> bundles = service.bundles(locale); if (bundles.isEmpty()) { return notFound().as(MimeTypes.TEXT); } else { StringBuilder builder = new StringBuilder(); for (ResourceBundle bundle : bundles) { for (String key : bundle.keySet()) { builder.append(key).append("=").append(bundle.getString(key)).append("\n"); } } return ok(builder.toString()).as(MimeTypes.TEXT).with(HeaderNames.ETAG, etag); } }  @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") Result getBundleResource(@PathParameter("file") String file,
                                    @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch); @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.json") Result getBundleResourceForI18Next(@QueryParameter("locales") String listOfLocales,
                                              @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch); @Route(method = HttpMethod.GET, uri = "i18n/{key}") Result getMessage(@Parameter("key") String key, @QueryParameter("locale") Locale locale); @Route(method = HttpMethod.GET, uri = "i18n") Result getMessages(@QueryParameter("locales") List<Locale> locales,
                              @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch); }
I18nController extends DefaultController { @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") public Result getBundleResource(@PathParameter("file") String file, @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch) { if (Strings.isNullOrEmpty(file)) { return notFound().as(MimeTypes.TEXT); } Locale locale = InternationalizationService.DEFAULT_LOCALE; if (file.contains("_")) { locale = Locale.forLanguageTag(file.substring(file.indexOf('_') + 1).replace("_", "-")); } String etag = service.etag(locale); if (ifNoneMatch != null && ifNoneMatch.equals(etag)) { return new Result(Status.NOT_MODIFIED); } Collection<ResourceBundle> bundles = service.bundles(locale); if (bundles.isEmpty()) { return notFound().as(MimeTypes.TEXT); } else { StringBuilder builder = new StringBuilder(); for (ResourceBundle bundle : bundles) { for (String key : bundle.keySet()) { builder.append(key).append("=").append(bundle.getString(key)).append("\n"); } } return ok(builder.toString()).as(MimeTypes.TEXT).with(HeaderNames.ETAG, etag); } }  @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.properties") Result getBundleResource(@PathParameter("file") String file,
                                    @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch); @Route(method = HttpMethod.GET, uri = "i18n/bundles/{file<.+>}.json") Result getBundleResourceForI18Next(@QueryParameter("locales") String listOfLocales,
                                              @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch); @Route(method = HttpMethod.GET, uri = "i18n/{key}") Result getMessage(@Parameter("key") String key, @QueryParameter("locale") Locale locale); @Route(method = HttpMethod.GET, uri = "i18n") Result getMessages(@QueryParameter("locales") List<Locale> locales,
                              @HttpParameter(HeaderNames.IF_NONE_MATCH) String ifNoneMatch);  }