partizan
partizan

Reputation: 469

How can Locale.setDefault(new Locale ("en", "US")) be changed at runtime?

I keep looking for the answer on how to reset/change the Locale of a Java (Spring) web application once it's set via the Locale.setDefault(new Locale ("en", "US")). Can someone please help me because it's frustrating seeing that after I set the Locale in my web app, I can't change it by simply calling Locale.setDefault(new Locale ("newLang", "newCountry")).

Is the locale being cached on the server?

Upvotes: 5

Views: 19238

Answers (3)

Udo Held
Udo Held

Reputation: 12548

The Locale.setDefault is a global thing. If you have two users that need to use different locales that won't work out.

You should probably put the locale in the HttpServletRequest.getSession().

After we did the global setDefault it even switched the language and logging of our application server.

Upvotes: 4

hassan goda
hassan goda

Reputation: 1

public String arabic_lng() {
    // Add event code here...
        FacesContext ha_faces= FacesContext.getCurrentInstance();
        Locale ar = new Locale("ar","SA");
       Locale.setDefault(new Locale ("ar","SA"));
        System.out.println(ar.getDisplayName(ar));
        ha_faces.getViewRoot().setLocale(ar);



    return null ;
}

public String eng_lng() {
    // Add event code here...

    FacesContext ha_faces= FacesContext.getCurrentInstance();
    Locale en = new Locale("en", "US");
    System.out.println(en.getDisplayName(en));
    ha_faces.getViewRoot().setLocale(en);
    Locale.setDefault(new Locale ("en", "US"));

    return null ;
}

Upvotes: -3

MaDa
MaDa

Reputation: 10762

The Locale.setDefault(...) has no restrictions as to when or how many times it can be called. The change can be prevented by the security manager, but I assume it's not your case, since you don't mention any exceptions (just in case, though, verify if you don't hide SecurityException in a try-catch block).

Other reason why you might observe such behaviour, is that perhaps your application obtains the default locale only once, caches it and uses forever?

Upvotes: 1

Related Questions