[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 693
  • Last Modified:

Struts and Internationalized site

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!!
0
sahrom
Asked:
sahrom
  • 4
  • 2
  • 2
  • +4
1 Solution
 
CEHJCommented:
You could ask them in the first place and then store their choice in a cookie or a server-side 'db'
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can give various choices in HREFs or radio-buttons and store the selected choice in the session.
0
 
gksinghietCommented:
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Peter KwanCommented:
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, ApplicationResources_en.properties and ApplicationResouces_fr.properties
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:
http://www.systemmobile.com/articles/strutsMessageResources.html
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
0
 
sdbanksCommented:
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("?");
                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) {
                  nl.printStackTrace();
                  throw nl;
              } catch (Exception ex) {
                  ex.printStackTrace();
                  throw ex;
                 
              }
               response.sendRedirect(sourceURL);

        } 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);

    }
}

0
 
Mayank SAssociate Director - Product EngineeringCommented:
sdbanks, posting full working code is against EE rules until the questioner posts something.
0
 
sahromAuthor Commented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> what are you saying  mayankeagle?

That on EE, we are not supposed to provide full-blown code because it is against the membership agreement.
0
 
sdbanksCommented:
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="/switchlanguage.do">&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 switchLanguage.do into your Struts-config file.  We also put

resource.properties
label.switch.language=Francais

and

resource_fr.properties
label.switch.language=English

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






0
 
sahromAuthor Commented:
thanks for providing that information.  I did not know that was a rule either.  
It was a big help
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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 ;) ?
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now