0

I am trying to internationalize an application, but when I try to change language, following warning appears:

WARNING: Using en_US for locale because expression /login.xhtml @15,41 locale="#{language.localeCode}" returned null.

It appears in the following line

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

Whole body of .xhtml file is within f:view tag.

The bean it calls is:

@ManagedBean(name = "language")
@SessionScoped
public class LanguageHelper implements Serializable {
private static final long serialVersionUID = 1L;
private String localeCode;

public String getLocaleCode() {
    return localeCode;
}

public void setLocaleCode(String localeCode) {
    changeLang(localeCode);
}

public String changeLang(String langCode) {
    localeCode = langCode;
    FacesContext.getCurrentInstance().getViewRoot()
            .setLocale(new Locale(langCode));
    return null;
}
}

How can I fix this? I would be grateful for any info.

Sarpy
  • 265
  • 1
  • 7
  • 18
  • Where did you initialize `localeCode`? `localeCode` always remains `null`, since you are calling `changeLang(localeCode);` from `setLocaleCode()`, passing `localeCode` as a parameter (an instance member of non-primitive types is assigned a `null` value by default) and purposely assigning `localeCode` to itself (`null`) in `setLocaleCode()` and finally returning `null`. What is the purpose? – Tiny May 23 '15 at 12:30
  • I have defined a default locale, in faces-config (hr), and languages are changed via buttons, such as . Is this ok? – Sarpy May 23 '15 at 12:36
  • This instance member `private String localeCode;` remains `null` as it is not purposely assigned a value. You have to assign it a meaningful value anyway in a due time (`@PostConstruct`, for example) -- `localeCode = FacesContext.getCurrentInstance().getViewRoot().getLocale();`. It will assign a default locale to `localeCode` in the beginning of the user's session (needless to say, depending upon the functional requirement(s)). – Tiny May 23 '15 at 12:43
  • http://stackoverflow.com/a/4830669/1391249, http://stackoverflow.com/a/5391493/1391249 – Tiny May 23 '15 at 12:52
  • This did help a lot, but I can't change locale of welcome page. Nevertheless, thanks for help – Sarpy May 23 '15 at 13:41
  • 2
    "*Locale of welcome page.*" A locale is to be set to its default value which comes into play whenever a page is requested. Anyway, that seems to be another question being fully transparently dependent upon the function requirement/s and cannot reasonably be answered without having any eyesight on it :) – Tiny May 23 '15 at 13:46

0 Answers0