Solved

Change a locale dynamically

Posted on 2014-01-03
16
249 Views
Last Modified: 2014-02-10
I'm trying to change the locale of my application , but nothing happens . it always stays at a default definition. I'm using JSF and Bean Validation and MessagesProperties of BeanValidation I know that does't need any configuration at faces-config.xml

@Named
@SessionScoped
public class UsuarioMB implements Serializable {

	private static final long serialVersionUID = 1L;
	private String userCSS = "estilo.css";
&#9;private List<String> cssDisponivel;
&#9;private String tema;
&#9;private String linguaEscolhida="pt" ;
&#9;private Locale locale;

&#9;private int tabAtiva;

&#9;public void inicializar() {
&#9;&#9;cssDisponivel = new ArrayList<String>();
&#9;&#9;cssDisponivel.add("estilo.css");
&#9;&#9;cssDisponivel.add("estilo2.css");
&#9;}

&#9;public void alterarTema() {
&#9;&#9;System.out.println("alterado para: " + tema);
&#9;}

&#9;public String alterarEstilo() {
&#9;&#9;return null;
&#9;}

&#9;public String alterarIdioma() {
&#9;&#9;locale = new Locale(linguaEscolhida);
&#9;&#9;FacesContext instance = FacesContext.getCurrentInstance();
&#9;&#9;instance.getViewRoot().setLocale(locale);
&#9;&#9;return null;
&#9;}

&#9;public List<String> getCssDisponivel() {
&#9;&#9;return cssDisponivel;
&#9;}

&#9;public void setCssDisponivel(List<String> cssDisponivel) {
&#9;&#9;this.cssDisponivel = cssDisponivel;
&#9;}

&#9;public String getUserCSS() {
&#9;&#9;return userCSS;
&#9;}

&#9;public void setUserCSS(String userCSS) {
&#9;&#9;this.userCSS = userCSS;
&#9;}

&#9;public String getTema() {
&#9;&#9;if (tema == null) {
&#9;&#9;&#9;tema = "bluesky";
&#9;&#9;}

&#9;&#9;return tema;
&#9;}

&#9;public void setTema(String tema) {
&#9;&#9;this.tema = tema;
&#9;}

&#9;public String getLinguaEscolhida() {
&#9;&#9;return linguaEscolhida;
&#9;}

&#9;public void setLinguaEscolhida(String linguaEscolhida) {
&#9;&#9;this.linguaEscolhida = linguaEscolhida;
&#9;}

&#9;public Locale getLocale() {
&#9;&#9;if (locale == null) {
&#9;&#9;&#9;locale = new Locale(linguaEscolhida);
&#9;&#9;}

&#9;&#9;return locale;
&#9;}

&#9;public int getTabAtiva() {
&#9;&#9;return tabAtiva;
&#9;}

&#9;public void setTabAtiva(int tabAtiva) {
&#9;&#9;this.tabAtiva = tabAtiva;
&#9;}

&#9;public void onTabChange(TabChangeEvent event) {
&#9;&#9;TabView tabView = (TabView) event.getComponent();
&#9;&#9;setTabAtiva(tabView.getActiveIndex());
&#9;}

Open in new window



My faces config:

<message-bundle>br.com.confiabilidade.mensagens.Messages</message-bundle>

&#9;&#9;<resource-bundle>
&#9;&#9;&#9;<base-name>br.com.confiabilidade.mensagens.sistema</base-name>
&#9;&#9;&#9;<var>mensagens</var>
&#9;&#9;</resource-bundle>

&#9;&#9;<locale-config>
&#9;&#9;&#9;<default-locale>en</default-locale>
&#9;&#9;&#9;<supported-locale>pt</supported-locale>
&#9;&#9;&#9;<supported-locale>en</supported-locale>
&#9;&#9;</locale-config>
&#9;</application>

Open in new window



I've put at root view:

<f:view contentType="text/html; charset=UTF-8" encoding="UTF-8" locale="#{usuarioMB.locale}">
0
Comment
Question by:Rafael_Moreira
  • 8
  • 6
16 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 39758577
How you are setting linguaEscolhida?
If you change
return linguaEscolhida; to
return "pt";
is it working?
0
 

Author Comment

by:Rafael_Moreira
ID: 39760159
When i put

      private String linguaEscolhida="en" ;

the locale change to english
and

when put

public void setLinguaEscolhida(String linguaEscolhida) {
            this.linguaEscolhida = "pt";
      }

with the atribute above(private String linguaEscolhida="en" ;

Does't change. keep "en"


Looks like that my method its' not working.
0
 
LVL 11

Expert Comment

by:Manish
ID: 39761239
Whatever value you are setting in method, locale should work on that.
Is it working if you set
public void setLinguaEscolhida(String linguaEscolhida) {
            this.linguaEscolhida = "pt";
      }

?
0
 

Author Comment

by:Rafael_Moreira
ID: 39761753
No . take a look:

public void setLinguaEscolhida(String linguaEscolhida) {
            this.linguaEscolhida = "en";
      }

output on console:



      public Locale getLocale() {
            if (locale == null) {
                  locale = new Locale(linguaEscolhida);
                  System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            }

            return locale;
      }

português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
português --- português


Here is the code  that change the locale

private String linguaEscolhida = "pt";

or

private String linguaEscolhida = "en";
0
 
LVL 11

Expert Comment

by:Manish
ID: 39761759
If you are not calling setLinguaEscolhida method then locale will not change.
I am not getting - how you want to set locale if you are not calling setLinguaEscolhida  method?
0
 

Author Comment

by:Rafael_Moreira
ID: 39761774
In my view:

<p:selectOneMenu value="#{usuarioMB.linguaEscolhida}">  <----HERE
				<f:selectItem itemValue="pt"
					itemLabel="#{mensagens.internacionalizacaoPT}" />


				<f:selectItem itemValue="en"
					itemLabel="#{mensagens.internacionalizacaoEN}" />

			</p:selectOneMenu>

			<p:commandButton action="#{usuarioMB.alterarIdioma}" update="@form"
				value="#{mensagens.internacionalizacaoTrocar}"/>

Open in new window



and when i click at commandbutton  calls the metod:

public String alterarIdioma() {
            locale = new Locale(linguaEscolhida);
            FacesContext instance = FacesContext.getCurrentInstance();
            instance.getViewRoot().setLocale(locale);
            System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            return null;
      }

That would must be working . Would't it?
0
 
LVL 11

Expert Comment

by:Manish
ID: 39761809
so is it not getting set?
what is syout of linguaEscolhida values prints?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:Rafael_Moreira
ID: 39762741
I put a sysout:

public Locale getLocale() {
            if (locale == null) {
                  locale = new Locale(linguaEscolhida);
                  System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            }

            return locale;
      }

português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
português --- português
0
 
LVL 11

Expert Comment

by:Manish
ID: 39764318
No, I am asking about ,
when you click the command button,
what is sys out in alterarIdioma method?
0
 

Author Comment

by:Rafael_Moreira
ID: 39764795
i put a sysout at method and the result is equals:

public String alterarIdioma() {
            locale = new Locale(linguaEscolhida);
            FacesContext instance = FacesContext.getCurrentInstance();
            instance.getViewRoot().setLocale(locale);
            System.out.println(locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
            return null;
      }

Jan 08, 2014 9:06:37 AM org.apache.catalina.startup.Catalina start
Informações: Server startup in 16503 ms
português --- português
português --- português
português --- português
português --- português
português --- português


only change if i meke a alteration at  :

private String linguaEscolhida = "em"

for example .
0
 
LVL 11

Expert Comment

by:Manish
ID: 39767468
When you select pt in dropdown, what value you get in alterarIdioma of linguaEscolhida?
ALso check you are importing correct package for Named and SessionScope annotation?
0
 

Author Comment

by:Rafael_Moreira
ID: 39769947
i vrrifyed that the value of drop down its not been captured.

public String alterarIdioma() {
		locale = new Locale(linguaEscolhida);
		FacesContext instance = FacesContext.getCurrentInstance();
		instance.getViewRoot().setLocale(locale);
		System.out.println("lingua----> "+linguaEscolhida +"Local ----->"+locale.getDisplayName() +" --- "+ locale.getDisplayLanguage());
		return null;
	}

Open in new window


susout:

lingua----> portuguêsLocal ----->português --- português

when i have choosed English.
0
 

Accepted Solution

by:
Rafael_Moreira earned 0 total points
ID: 39777008
With sessionecope

import javax.enterprise.context.SessionScoped;

it Works . the locale change in sysout method alterarIdioma, but the labels does' t change.

very strange .
the faces-config :


      <application>

            <resource-bundle>
                  <base-name>br.com.confiabilidade.resources.sistema</base-name>
                  <var>msg</var>
            </resource-bundle>

            <locale-config>

                  <default-locale>pt_BR</default-locale>
                  <supported-locale>pt</supported-locale>
                  <supported-locale>en</supported-locale>
            </locale-config>
      </application>

      <factory>
            <exception-handler-factory>
                  br.com.confiabilidade.util.jsf.JsfExceptionHandlerFactory
            </exception-handler-factory>
      </factory>

</faces-config>

se the link of my  Developper enviorment:

http://snag.gy/XAYQ1.jpg


ps. i m using bean validation so ValidationMessages_xx.properties has the messages of the project
0
 

Author Closing Comment

by:Rafael_Moreira
ID: 39846843
tha ajax was the problem .

When i used jsf without ajax it Works.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now