I am trying to localize images in JSF, and I want the user to be able to set the开发者_如何学Pythonir locale on my site. This I am doing by a call to
FacesContext.getCurrentInstance().getViewRoot().setLocale(_locale);
which works great, with one exception:
<h:graphicImage library="images" name="pic.gif" />
uses the Accept-Language
sent in the browser's request to determine which locale to use. I can work around this by placing a locale string in each .properties
file and referencing images by
<h:graphicImage library="#{resource.locale}/images" name="pic.gif" />
but then there is no fall-back for individual images, so I have to have a copy of every image, whether it is different or not, in every locale's directory. This is quite cumbersome, given that I support 9 locales and probably more in the future. Any advice would be much appreciated.
Interesting issue. There is however no builtin support for this, not by JSF nor by remnant of Java EE. Your best bet is to create a Servlet
which knows about the fallback locale and thus basically does the following test:
if (getServletContext().getResource(request.getPathInfo()) == null) {
// Display fallback image instead.
}
You could eventually do this by a custom JSF component which does the same check.
精彩评论