Struts and Internationalized site

Posted on 2006-05-01
Last Modified: 2007-11-27
I am setting up struts site to work in English and French.
I have the properties files set up and it works when I check with the users browsers settings,
Now I need to let the user select the language.  Will this work on a line - href, where they can select to go to either language.  I have not seen a example how to do this.  Can anyone point me in the direction, or give a example!!
Question by:sahrom
    LVL 86

    Expert Comment

    You could ask them in the first place and then store their choice in a cookie or a server-side 'db'
    LVL 30

    Expert Comment

    You can give various choices in HREFs or radio-buttons and store the selected choice in the session.
    LVL 5

    Expert Comment

    You need to set the Locale for your application when the language is changed by clicking on the link.
    If you are using JSTL then you can set the locale using fmt:setLocale tag.
    LVL 16

    Expert Comment

    by:Peter Kwan
    An example approach:

    1. Create a hidden form to store your existing query string parameters and an additional field called "lang"
    2. When the language link is clicked, set the "lang" field to that language and then submit the hidden form
    3. Set your language specific messages in message resources, and
    4. In your action, you can either do the following:

    a) Set all your required message specific fields in request parameters, and get the request parameters from jsp:
    MessageResouces mr = getResources(request, <lang>);
    request.setParameter("abc", mr.getMessage(<key>);

    In your jsp:

    <%= request.getParameter("abc");

    b) If you are using JSTL, you may set "bundle" attribute in JSP tag, an example is here:
    LVL 23

    Expert Comment

    LVL 1

    Expert Comment

    We use the following code to switch the users language dynamically between French and English.

    public class SwitchLanguageAction extends Action {

        public ActionForward execute(
           ActionMapping mapping,
           ActionForm form,
           HttpServletRequest request,
           HttpServletResponse response)
           throws Exception {

            ActionErrors errors = new ActionErrors();
            ActionForward forward = new ActionForward();

            try {
                Locale locale = null;
                String language = null;
                String new_language = null;
                String country  = "CA";
                HttpSession   session  = request.getSession( true );
                //  Step one is to set up the users session to indicate the new language

                Locale currentLocale = (Locale) session.getAttribute(Globals.LOCALE_KEY);
                if (currentLocale.getLanguage().equals("en")) {
          locale = Locale.CANADA_FRENCH;
                    new_language = "fr";
                } else {
                    locale = locale = Locale.CANADA;
                    new_language = "en";
                //Locale newLocale = new Locale( new_language, country );

                if (session != null) {
                    session.setAttribute("locale", locale);
                    session.setAttribute( Globals.LOCALE_KEY, locale );

                //  Step two is to determine the url user was at before selecting switch language
                 String sourceURL = null;
                 StringBuffer url = new StringBuffer(request.getHeader("referer"));
                 String queryString = request.getQueryString();
                 if( (queryString!=null) && (queryString.length()>0) ) {
                    url.append( queryString );
                 try {
                      sourceURL = url.toString();
    //                check if parameters language=fr or language=en exist
                      int x = sourceURL.indexOf("language=en");
                      if (x == -1) {
                        x = sourceURL.indexOf("language=fr");
    //                remove the language=fr or language=en if they exist
                      if (x > -1) {
                          sourceURL = sourceURL.substring(0,x) + sourceURL.substring(x + 11);

                  } catch (NullPointerException nl) {
                      throw nl;
                  } catch (Exception ex) {
                      throw ex;

            } catch (Exception e) {

                // Report the error using the appropriate name and ID.
                errors.add("name", new ActionError("id"));


            // If a message is required, save the specified key(s)
            // into the request for use by the <struts:errors> tag.

            if (!errors.isEmpty()) {
                saveErrors(request, errors);

                // Forward control to the appropriate 'failure' URI (change name as desired)
                //  forward = mapping.findForward("failure");

            } else {

                // Forward control to the appropriate 'success' URI (change name as desired)
                // The "success" does not do anything.  It would normally cause a blank
                // page to display, but the code "response.sendRedirect(sourceURL);" previously used will
                // cause the browser to load the users current page again.
                forward = mapping.findForward("success");


            // Finish with
            return (forward);


    LVL 30

    Expert Comment

    sdbanks, posting full working code is against EE rules until the questioner posts something.

    Author Comment

    what are you saying  mayankeagle?

    The code that sdbanks  provided is what I was looking for - it is more helpful.

    I am just trying to figure it out now?

    sdbanks what would I use for the link - or toggle button - I am also using tiles, not sure if that matters, I also have a few forms or actions

    thank you
    LVL 30

    Expert Comment

    >> what are you saying  mayankeagle?

    That on EE, we are not supposed to provide full-blown code because it is against the membership agreement.
    LVL 1

    Accepted Solution

    First.  Sorry for breaking the rules.  I have not been on here for several years and came looking for something else and just happend to notice this question.

    What we do is have the following link as :

    <html:link page="/">&nbsp;<bean:message key="label.switch.language"/></html:link>

    The link is in a tile that is the header for every page.  You will need to put the into your Struts-config file.  We also put


    In the end, users can switch languages from any page in the web site.  


    Author Comment

    thanks for providing that information.  I did not know that was a rule either.  
    It was a big help
    LVL 30

    Expert Comment

    >> I did not know that was a rule either.

    Why do you think nobody else provided full-code earlier but just tried to give an algo or directions ;) ?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    NotAlone Challenge 20 65
    sumHeights  challenge 17 50
    mergeTwo  challenge 13 53
    Receive file in Servlet 1 25
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    759 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