Re: Sharing what I've learned: locale switching
None None wrote: Because this might be helpful to others, and because I probably would have spent another couple of hours figuring it out on my own without the help of some people on this lsit, I wanted to give back as much as I could. So, here's a consolidated bit of info I've learned about switching language in your Struts apps... What I have is a file manager application, more or less just for me to learn Struts. I wanted to have the ability to switch languages on-the-fly. To do this, I've done the following: Does anyone know how to make the Locale settings global for all web applications deployed in a web container? Is it at all possible? I have four web applications deployed in Jetty and I want to have them share the user chosen locale. Best regards, Jan Nielsen - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
R: Sharing what I've learned: locale switching
Hi Jan What do you mean with sharing user chosen locale? Does your user access thru a common login application? If it's something like that, then you might share your Locale putting it in session Session.setAttribute(myLocale, myLocale); Then retrieving it in the other applications with (Locale)request.getSession().getAttribute(myLocale); This usually works pretty well since Session is a serializable object. Not sure it would work in a clustered environment though -Messaggio originale- Da: Jan Normann Nielsen [mailto:[EMAIL PROTECTED] Inviato: lunedì 17 maggio 2004 10.42 A: Struts Users Mailing List Oggetto: Re: Sharing what I've learned: locale switching None None wrote: Because this might be helpful to others, and because I probably would have spent another couple of hours figuring it out on my own without the help of some people on this lsit, I wanted to give back as much as I could. So, here's a consolidated bit of info I've learned about switching language in your Struts apps... What I have is a file manager application, more or less just for me to learn Struts. I wanted to have the ability to switch languages on-the-fly. To do this, I've done the following: Does anyone know how to make the Locale settings global for all web applications deployed in a web container? Is it at all possible? I have four web applications deployed in Jetty and I want to have them share the user chosen locale. Best regards, Jan Nielsen - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: R: Sharing what I've learned: locale switching
If I understand you right, you want data (in this case the locale) to be stored at a scope higher than session, because it involves more webapps, but less than application, because it is user (session) specific. Far as I know, there is no such scope defined. Maybe you can simulate this behaviour by adding the webapps together as modules of one encapsulating webapp, and store the locale in the session scope for the encapsulating webapp? Just an idea. Jeroen -Oorspronkelijk bericht- Van: Jan Normann Nielsen [mailto:[EMAIL PROTECTED] Verzonden: maandag 17 mei 2004 14:31 Aan: Struts Users Mailing List Onderwerp: Re: R: Sharing what I've learned: locale switching Andrea M. wrote: Hi Jan What do you mean with sharing user chosen locale? Does your user access thru a common login application? If it's something like that, then you might share your Locale putting it in session Session.setAttribute(myLocale, myLocale); Then retrieving it in the other applications with (Locale)request.getSession().getAttribute(myLocale); This usually works pretty well since Session is a serializable object. Not sure it would work in a clustered environment though I have no problem putting the locale in the session if it wasn't for the fact that each deployed web application has its own session handler, so the locale is not shared between web applications. It this possible? Best regards, Jan Nielsen - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
R: R: Sharing what I've learned: locale switching
Hi again I'm afraid I was kind of inaccurate in my previous answer you can retrieve session from the request, but that's only with forwards of course, and forwards work only in the same application. What you can do is to share your objects putting them in JNDI. JNDI tree is shared between all of your contexts, and it's replicated, so it's cluster-safe. Of course you should make sure to have set your object before accessing it from another app, but that's obvious. Hope that helps -Messaggio originale- Da: Jan Normann Nielsen [mailto:[EMAIL PROTECTED] Inviato: lunedì 17 maggio 2004 14.31 A: Struts Users Mailing List Oggetto: Re: R: Sharing what I've learned: locale switching Andrea M. wrote: Hi Jan What do you mean with sharing user chosen locale? Does your user access thru a common login application? If it's something like that, then you might share your Locale putting it in session Session.setAttribute(myLocale, myLocale); Then retrieving it in the other applications with (Locale)request.getSession().getAttribute(myLocale); This usually works pretty well since Session is a serializable object. Not sure it would work in a clustered environment though I have no problem putting the locale in the session if it wasn't for the fact that each deployed web application has its own session handler, so the locale is not shared between web applications. It this possible? Best regards, Jan Nielsen - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Sharing what I've learned: locale switching
Because this might be helpful to others, and because I probably would have spent another couple of hours figuring it out on my own without the help of some people on this lsit, I wanted to give back as much as I could. So, here's a consolidated bit of info I've learned about switching language in your Struts apps... What I have is a file manager application, more or less just for me to learn Struts. I wanted to have the ability to switch languages on-the-fly. To do this, I've done the following: (1) I created two files and placed them in WEB-INF/classes. They are ofmResources_en.properties and ofmResources_de.properties (en for English, de for German). These files contain various text strings in both language. For instance, there is a lable on the screen for file uploads which is defined as follows: labels.uploadFile=Upload a file: and for the German version: labels.uploadFile=hochladen Sie eine Datei: (2) I added the following entry to web.xml, as an init parameter of the ActionServlet: init-param param-nameapplication/param-name param-valueofmResources/param-value /init-param As near as I can tell, NO entries are required in struts-config.xml. You also do NOT need to do anything for each version of the resource file. As long as they are named x_ll.properties, where x is the value of the application parameter above, and ll is a valid country code, that's all there is to it. (3) Next, I added some flag graphics to my web pages, one an American flag, one a German flag. Here is the HTML for them: form name=changeLocaleForm method=post action=changeLocale.ofm style=display:inline; input type=hidden name=languageCode table width=100% border=0 cellpadding=0 cellspacing=0trtd align=right input type=image src=img/flag_usa.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='en'; input type=image src=img/flag_germany.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='de'; /td/tr?table /form Pretty trivial stuff there. (4) Next, I created an ActionForm called ChangeLocaleActionForm as follows: package com.mycompany.ofm.actionforms; import org.apache.struts.action.*; public class ChangeLocaleActionForm extends ActionForm { private String languageCode = null; public ChangeLocaleActionForm() { languageCode = null; } public void setLanguageCode(String inLanguageCode) { languageCode = inLanguageCode; } public String getLsnguageCode() { return languageCode; } } (5) Next, I created an accompanying Action: package com.mycompany.ofm.actions; import java.util.*; import javax.servlet.http.*; import com.omnytex.ofm.actionforms.*; import org.apache.struts.*; import org.apache.struts.action.*; public class ChangeLocaleAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ChangeLocaleActionForm claf = (ChangeLocaleActionForm)form; String languageCode = claf.getLsnguageCode(); request.getSession().setAttribute(Globals.LOCALE_KEY, new Locale(languageCode)); return mapping.findForward(showPathContents); } } As it turns out as someone here informed me, there is automagically a Locale in session, created based on what is sent by the browser. So, by default on my system the value en_US is stored in session under the name Globals.LOCALE_KEY. By the way, as near as I can tell, the _US portion of the language code doesn't matter (I'm sure it MATTERS, but for what I'm describing it doesn't). So, this allows one to switch the locale (read: language) of the app by clicking a flag. No big deal. (6) To make use of this all, there are two concerns... One is messages in a JSP rendered with the bean:message tag, the other is messages returned from an Action that you want to display to the user. For the JSP side of things, it's simple... you just do this... bean:message key=labels.uploadFile/ Struts uses the Locale stored in session to pull the key from the correct resource file. Yeah, it's that easy! As I said previously, the fact that to start my Locale contains en_US doesn't seem to matter... Struts looks to be smart enough to look for a properties file with just _en in the name... I presume that if I named the file ofmResources_en_US.properties it would work as well, but I haven't verified that. For messages returned from an Action, I have found that this code does what I want: lpcaf.setMessage(getResources(request).getMessage( (Locale)request.getSession().getAttribute(Globals.LOCALE_KEY), messages.deleteFailed)); This is just setting a message in an ActionForm that is returned to the view. In the view I do: body
RE: Sharing what I've learned: locale switching
Additionally, if you want other key(other than org.apache.struts.action.LOCALE =Globals.LOCALE_KEY) to store your locale, you can do this: session.setAttribute(myNewMeaningfulKey,myLocaleObject), then in your JSP you would use this: bean:message key=myPropertyKey locale=myNewMeaningfulKey /. This is the way the locale attribute in message tag is used in struts. Thanks. Larry Zhang -Original Message- From: Wang, Yuanbo [mailto:[EMAIL PROTECTED] Sent: Friday, May 14, 2004 2:40 PM To: Struts Users Mailing List Subject: RE: Sharing what I've learned: locale switching Thanks for sharing the information. Basically struts using the following method to decide which Locale is in the session, then load the corresponding resource bundle: protected Locale getLocale(HttpServletRequest request) { HttpSession session = request.getSession(); Locale locale = (Locale)session.getAttribute(org.apache.struts.action.LOCALE); if(locale == null) locale = defaultLocale; return locale; } So to switch the Locale dynamically, update the Locale object saved in the session. Thanks, Yuanbo -Original Message- From: None None [mailto:[EMAIL PROTECTED] Sent: Friday, May 14, 2004 2:16 PM To: [EMAIL PROTECTED] Subject: Sharing what I've learned: locale switching Because this might be helpful to others, and because I probably would have spent another couple of hours figuring it out on my own without the help of some people on this lsit, I wanted to give back as much as I could. So, here's a consolidated bit of info I've learned about switching language in your Struts apps... What I have is a file manager application, more or less just for me to learn Struts. I wanted to have the ability to switch languages on-the-fly. To do this, I've done the following: (1) I created two files and placed them in WEB-INF/classes. They are ofmResources_en.properties and ofmResources_de.properties (en for English, de for German). These files contain various text strings in both language. For instance, there is a lable on the screen for file uploads which is defined as follows: labels.uploadFile=Upload a file: and for the German version: labels.uploadFile=hochladen Sie eine Datei: (2) I added the following entry to web.xml, as an init parameter of the ActionServlet: init-param param-nameapplication/param-name param-valueofmResources/param-value /init-param As near as I can tell, NO entries are required in struts-config.xml. You also do NOT need to do anything for each version of the resource file. As long as they are named x_ll.properties, where x is the value of the application parameter above, and ll is a valid country code, that's all there is to it. (3) Next, I added some flag graphics to my web pages, one an American flag, one a German flag. Here is the HTML for them: form name=changeLocaleForm method=post action=changeLocale.ofm style=display:inline; input type=hidden name=languageCode table width=100% border=0 cellpadding=0 cellspacing=0trtd align=right input type=image src=img/flag_usa.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='en'; input type=image src=img/flag_germany.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='de'; /td/tr?table /form Pretty trivial stuff there. (4) Next, I created an ActionForm called ChangeLocaleActionForm as follows: package com.mycompany.ofm.actionforms; import org.apache.struts.action.*; public class ChangeLocaleActionForm extends ActionForm { private String languageCode = null; public ChangeLocaleActionForm() { languageCode = null; } public void setLanguageCode(String inLanguageCode) { languageCode = inLanguageCode; } public String getLsnguageCode() { return languageCode; } } (5) Next, I created an accompanying Action: package com.mycompany.ofm.actions; import java.util.*; import javax.servlet.http.*; import com.omnytex.ofm.actionforms.*; import org.apache.struts.*; import org.apache.struts.action.*; public class ChangeLocaleAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ChangeLocaleActionForm claf = (ChangeLocaleActionForm)form; String languageCode = claf.getLsnguageCode(); request.getSession().setAttribute(Globals.LOCALE_KEY, new Locale(languageCode)); return mapping.findForward(showPathContents); } } As it turns out as someone here informed me, there is automagically a Locale in session, created based on what is sent by the browser. So, by default on my system the value en_US is stored in session under the name
RE: Sharing what I've learned: locale switching
That's good to know too, thanks very much Larry! From: Zhang, Larry (L.) [EMAIL PROTECTED] Reply-To: Struts Users Mailing List [EMAIL PROTECTED] To: Struts Users Mailing List [EMAIL PROTECTED] Subject: RE: Sharing what I've learned: locale switching Date: Fri, 14 May 2004 14:53:06 -0400 Additionally, if you want other key(other than org.apache.struts.action.LOCALE =Globals.LOCALE_KEY) to store your locale, you can do this: session.setAttribute(myNewMeaningfulKey,myLocaleObject), then in your JSP you would use this: bean:message key=myPropertyKey locale=myNewMeaningfulKey /. This is the way the locale attribute in message tag is used in struts. Thanks. Larry Zhang -Original Message- From: Wang, Yuanbo [mailto:[EMAIL PROTECTED] Sent: Friday, May 14, 2004 2:40 PM To: Struts Users Mailing List Subject: RE: Sharing what I've learned: locale switching Thanks for sharing the information. Basically struts using the following method to decide which Locale is in the session, then load the corresponding resource bundle: protected Locale getLocale(HttpServletRequest request) { HttpSession session = request.getSession(); Locale locale = (Locale)session.getAttribute(org.apache.struts.action.LOCALE); if(locale == null) locale = defaultLocale; return locale; } So to switch the Locale dynamically, update the Locale object saved in the session. Thanks, Yuanbo -Original Message- From: None None [mailto:[EMAIL PROTECTED] Sent: Friday, May 14, 2004 2:16 PM To: [EMAIL PROTECTED] Subject: Sharing what I've learned: locale switching Because this might be helpful to others, and because I probably would have spent another couple of hours figuring it out on my own without the help of some people on this lsit, I wanted to give back as much as I could. So, here's a consolidated bit of info I've learned about switching language in your Struts apps... What I have is a file manager application, more or less just for me to learn Struts. I wanted to have the ability to switch languages on-the-fly. To do this, I've done the following: (1) I created two files and placed them in WEB-INF/classes. They are ofmResources_en.properties and ofmResources_de.properties (en for English, de for German). These files contain various text strings in both language. For instance, there is a lable on the screen for file uploads which is defined as follows: labels.uploadFile=Upload a file: and for the German version: labels.uploadFile=hochladen Sie eine Datei: (2) I added the following entry to web.xml, as an init parameter of the ActionServlet: init-param param-nameapplication/param-name param-valueofmResources/param-value /init-param As near as I can tell, NO entries are required in struts-config.xml. You also do NOT need to do anything for each version of the resource file. As long as they are named x_ll.properties, where x is the value of the application parameter above, and ll is a valid country code, that's all there is to it. (3) Next, I added some flag graphics to my web pages, one an American flag, one a German flag. Here is the HTML for them: form name=changeLocaleForm method=post action=changeLocale.ofm style=display:inline; input type=hidden name=languageCode table width=100% border=0 cellpadding=0 cellspacing=0trtd align=right input type=image src=img/flag_usa.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='en'; input type=image src=img/flag_germany.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='de'; /td/tr?table /form Pretty trivial stuff there. (4) Next, I created an ActionForm called ChangeLocaleActionForm as follows: package com.mycompany.ofm.actionforms; import org.apache.struts.action.*; public class ChangeLocaleActionForm extends ActionForm { private String languageCode = null; public ChangeLocaleActionForm() { languageCode = null; } public void setLanguageCode(String inLanguageCode) { languageCode = inLanguageCode; } public String getLsnguageCode() { return languageCode; } } (5) Next, I created an accompanying Action: package com.mycompany.ofm.actions; import java.util.*; import javax.servlet.http.*; import com.omnytex.ofm.actionforms.*; import org.apache.struts.*; import org.apache.struts.action.*; public class ChangeLocaleAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ChangeLocaleActionForm claf = (ChangeLocaleActionForm)form; String languageCode = claf.getLsnguageCode(); request.getSession().setAttribute(Globals.LOCALE_KEY, new Locale(languageCode)); return mapping.findForward(showPathContents
RE: Sharing what I've learned: locale switching
Excellent, even better! Thank you! It's becoming obvious to me that I need to spend a few hours just looking at what's available in the Action (and other) base classes. I've been just learning what I need to each step of the way, but maybe a little time reading through the javadocs would be useful. Thanks again! From: Joe Hertz [EMAIL PROTECTED] Reply-To: Struts Users Mailing List [EMAIL PROTECTED] To: 'Struts Users Mailing List' [EMAIL PROTECTED] Subject: RE: Sharing what I've learned: locale switching Date: Fri, 14 May 2004 18:09:01 -0400 Suggestion: Rather than using the GLOBALS.Locale_Key in the session, use the action.setLocale() method. This way you won't have to be changing your code if the internals change. For example, to decide between, say English and Russian, you can do this and never mess with the httpSession directly. Locale russian = new Locale(ru); Locale english = Locale.ENGLISH; if (condition == true) { this.setLocale(request, english); } else { this.setLocale(request, russian); } -Original Message- From: None None [mailto:[EMAIL PROTECTED] Sent: Friday, May 14, 2004 2:16 PM To: [EMAIL PROTECTED] Subject: Sharing what I've learned: locale switching Because this might be helpful to others, and because I probably would have spent another couple of hours figuring it out on my own without the help of some people on this lsit, I wanted to give back as much as I could. So, here's a consolidated bit of info I've learned about switching language in your Struts apps... What I have is a file manager application, more or less just for me to learn Struts. I wanted to have the ability to switch languages on-the-fly. To do this, I've done the following: (1) I created two files and placed them in WEB-INF/classes. They are ofmResources_en.properties and ofmResources_de.properties (en for English, de for German). These files contain various text strings in both language. For instance, there is a lable on the screen for file uploads which is defined as follows: labels.uploadFile=Upload a file: and for the German version: labels.uploadFile=hochladen Sie eine Datei: (2) I added the following entry to web.xml, as an init parameter of the ActionServlet: init-param param-nameapplication/param-name param-valueofmResources/param-value /init-param As near as I can tell, NO entries are required in struts-config.xml. You also do NOT need to do anything for each version of the resource file. As long as they are named x_ll.properties, where x is the value of the application parameter above, and ll is a valid country code, that's all there is to it. (3) Next, I added some flag graphics to my web pages, one an American flag, one a German flag. Here is the HTML for them: form name=changeLocaleForm method=post action=changeLocale.ofm style=display:inline; input type=hidden name=languageCode table width=100% border=0 cellpadding=0 cellspacing=0trtd align=right input type=image src=img/flag_usa.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='en'; input type=image src=img/flag_germany.gif hspace=6 border=0 onClick=changeLocaleForm.languageCode.value='de'; /td/tr?table /form Pretty trivial stuff there. (4) Next, I created an ActionForm called ChangeLocaleActionForm as follows: package com.mycompany.ofm.actionforms; import org.apache.struts.action.*; public class ChangeLocaleActionForm extends ActionForm { private String languageCode = null; public ChangeLocaleActionForm() { languageCode = null; } public void setLanguageCode(String inLanguageCode) { languageCode = inLanguageCode; } public String getLsnguageCode() { return languageCode; } } (5) Next, I created an accompanying Action: package com.mycompany.ofm.actions; import java.util.*; import javax.servlet.http.*; import com.omnytex.ofm.actionforms.*; import org.apache.struts.*; import org.apache.struts.action.*; public class ChangeLocaleAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ChangeLocaleActionForm claf = (ChangeLocaleActionForm)form; String languageCode = claf.getLsnguageCode(); request.getSession().setAttribute(Globals.LOCALE_KEY, new Locale(languageCode)); return mapping.findForward(showPathContents); } } As it turns out as someone here informed me, there is automagically a Locale in session, created based on what is sent by the browser. So, by default on my system the value en_US is stored in session under the name Globals.LOCALE_KEY. By the way