Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reload JSF ResourceBundle?
    primarykey
    data
    text
    <p>I am currently designing an application that needs to be available in so many different languages and after much research I reverted in to using resouceBundles. I am using java se 1.6 and java ee 6 with jsf 1.2 and glassfish 3.1..</p> <p>I got following codes from all over the internet and arranged it in a manner to make sense.. Now the question is, the ResourceBundle loads alright, however it doesnt get refreshed.. i tried so many different ResourceBundle.clearCache() methods but none of those seems to be working...</p> <p>Please note in the Control class I am checking the locale and loading the data to fit that. This will be later replaced by a database backed system. This is only test code to make sure i have the underlying structure working...</p> <p>CODE_SAMPLE: LocalTest.java - The Resource Bundle</p> <pre><code>public class LocalTester extends ResourceBundle { protected static final String BUNDLE_NAME = "local.lantest.LocalTester"; protected static final Control UTF8_CONTROL = new LocalTesterControl(); public LocalTester() { setParent(ResourceBundle.getBundle(BUNDLE_NAME, FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL)); } @Override protected Object handleGetObject(String key) { return parent.getObject(key); } @Override public Enumeration&lt;String&gt; getKeys() { return parent.getKeys(); } public static Control getUTF8_CONTROL() { return UTF8_CONTROL; } } </code></pre> <p>CODE_SAMPLE: LocalTestControl.java - The Resource Bundle Control</p> <pre><code>public class LocalTesterControl extends Control { @Override public boolean needsReload(String baseName, Locale locale, String format, ClassLoader loader, ResourceBundle bundle, long loadTime) { return true; } @Override public long getTimeToLive(String baseName, Locale locale) { return TTL_DONT_CACHE; } @Override public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { System.out.println("---------------------------LOADING LOCALE PACK-------------------------------------"); System.out.println("Printing baseName = " + baseName); System.out.println("Printing locale.getLanguage() = " + locale.getLanguage()); System.out.println("Printing locale.getDisplayLanguage() = " + locale.getDisplayLanguage()); ResourceBundle rbdl; if (locale.getLanguage().compareTo("fr") == 0) { System.out.println("------- LOAD FRENCH language pack load"); rbdl = new ListResourceBundle() { @Override protected Object[][] getContents() { Object[][] aa = { {"welcome", "FR- Welcome" + (new Date().toString())}, {"message", "FR- message"}, {"signoff", "FR- Sign Off"}, {"parametWelcome", "FR-Para Meter Welcome... {0} is parameter..."}, {"numberParaFormat", "FR-NUMBER parameter = {0}...."} }; return aa; } }; } else if (locale.getLanguage().compareTo("es") == 0) { System.out.println("------- LOAD ESPANIOLA language pack load"); rbdl = new ListResourceBundle() { @Override protected Object[][] getContents() { Object[][] aa = { {"welcome", "ES- Welcome" + (new Date().toString())}, {"message", "ES- message"}, {"signoff", "ES- Sign Off"}, {"parametWelcome", "ES-Para Meter Welcome... {0} is parameter..."}, {"numberParaFormat", "ES-NUMBER parameter = {0}...."} }; return aa; } }; } else { System.out.println("------- LOAD DEFAULT language pack load"); rbdl = new ListResourceBundle() { @Override protected Object[][] getContents() { Object[][] aa = { {"welcome", "EN- Welcome" + (new Date().toString())}, {"message", "EN- message"}, {"signoff", "EN- Sign Off"}, {"parametWelcome", "EN-Para Meter Welcome... {0} is parameter..."}, {"numberParaFormat", "EN-NUMBER parameter = {0}...."}, {"notrans", "EN- No Language Translation"} }; return aa; } }; } return rbdl; } } </code></pre> <p>And finally the Faces config:</p> <pre><code>&lt;faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"&gt; &lt;application&gt; &lt;resource-bundle&gt; &lt;base-name&gt;local.lantest.LocalTester&lt;/base-name&gt; &lt;var&gt;bundi&lt;/var&gt; &lt;/resource-bundle&gt; &lt;locale-config&gt; &lt;default-locale&gt;en&lt;/default-locale&gt; &lt;supported-locale&gt;en&lt;/supported-locale&gt; &lt;supported-locale&gt;es&lt;/supported-locale&gt; &lt;supported-locale&gt;fr&lt;/supported-locale&gt; &lt;/locale-config&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p>Any help and advise is much valued.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload