Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image i18n in JSF2

I am trying to localize images in JSF, and I want the user to be able to set their 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.

like image 451
kenny_k Avatar asked Nov 05 '22 03:11

kenny_k


1 Answers

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.

like image 137
BalusC Avatar answered Nov 14 '22 19:11

BalusC