Re: Internationalization of menu items using Struts-menu with struts 2

Matt Raible <matt-T7vlz3gD7BdF/EzeuO4KJwC/[email protected]> Tue, 12 Jul 2011 11:44:13 -0600
Newsgroups gmane.comp.java.struts-menu.user
Message-ID <[email protected]>
You might need a LocaleFilter to switch the locale - or similar logic to re=
set it for JSTL. Here's one that I use with Spring:

package org.appfuse.tutorial.webapp.filter;

import org.appfuse.Constants;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;
import java.io.IOException;
import java.util.Locale;
import java.util.Map;

/**
 * Filter to wrap request with a request including user preferred locale.
 */
public class LocaleFilter extends OncePerRequestFilter {

    /**
     * This method looks for a "locale" request parameter. If it finds one,=
 it sets it as the preferred locale
     * and also configures it to work with JSTL.
     * =

     * @param request the current request
     * @param response the current response
     * @param chain the chain
     * @throws IOException when something goes wrong
     * @throws ServletException when a communication failure happens
     */
    @SuppressWarnings("unchecked")
    public void doFilterInternal(HttpServletRequest request, HttpServletRes=
ponse response,
                                 FilterChain chain)
            throws IOException, ServletException {

        String locale =3D request.getParameter("locale");
        Locale preferredLocale =3D null;

        if (locale !=3D null) {
            int indexOfUnderscore =3D locale.indexOf('_');
            if (indexOfUnderscore !=3D -1) {
                String language =3D locale.substring(0, indexOfUnderscore);
                String country =3D locale.substring(indexOfUnderscore + 1);
                preferredLocale =3D new Locale(language, country);
            } else {
                preferredLocale =3D new Locale(locale);
            }
        }

        HttpSession session =3D request.getSession(false);

        if (session !=3D null) {
            if (preferredLocale =3D=3D null) {
                preferredLocale =3D (Locale) session.getAttribute(Constants=
.PREFERRED_LOCALE_KEY);
            } else {
                session.setAttribute(Constants.PREFERRED_LOCALE_KEY, prefer=
redLocale);
                Config.set(session, Config.FMT_LOCALE, preferredLocale);
            }

            if (preferredLocale !=3D null && !(request instanceof LocaleReq=
uestWrapper)) {
                request =3D new LocaleRequestWrapper(request, preferredLoca=
le);
                LocaleContextHolder.setLocale(preferredLocale);
            }
        }

        chain.doFilter(request, response);

        // Reset thread-bound LocaleContext.
        LocaleContextHolder.setLocaleContext(null);
    }
}


On Jul 12, 2011, at 11:34 AM, max max wrote:

> if you want more precision about my code  i put you more :)
> =

> --- En date de : Mar 12.7.11, max max <[email protected]> a =E9crit :
> =

>> De: max max <[email protected]>
>> Objet: Re: [struts-menu] Internationalization of menu items using Struts=
-menu with struts 2
>> =C0: "General mailing list for Struts Menu" <[email protected]=
ceforge.net>
>> Date: Mardi 12 juillet 2011, 19h18
>> hi =

>> i want to change langage but i cannot do that i try to put
>> the =

>> <context-param>
>>        =

>> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
>>        =

>> <param-value>Global</param-value>
>>     </context-param>
>> on my Web.xml its not working too.
>> --- En date de : Mar 12.7.11, Matt Raible <matt-T7vlz3gD7BdF/EzeuO4KJwC/[email protected]>
>> a =E9crit :
>> =

>>> De: Matt Raible <matt-T7vlz3gD7BdF/EzeuO4KJwC/[email protected]>
>>> Objet: Re: [struts-menu] Internationalization of menu
>> items using Struts-menu with struts 2
>>> =C0: "General mailing list for Struts Menu" <[email protected]=
rceforge.net>
>>> Date: Mardi 12 juillet 2011, 18h50
>>> What are you using to try and switch
>>> the locale? Are you able to switch the locale for the
>> rest
>>> of your messages?
>>> =

>>> On Jul 12, 2011, at 10:46 AM, yvan khekhone wrote:
>>> =

>>>> =

>>>> hi =

>>>> i have the same configuration with struts2 and
>> Struts
>>> menu i cannot switch
>>>> language.
>>>> i show my code =

>>>> =

>>>> on my struts menu
>>>> =

>>>> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
>>>> <MenuConfig>
>>>>     <Displayers>
>>>> =

>>>> =

>>>> <Displayer name=3D"TabbedMenu"
>>>>  =

>>> =

>> type=3D"net.sf.navigator.displayer.TabbedMenuDisplayer"/>
>>>>      </Displayers>
>>>> <Menus> =

>>>> <Menu name=3D"Global.Home" title=3D"Home" =

>>> page=3D"totoAction.action"  /> =

>>>>   <Menu name=3D"Global.About" title=3D"About" =

>>> page=3D"modification.action" />
>>>>   <Menu name=3D"Global.About"
>>> title=3D"animaux"  page=3D"animal.action" />
>>>>   </Menus>
>>>> =

>>>> </MenuConfig>
>>>> =

>>>> on my jsp page.
>>>> =

>>>> <%@ page language=3D"java"
>> contentType=3D"text/html;
>>> charset=3DISO-8859-1"
>>>>     pageEncoding=3D"ISO-8859-1"%>
>>>>      <%@ taglib prefix=3D"s"
>>> uri=3D"/struts-tags" %>
>>>>      <%@ taglib prefix=3D"sx"
>>> uri=3D"/struts-dojo-tags" %>
>>>>      <%@ taglib prefix=3D"menu"
>>> uri=3D"http://struts-menu.sf.net/tag-el"%>
>>>> =

>>>> <html>
>>>>   <head>
>>>> =

>>>>   </head>
>>>>   <body>
>>>> =

>>>>     <h1 class=3D'title'>SiteMesh example
>>> site: <sitemesh:write
>>>> property=3D'title'/></h1>
>>>> <div>
>>>> <div id=3D"menu" class=3D"menu">
>>>> <menu:useMenuDisplayer name=3D"TabbedMenu"
>>>>   bundle=3D"Global" >
>>>>   <menu:displayMenu name=3D"Global.Home" />
>>>>   <menu:displayMenu name=3D"Global.About" />
>>>> </menu:useMenuDisplayer>
>>>> </div>
>>>>     <div class=3D'mainBody'>
>>>>       <sitemesh:write
>>> property=3D'body'/>
>>>>     </div>
>>>> </div>
>>>>     <div class=3D'disclaimer'>Site
>>> disclaimer. This is an example.</div>
>>>> =

>>>>   </body>
>>>> </html>
>>>> i put attribut but its not working too :/
>>>> =

>>>> =

>>>> mraible wrote:
>>>>> =

>>>>> Struts Menu uses JSTL in the "el" version to
>>> resolve messages.
>>>>> =

>>>>> On Feb 16, 2011, at 12:26 PM, Tiago Farias
>> wrote:
>>>>> =

>>>>>> So rude. Sorry I forgot to thank you for
>>> saving me! Thank you very much!
>>>>>> =3DD
>>>>>> =

>>>>>> On Wed, Feb 16, 2011 at 4:25 PM, Tiago
>> Farias
>>> <[email protected]>
>>> wrote:
>>>>>> Yup! You're absolutely right. Now I'm
>> just
>>> curious. How does that work
>>>>>> Matt?
>>>>>> =

>>>>>> =

>>>>>> On Wed, Feb 16, 2011 at 1:40 PM, Matt
>> Raible
>>> <matt-T7vlz3gD7BdF/EzeuO4KJwC/[email protected]>
>>>>>> wrote:
>>>>>> If you remove the bundle attribute and
>> add the
>>> following to your web.xml,
>>>>>> it should work. The param name is the
>> name of
>>> your properties file in
>>>>>> WEB-INF/classes, without the .properties
>>> extension.
>>>>>> =

>>>>>>     <!-- Define the basename for a
>>> resource bundle for I18N -->
>>>>>>     <context-param>
>>>>>> =

>>>>>> =

>>> =

>> <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
>>>>>>        =

>>> =

>> <param-value>ApplicationResources</param-value>
>>>>>>     </context-param>
>>>>>> =

>>>>>> =

>>>>>> On Feb 16, 2011, at 9:27 AM, Tiago
>> Farias
>>> wrote:
>>>>>> =

>>>>>>> Hello, I'm trying to make a custom
>> menu
>>> for a web app and it all worked
>>>>>>> just fine, but now I need to
>>> internationalize it using a properties
>>>>>>> file. This properties file already
>> exists
>>> and the whole app uses it. My
>>>>>>> menu-config.xml is this: =

>>>>>>> =

>>>>>>>    <Displayers>
>>>>>>>      =

>>>    <Displayer name=3D"Velocity"
>>>>>>> =

>>> =

>> type=3D"net.sf.navigator.displayer.VelocityMenuDisplayer"/>
>>>>>>>     </Displayers>
>>>>>>> =

>>>>>>>     <Menus>
>>>>>>>         <!-- =

>>> <Menu name=3D"ecfMenu" title=3D"ECFs"
>>> location=3D"?Contact">
>>>>>>>           <Item
>>> name=3D"email" title=3D"E-Mail" location=3D"?EMail"/>
>>>>>>>           <Item
>>> name=3D"phone" title=3D"Phone" location=3D"?Phone"/>
>>>>>>>        =

>>> </Menu>-->
>>>>>>> =

>>>>>>>         <Menu
>>> name=3D"mainMenu" title=3D"" >
>>>>>>>           <Item
>>> name=3D"ecfMenu" title=3D"ECFs" roles=3D"FISCO"
>>>>>>> location=3D"DisconnectedEcfsSearch"
>> />
>>>>>>>           <Item
>>> name=3D"reportsMenu" title=3D"Relat=F3rios" roles=3D"ADMIN"
>>>>>>> location=3D"AlarmReportSearch"/>
>>>>>>>           <Item
>>> name=3D"fiscoMenu" title=3D"Fisco" roles=3D"ADMIN"
>>>>>>> location=3D"UpdateFiscoForm"/>
>>>>>>>           <Item
>>> name=3D"alarmMenu" title=3D"Alarmes" roles=3D"ADMIN"
>>>>>>> location=3D"AlarmNotification"/>
>>>>>>>           <Item
>>> name=3D"userMenu" title=3D"application.header.users"
>>>>>>> roles=3D"ADMIN"
>> location=3D"UserSearch"/>
>>>>>>>           <Item
>>> name=3D"consoleMenu" title=3D"Console" roles=3D"FISCO"
>>>>>>> location=3D"ConsoleSearch"/>
>>>>>>>         </Menu>
>>>>>>> =

>>>>>>> And my jsp file is this:
>>>>>>> <%@taglib uri=3D"http://struts-menu.sf.net/tag-el" prefix=3D"menu" =
%>
>>>>>>> =

>>>>>>>        =

>>>    <menu:useMenuDisplayer
>>> permissions=3D"rolesAdapter"
>>>>>>> name=3D"Velocity"
>>> config=3D"/WEB-INF/tabs.html"
>>>>>>>            =

>>>     bundle=3D"org.apache.struts.action.MESSAGE">
>>>>>>>            =

>>>     <menu:displayMenu name=3D"mainMenu"/>
>>>>>>>            =

>>> </menu:useMenuDisplayer>
>>>>>>> =

>>>>>>> The thing is that all the menu titles
>> are
>>> ok except for the "userMenu"
>>>>>>> title that is displaying the key to
>> the
>>> properties file
>>>>>>> (application.header.users). I wanted
>> it to
>>> get the i18n file and display
>>>>>>> the value associated with this key.
>> I'm
>>> using struts2 and all the
>>>>>>> tutorials I found for this are
>> Struts1's.
>>> Need help! Don't know what
>>>>>>> else to do...
>>>>>>> =

>>>>>>> Best regards!
>>>>>>> =

>>> =

>> ------------------------------------------------------------------------=
------
>>>>>>> The ultimate all-in-one performance
>>> toolkit: Intel(R) Parallel Studio
>>>>>>> XE:
>>>>>>> Pinpoint memory and threading errors
>>> before they happen.
>>>>>>> Find and fix more than 250 security
>>> defects in the development cycle.
>>>>>>> Locate bottlenecks in serial and
>> parallel
>>> code that limit performance.
>>>>>>> http://p.sf.net/sfu/intel-dev2devfeb_______________________________=
________________
>>>>>>> struts-menu-user mailing list
>>>>>>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>>>>>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>>>>>> =

>>>>>> =

>>>>>> =

>>> =

>> ------------------------------------------------------------------------=
------
>>>>>> The ultimate all-in-one performance
>> toolkit:
>>> Intel(R) Parallel Studio XE:
>>>>>> Pinpoint memory and threading errors
>> before
>>> they happen.
>>>>>> Find and fix more than 250 security
>> defects in
>>> the development cycle.
>>>>>> Locate bottlenecks in serial and parallel
>> code
>>> that limit performance.
>>>>>> http://p.sf.net/sfu/intel-dev2devfeb
>>>>>> =

>>> _______________________________________________
>>>>>> struts-menu-user mailing list
>>>>>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>>>>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>>>>>> =

>>>>>> =

>>>>>> =

>>>>>> =

>>> =

>> ------------------------------------------------------------------------=
------
>>>>>> The ultimate all-in-one performance
>> toolkit:
>>> Intel(R) Parallel Studio XE:
>>>>>> Pinpoint memory and threading errors
>> before
>>> they happen.
>>>>>> Find and fix more than 250 security
>> defects in
>>> the development cycle.
>>>>>> Locate bottlenecks in serial and parallel
>> code
>>> that limit performance.
>>>>>> http://p.sf.net/sfu/intel-dev2devfeb________________________________=
_______________
>>>>>> struts-menu-user mailing list
>>>>>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>>>>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>>>>> =

>>>>> =

>>>>> =

>>> =

>> ------------------------------------------------------------------------=
------
>>>>> The ultimate all-in-one performance toolkit:
>>> Intel(R) Parallel Studio XE:
>>>>> Pinpoint memory and threading errors before
>> they
>>> happen.
>>>>> Find and fix more than 250 security defects
>> in the
>>> development cycle.
>>>>> Locate bottlenecks in serial and parallel
>> code
>>> that limit performance.
>>>>> http://p.sf.net/sfu/intel-dev2devfeb
>>>>> =

>> _______________________________________________
>>>>> struts-menu-user mailing list
>>>>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>>>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>>>>> =

>>>>> =

>>>> =

>>>> -- =

>>>> View this message in context: http://old.nabble.com/Internationalizati=
on-of-menu-items-using-Struts-menu-with-struts-2-tp30942065p32047366.html
>>>> Sent from the struts-menu-user mailing list
>> archive at
>>> Nabble.com.
>>>> =

>>>> =

>>>> =

>>> =

>> ------------------------------------------------------------------------=
------
>>>> AppSumo Presents a FREE Video for the
>> SourceForge
>>> Community by Eric =

>>>> Ries, the creator of the Lean Startup Methodology
>> on
>>> "Lean Startup =

>>>> Secrets Revealed." This video shows you how to
>>> validate your ideas, =

>>>> optimize your ideas and identify your business
>>> strategy.
>>>> http://p.sf.net/sfu/appsumosfdev2dev
>>>> _______________________________________________
>>>> struts-menu-user mailing list
>>>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>>> =

>>> =

>>> =

>> ------------------------------------------------------------------------=
------
>>> AppSumo Presents a FREE Video for the SourceForge
>> Community
>>> by Eric =

>>> Ries, the creator of the Lean Startup Methodology on
>> "Lean
>>> Startup =

>>> Secrets Revealed." This video shows you how to
>> validate
>>> your ideas, =

>>> optimize your ideas and identify your business
>> strategy.
>>> http://p.sf.net/sfu/appsumosfdev2dev
>>> _______________________________________________
>>> struts-menu-user mailing list
>>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>>> =

>> =

>> ------------------------------------------------------------------------=
------
>> AppSumo Presents a FREE Video for the SourceForge Community
>> by Eric =

>> Ries, the creator of the Lean Startup Methodology on "Lean
>> Startup =

>> Secrets Revealed." This video shows you how to validate
>> your ideas, =

>> optimize your ideas and identify your business strategy.
>> http://p.sf.net/sfu/appsumosfdev2dev
>> _______________________________________________
>> struts-menu-user mailing list
>> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>> https://lists.sourceforge.net/lists/listinfo/struts-menu-user
>> =

> =

> -------------------------------------------------------------------------=
-----
> AppSumo Presents a FREE Video for the SourceForge Community by Eric =

> Ries, the creator of the Lean Startup Methodology on "Lean Startup =

> Secrets Revealed." This video shows you how to validate your ideas, =

> optimize your ideas and identify your business strategy.
> http://p.sf.net/sfu/appsumosfdev2dev
> _______________________________________________
> struts-menu-user mailing list
> struts-menu-user-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/struts-menu-user


---------------------------------------------------------------------------=
---
AppSumo Presents a FREE Video for the SourceForge Community by Eric =

Ries, the creator of the Lean Startup Methodology on "Lean Startup =

Secrets Revealed." This video shows you how to validate your ideas, =

optimize your ideas and identify your business strategy.
http://p.sf.net/sfu/appsumosfdev2dev