use local language in your site in spring boot application using jsf

step-1:Add bellow line in faces-config.xml

<locale-config>
<default-locale>en_US</default-locale>
<supported-locale>en</supported-locale>
</locale-config>
<resource-bundle>
<base-name>prj</base-name>
<var>msg</var>
</resource-bundle>

step-2:Add this file in your jsf application

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j;
@ManagedBean(name = “language”)
@SessionScoped
@Log4j
public class LanguageBean implements Serializable {

private static final long serialVersionUID = 1L;
private String localeCode = “en_US”;
@Getter
@Setter
private String languageCss = “lang-tamil”;
@Setter
@Getter
String language = “en_US”;
private static Map<String, Object> countries;

static {
countries = new LinkedHashMap<String, Object>();
// unicode for Tamil
countries.put(“\u0BA4\u0BAE\u0BBF\u0BB4\u0BCD”, Locale.US);
countries.put(“English”, Locale.ENGLISH); // label, value
}

public Map<String, Object> getCountriesInMap() {
return countries;
}

public String getLocaleCode() {
return localeCode;
}

public void setLocaleCode(String localeCode) {
this.localeCode = localeCode;
}

// value change event listener
public void countryLocaleCodeChanged(ValueChangeEvent e) {
log.info(“Starts LanguageBean.countryLocaleCodeChanged====>”);

String newLocaleValue = e.getNewValue().toString();
log.info(“newLocaleValue:-” + newLocaleValue);
language = newLocaleValue;

if (newLocaleValue.equals(“en_US”)) {
log.info(“tamil font selected…”);
languageCss = “lang-tamil”;
} else {
log.info(“english font selected…”);
languageCss = “lang-english”;
}
// loop country map to compare the locale code
for (Map.Entry<String, Object> entry : countries.entrySet()) {

if (entry.getValue().toString().equals(newLocaleValue)) {

FacesContext.getCurrentInstance().getViewRoot()
.setLocale((Locale) entry.getValue());

}
}
log.info(“Ends LanguageBean.countryLocaleCodeChanged====>”);
}

}

step-3:

1)prj_en_US.properties—for local language use unicode
prj.infrastructure=\u0B95\u0B9F\u0BCD\u0B9F\u0BAE\u0BC8\u0BAA\u0BCD\u0BAA\u0BC1
prj.information=\u0BB5\u0BBF\u0BB5\u0BB0\u0BAE\u0BCD
prj.organization=\u0BA4\u0BC1\u0BB1\u0BC8 \u0BB5\u0BBF\u0BB5\u0BB0\u0BAE\u0BCD
prj.operations=\u0BAA\u0BA3\u0BBF \u0BB5\u0BBF\u0BB5\u0BB0\u0BAE\u0BCD
prj.scheme=\u0BA4\u0BBF\u0B9F\u0BCD\u0B9F\u0BAE\u0BCD
2)prj_en.properties—for english language
prj.infrastructure=Infrastructure
prj.information=Information
prj.organization=Organization
prj.operations=Operations

step-4:

Add in layout.xhtml

<f:view locale=”#{language.localeCode}” />

step-4:Add values in jsf files and give the key

<h:outputText value=”#{msg[‘prj.customerLogin’]}” />

Advertisements