Tuesday, 20 August 2013

Localization with Hindi language in JSF 2.0

Localization with Hindi language in JSF 2.0

I'm using this English to Hindi Unicode converter. The code produced is
placed in the MessageBundle_hi_IN.properties file.
When a constraint is violated like a mandatory text field. A corresponding
error message should be picked up from this file and displayed in Hindi
language but it displays the same Unicode as in the file without any
conversion like,
खालि नहि रख
शकते.
It is expected to be shown in Hindi like.
खालि नहि रख
शकते.
It means "Cannot be left blank."



The XHTML page.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="#{localeBean.language}"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<f:view locale="#{localeBean.locale}">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<h:form>
<h:selectOneMenu id="languages"
value="#{localeBean.language}" onchange="submit();"
style="position: absolute; right: 0; top: 50px;">
<f:selectItem itemValue="en" itemLabel="English" />
<f:selectItem itemValue="hi" itemLabel="Hindi" />
</h:selectOneMenu>
</h:form>
</h:body>
</f:view>
</html>
The corresponding managed bean.
package admin.mangedbean;
import java.io.Serializable;
import java.util.Locale;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
@ManagedBean
@SessionScoped
public final class LocaleBean implements Serializable
{
private Locale locale;
private static final long serialVersionUID = 1L;
public LocaleBean() {}
@PostConstruct
private void init()
{
locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
}
public Locale getLocale()
{
return locale;
}
public String getLanguage()
{
return locale.getLanguage();
}
public void setLanguage(String language)
{
if(language.equals("hi"))
{
locale = new Locale(language, "IN");
}
else
{
locale = new Locale(language);
}
FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
}
}



The faces-config.xml file.
<application>
<message-bundle>
messages.MessageBundle
</message-bundle>
<locale-config>
<default-locale>en</default-locale>
<supported-locale>hi_IN</supported-locale>
</locale-config>
<resource-bundle>
<base-name>messages.ResourceBundle</base-name>
<var>messages</var>
</resource-bundle>
</application>



This works in JSP something like,
<fmt:setLocale value="hi_IN"/>
<fmt:setBundle basename="bundles.resourceBundle_hi_IN" var="resourceBundle"/>
<fmt:message key="someKey" bundle="${resourceBundle}"/>
What is going wrong here? Am I using a wrong converter? Is this language
not supported by JSF?

1 comment:

  1. You can also try doing the Hindi localization with this online tool: https://poeditor.com/. It offers useful support in many languages.

    ReplyDelete