I'm using this code but I can't set default locale other than english. I tried this with latest Mojarra version:
faces-config.xml:
<?xml version="1.0"?>
<faces-config xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">
<application>
<locale-config>
<default-locale>bg</default-locale>
<supported-locale>en</supported-locale>
<supported-locale>bg</supported-locale>
</locale-config>
<resource-bundle>
<base-name>com.web.common.ints.Text</base-name>
<var>text</var>
</resource-bundle>
</application>
</faces-config>
Java class to load properties files:
public class Text extends ResourceBundle
{
protected static final String BUNDLE_NAME = "com.web.common.ints.text";
protected static final String BUNDLE_EXTENSION = "properties";
protected static final Control UTF8_CONTROL = new UTF8Control();
public Text()
{
setParent(ResourceBundle.getBundle(BUNDLE_NAME,
FacesContext.getCurrentInstance().getViewRoot().getLocale(), UTF8_CONTROL));
}
@Override
protected Object handleGetObject(String key)
{
return parent.getObject(key);
}
@Override
public Enumeration getKeys()
{
return parent.getKeys();
}
protected static class UTF8Control extends Control
{
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
// The below code is copied from default Control#newBundle() implementation.
// Only the PropertyResourceBundle line is changed to read the file as UTF-8.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, BUNDLE_EXTENSION);
ResourceBundle bundle = null;
InputStream stream = null;
if (reload)
{
URL url = loader.getResource(resourceName);
if (url != null)
{
URLConnection connection = url.openConnection();
if (connection != null)
{
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
}
else
{
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null)
{
try
{
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
}
finally
{
stream.close();
}
}
return bundle;
}
}
}
When I open the web page he locale is always in english? How I can set custom locale locale?
Do you have some idea how I can fix this issue?
I used this tutorial: http://jdevelopment.nl/internationalization-jsf-utf8-encoded-properties-files/
So I changed this line:
private Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
To:
locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
If I use the original code I get NPE. But with the second code I get always en as default locale.
<supported-locale>bg</supported-locale>
out. – K.Nicholas