1

I have a Java (JSF) web application that needs to be localized, including text, images and etc (numbers, dates). I'm trying to have programmers to only use

<h:graphicImage value="#{images['i_print.png']}" ... />

... and have the correct localized image automatically picked in the background, according to the user's locale.

One approach is to have resource bundles property files with different image paths for a certain image key. Is there a way this can be done without property files at all?

I mean.. the images directory having the following structure:

-Images
--en
---en_US
---en_CA
--fr
---fr_FR

Is it possible to create resource bundles where the lookup would go through the most localized directory first and going up the tree until the image name is found?

So far I have the following:

(1) An images handler.

<resource-bundle>
    <base-name>com.example.ImageResourcesHandler</base-name>
    <var>images</var>
</resource-bundle>

(2) The handler implementation, which extends resource bundle and handles the call by overriding getObject

public class ImageResourcesHandler extends ResourceBundle {

@Override
protected Object handleGetObject(String key) {...}


}

(3) An Images Control that extends ResourceBundle.Control and is supposed to the the BundleLoading and control

public class ImageControl extends ResourceBundle.Control { ... }

Right now I go through the images root directory and create a map with the names of images as the key and their path as values.

The problem is I don't have a Bundle structure, meaning that if I have the key in two different locales, my map clearly doesn't work (two+ paths are mapped to the same key).

Do you have any ideas on how to accomplish this? I wouldn't like to maintain properties files.

TL;DR: I would like to implement a mechanism that gets images according to the user's locale from the file system without using properties files.

Any ideas are highly appreciated. Thanks!

fabiorocha
  • 140
  • 1
  • 14

1 Answers1

1

JSF has a built-in mechanism to do this: Resources.

A resource is localized by placing it in a directory named /resources/locale/library in the .war. So your .war might contain these files:

  • /resources/images/i_print.png
  • /resources/en_UK/images/i_print.png
  • /resources/de/images/i_print.png
  • /resources/ja/images/i_print.png

…and so on.

For images, this is accessed as <h:graphicImage library="images" name="i_print.png"/>. JSF will automatically select the resources directory based on the locale of the view root.

Stylesheets can be localized in the same way: <h:outputStylesheet library="css" name="apptheme.css"/>.

For resources other than images and stylesheets, JSF provides a built-in EL resolver that treats the identifier resource like a map, whose keys are library + ":" + filename. So page authors could access the above resource as #{resource['images:i_print.png']}, though I think it's far more legible to use the library and name attributes.

VGR
  • 40,506
  • 4
  • 48
  • 63
  • Thank you for this! This might work, I will give a try and comment here again. One thing though, is that file structure correct? Would the locales contain an image folder or would they be inside the images folder (as the first file) ? – fabiorocha Oct 21 '13 at 15:16
  • I think I know what you mean now. That structure is right. Thanks! – fabiorocha Oct 21 '13 at 16:00
  • what would the structure of the three be if I had multiple locales of the same language? e.g.: en, en_US, en_CA ? – fabiorocha Oct 22 '13 at 15:47
  • 1
    You can certainly have a /resources/en directory, a /resources/en_US directory, and a /resources/en_CA directory. I would like to believe that the same fallback rules apply to locale matching as with ResourceBundle rules: if the view locale were en_UK and /resources/en_UK was not present but /resources/en was present, I would expect JSF to use /resources/en, but the JSF specification does not address this situation and I've never put it to the test. – VGR Oct 22 '13 at 17:41
  • Last question, sorry. I'm testing this and it seems that it works based on the language set in the browser correct? Is it possible to make it work based on the application locale? Thanks again! – fabiorocha Oct 22 '13 at 19:19
  • Yes, it uses the language set in the browser, but only if it matches a in faces-config.xml. You can override that by supplying a element in faces-config.xml. Or, you can specify it in , which has an optional locale attribute. – VGR Oct 23 '13 at 16:32