In my previous post I explained how we can configure the ListResourceBundle to load property file as resource bundle.
Now we will use same configuration to load locale specific resource bundles.
Step 1 : Define the supported locale in faces-config.xml file.
<locale-config>
<default-locale>en</default-locale>
<supported-locale>de</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<base-name>view.MyResourceBundle</base-name>
<var>viewcontrollerBundle</var>
</resource-bundle>
Step 2 : Create a Util class having method such as "readPropertyFile" taking locale String as a parameter
package view.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Util {
public static Object[][] readPropertyFile(String locale) {
Object[][] data = null;
try {
StringBuilder buffer = new StringBuilder();
buffer.append("C:\\Users\\Gopal\\Desktop\\resource bundle\\MyResourceBundle");
if (null != locale) {
buffer.append(locale);
}
buffer.append(".properties");
String nodeProFile = buffer.toString();
System.out.println("nodeProFile : " + nodeProFile);
Properties prop = new Properties();
//load a properties file
prop.load(new FileInputStream(nodeProFile));
Enumeration enuKeys = prop.keys();
System.out.println("Property size : " + prop.size());
data = new Object[prop.size()][2];
int i = 0;
while (enuKeys.hasMoreElements()) {
String key = (String)enuKeys.nextElement();
String value = prop.getProperty(key);
System.out.println("Key " + key + " -> : " + value);
data[i][0] = key;
data[i][1] = value;
i++;
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return data;
}
}
Step 3 : Create Java Classes extends ListResourceBundle with naming convention as per the locale defined in faces-config.xml file e.g:
Sample source code :
MyResourceBundle.java
package view;
import java.util.ListResourceBundle;
import view.util.Util;
public class MyResourceBundle extends ListResourceBundle {
static final Object[][] contents = Util.readPropertyFile(null);
public Object[][] getContents() {
return contents;
}
}
MyResourceBundle_de.java
package view;
import java.util.ListResourceBundle;
import view.util.Util;
public class MyResourceBundle_de extends ListResourceBundle {
static final Object[][] contents = Util.readPropertyFile("_de");
public Object[][] getContents() {
return contents;
}
}
Step 4 : Testing using Test.jspx page.
Basic source code :
<af:panelGroupLayout id="pgl0">
<af:outputText value="#{viewcontrollerBundle.HELLO_WORLD}" id="ot1"/>
</af:panelGroupLayout>
Demo using mozilla :
Setting en [English] locale :
Changing locale to fr [French] and reload the page :
*Comments and feedback are most welcomed, Thanks.
Now we will use same configuration to load locale specific resource bundles.
Step 1 : Define the supported locale in faces-config.xml file.
<locale-config>
<default-locale>en</default-locale>
<supported-locale>de</supported-locale>
<supported-locale>fr</supported-locale>
</locale-config>
<resource-bundle>
<base-name>view.MyResourceBundle</base-name>
<var>viewcontrollerBundle</var>
</resource-bundle>
Step 2 : Create a Util class having method such as "readPropertyFile" taking locale String as a parameter
package view.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Util {
public static Object[][] readPropertyFile(String locale) {
Object[][] data = null;
try {
StringBuilder buffer = new StringBuilder();
buffer.append("C:\\Users\\Gopal\\Desktop\\resource bundle\\MyResourceBundle");
if (null != locale) {
buffer.append(locale);
}
buffer.append(".properties");
String nodeProFile = buffer.toString();
System.out.println("nodeProFile : " + nodeProFile);
Properties prop = new Properties();
//load a properties file
prop.load(new FileInputStream(nodeProFile));
Enumeration enuKeys = prop.keys();
System.out.println("Property size : " + prop.size());
data = new Object[prop.size()][2];
int i = 0;
while (enuKeys.hasMoreElements()) {
String key = (String)enuKeys.nextElement();
String value = prop.getProperty(key);
System.out.println("Key " + key + " -> : " + value);
data[i][0] = key;
data[i][1] = value;
i++;
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return data;
}
}
Step 3 : Create Java Classes extends ListResourceBundle with naming convention as per the locale defined in faces-config.xml file e.g:
Sample source code :
MyResourceBundle.java
package view;
import java.util.ListResourceBundle;
import view.util.Util;
public class MyResourceBundle extends ListResourceBundle {
static final Object[][] contents = Util.readPropertyFile(null);
public Object[][] getContents() {
return contents;
}
}
MyResourceBundle_de.java
package view;
import java.util.ListResourceBundle;
import view.util.Util;
public class MyResourceBundle_de extends ListResourceBundle {
static final Object[][] contents = Util.readPropertyFile("_de");
public Object[][] getContents() {
return contents;
}
}
Step 4 : Testing using Test.jspx page.
Basic source code :
<af:panelGroupLayout id="pgl0">
<af:outputText value="#{viewcontrollerBundle.HELLO_WORLD}" id="ot1"/>
</af:panelGroupLayout>
Demo using mozilla :
Setting en [English] locale :
Changing locale to fr [French] and reload the page :
*Comments and feedback are most welcomed, Thanks.
.properties and XML files can be translated with the localization platform https://poeditor.com quite simply, you should try it if you're meaning to make multilingual software.
ReplyDelete