When exactly is the locale changed when using the lang parameter?

View: New views
7 Messages — Rating Filter:   Alert me  

When exactly is the locale changed when using the lang parameter?

by Jean-Noël Rivasseau-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello, on my web-application I would like to setup a custom URL
mapping that contains the locale to be used in the URL. Eg,
www.example.com/en/, www.example.com/fr for French, etc.

That way the URL contains the locale information which is nice for
bookmarking etc.

I made up the following URL mapping:

          "/$lang?/about"
                          {
               
                  controller= "main"
                  constraints
                        {
                                lang(matches:/[a-z]{2}/)
                        }
                  action = "about"
               
         }

Unfortunately, it is not working, the locale is not changed (on
www.example.com/fr/about/ ). If I manually specify the ?lang parameter
at the end of the URL it works though. This had led me to believe that
the "locale switching", based on the lang parameter, is maybe ran
before the URL mapping, or it does not use the lang parameter as set
by the URL mapping code in params.

How should I proceed then to achieve what I want? In a Filter maybe?

On a sidenote, the Grails guide says that forcing the locale via the
lang parameter sets up a cookie that remembers the choice. I sniffed
requests with Firebug and found no trace of such a cookie, so I guess
it is stored within the session? Can someone confirm?

As I will be essentially forcing the lang parameter every time with my
approach, I hope that the locale switching code is not heavy - does
someone know about this?

Thanks

Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: When exactly is the locale changed when using the lang parameter?

by Graeme Rocher-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The lang switching relies on the Spring infrastructure and doesn't
read the Grails params object, but really it should. Please raise a
feature request in jIRA

Cheers

On Wed, May 14, 2008 at 10:08 PM, Jean-Noël Rivasseau <elvanor@...> wrote:

> Hello, on my web-application I would like to setup a custom URL
>  mapping that contains the locale to be used in the URL. Eg,
>  www.example.com/en/, www.example.com/fr for French, etc.
>
>  That way the URL contains the locale information which is nice for
>  bookmarking etc.
>
>  I made up the following URL mapping:
>
>           "/$lang?/about"
>                           {
>
>                   controller= "main"
>                   constraints
>                         {
>                                 lang(matches:/[a-z]{2}/)
>                         }
>                   action = "about"
>
>          }
>
>  Unfortunately, it is not working, the locale is not changed (on
>  www.example.com/fr/about/ ). If I manually specify the ?lang parameter
>  at the end of the URL it works though. This had led me to believe that
>  the "locale switching", based on the lang parameter, is maybe ran
>  before the URL mapping, or it does not use the lang parameter as set
>  by the URL mapping code in params.
>
>  How should I proceed then to achieve what I want? In a Filter maybe?
>
>  On a sidenote, the Grails guide says that forcing the locale via the
>  lang parameter sets up a cookie that remembers the choice. I sniffed
>  requests with Firebug and found no trace of such a cookie, so I guess
>  it is stored within the session? Can someone confirm?
>
>  As I will be essentially forcing the lang parameter every time with my
>  approach, I hope that the locale switching code is not heavy - does
>  someone know about this?
>
>  Thanks
>
>  Jean-Noel
>
>  ---------------------------------------------------------------------
>  To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>



--
Graeme Rocher
Grails Project Lead
G2One, Inc. Chief Technology Officer
http://www.g2one.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: When exactly is the locale changed when using the lang parameter?

by Jean-Noël Rivasseau-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Done,

http://jira.codehaus.org/browse/GRAILS-2951

I've looked at the Spring docs to understand how this worked, and
until the JIRA is fixed I came with a simple workaround in a filter
applying to all controllers:

                                if (params["lang"])
                                {
                                        def localeResolver = RequestContextUtils.getLocaleResolver(request);
                                        def locale = new Locale(params["lang"]);
                                        localeResolver.setLocale(request, response, locale);
                                }

Can you confirm, Graeme, that the LocaleResolver implementation used
by Grails is the SessionLocaleResolver? (not very clear from the
docs).

Also, while I was playing with the URL mappings I was wondering a few things:

In particular, does the constraints allow the URL mapper resolver to
discriminate on a optional parameter?

Eg, for the mapping:

 "/$lang?/$controller/$action?"
{
   constraints
  {
      lang(matches:/[a-z]{2}/)
   }
}

will the code checks if I have a 2 letter string after the first
slash, in that case fill the $lang parameter, and in the other case
assume the $lang parameter is not present? I am not sure if such
advanced conditional mappings currently work with Grails.

Thanks

Jean-Noel

On Wed, May 14, 2008 at 11:22 PM, Graeme Rocher <graeme@...> wrote:

> The lang switching relies on the Spring infrastructure and doesn't
> read the Grails params object, but really it should. Please raise a
> feature request in jIRA
>
> Cheers
>
> On Wed, May 14, 2008 at 10:08 PM, Jean-Noël Rivasseau <elvanor@...> wrote:
>> Hello, on my web-application I would like to setup a custom URL
>>  mapping that contains the locale to be used in the URL. Eg,
>>  www.example.com/en/, www.example.com/fr for French, etc.
>>
>>  That way the URL contains the locale information which is nice for
>>  bookmarking etc.
>>
>>  I made up the following URL mapping:
>>
>>           "/$lang?/about"
>>                           {
>>
>>                   controller= "main"
>>                   constraints
>>                         {
>>                                 lang(matches:/[a-z]{2}/)
>>                         }
>>                   action = "about"
>>
>>          }
>>
>>  Unfortunately, it is not working, the locale is not changed (on
>>  www.example.com/fr/about/ ). If I manually specify the ?lang parameter
>>  at the end of the URL it works though. This had led me to believe that
>>  the "locale switching", based on the lang parameter, is maybe ran
>>  before the URL mapping, or it does not use the lang parameter as set
>>  by the URL mapping code in params.
>>
>>  How should I proceed then to achieve what I want? In a Filter maybe?
>>
>>  On a sidenote, the Grails guide says that forcing the locale via the
>>  lang parameter sets up a cookie that remembers the choice. I sniffed
>>  requests with Firebug and found no trace of such a cookie, so I guess
>>  it is stored within the session? Can someone confirm?
>>
>>  As I will be essentially forcing the lang parameter every time with my
>>  approach, I hope that the locale switching code is not heavy - does
>>  someone know about this?
>>
>>  Thanks
>>
>>  Jean-Noel
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

Re: When exactly is the locale changed when using the lang parameter?

by Jean-Noël Rivasseau-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Done,

http://jira.codehaus.org/browse/GRAILS-2951

I've looked at the Spring docs to understand how this worked, and
until the JIRA is fixed I came with a simple workaround in a filter
applying to all controllers:

                               if (params["lang"])
                               {
                                       def localeResolver =
RequestContextUtils.getLocaleResolver(request);
                                       def locale = new Locale(params["lang"]);

localeResolver.setLocale(request, response, locale);
                               }

Can you confirm, Graeme, that the LocaleResolver implementation used
by Grails is the SessionLocaleResolver? (not very clear from the
docs).

Also, while I was playing with the URL mappings I was wondering a few things:

In particular, does the constraints allow the URL mapper resolver to
discriminate on a optional parameter?

Eg, for the mapping:

 "/$lang?/$controller/$action?"
{
  constraints
 {
     lang(matches:/[a-z]{2}/)
  }
}

will the code checks if I have a 2 letter string after the first
slash, in that case fill the $lang parameter, and in the other case
assume the $lang parameter is not present? I am not sure if such
advanced conditional mappings currently work with Grails.

Thanks

Jean-Noel

On Wed, May 14, 2008 at 11:22 PM, Graeme Rocher <graeme@...> wrote:

> The lang switching relies on the Spring infrastructure and doesn't
> read the Grails params object, but really it should. Please raise a
> feature request in jIRA
>
> Cheers
>
> On Wed, May 14, 2008 at 10:08 PM, Jean-Noël Rivasseau <elvanor@...> wrote:
>> Hello, on my web-application I would like to setup a custom URL
>>  mapping that contains the locale to be used in the URL. Eg,
>>  www.example.com/en/, www.example.com/fr for French, etc.
>>
>>  That way the URL contains the locale information which is nice for
>>  bookmarking etc.
>>
>>  I made up the following URL mapping:
>>
>>           "/$lang?/about"
>>                           {
>>
>>                   controller= "main"
>>                   constraints
>>                         {
>>                                 lang(matches:/[a-z]{2}/)
>>                         }
>>                   action = "about"
>>
>>          }
>>
>>  Unfortunately, it is not working, the locale is not changed (on
>>  www.example.com/fr/about/ ). If I manually specify the ?lang parameter
>>  at the end of the URL it works though. This had led me to believe that
>>  the "locale switching", based on the lang parameter, is maybe ran
>>  before the URL mapping, or it does not use the lang parameter as set
>>  by the URL mapping code in params.
>>
>>  How should I proceed then to achieve what I want? In a Filter maybe?
>>
>>  On a sidenote, the Grails guide says that forcing the locale via the
>>  lang parameter sets up a cookie that remembers the choice. I sniffed
>>  requests with Firebug and found no trace of such a cookie, so I guess
>>  it is stored within the session? Can someone confirm?
>>
>>  As I will be essentially forcing the lang parameter every time with my
>>  approach, I hope that the locale switching code is not heavy - does
>>  someone know about this?
>>
>>  Thanks
>>
>>  Jean-Noel
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>>
>
>
>
> --
> Graeme Rocher
> Grails Project Lead
> G2One, Inc. Chief Technology Officer
> http://www.g2one.com
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>

Re: When exactly is the locale changed when using the lang parameter?

by Peter Ledbrook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Can you confirm, Graeme, that the LocaleResolver implementation used
> by Grails is the SessionLocaleResolver? (not very clear from the
> docs).

My name isn't Graeme, but I can confirm that SessionLocaleResolver is
used. You can find out what Spring beans are used by looking inside
I18nGrailsPlugin.

> In particular, does the constraints allow the URL mapper resolver to
> discriminate on a optional parameter?
>
> Eg, for the mapping:
>
>  "/$lang?/$controller/$action?"
> {
>   constraints
>  {
>      lang(matches:/[a-z]{2}/)
>   }
> }
>
> will the code checks if I have a 2 letter string after the first
> slash, in that case fill the $lang parameter, and in the other case
> assume the $lang parameter is not present? I am not sure if such
> advanced conditional mappings currently work with Grails.

I'm afraid I don't know, but I don't think you can even have an option
parameter appearing ahead of a required one.

Regards,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: When exactly is the locale changed when using the lang parameter?

by Jean-Noël Rivasseau-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> My name isn't Graeme, but I can confirm that SessionLocaleResolver is
> used. You can find out what Spring beans are used by looking inside
> I18nGrailsPlugin.

OK. Well, Peter, you're our second Graeme on the MLs :)

>
>> In particular, does the constraints allow the URL mapper resolver to
>> discriminate on a optional parameter?
>>
>> Eg, for the mapping:
>>
>>  "/$lang?/$controller/$action?"
>> {
>>   constraints
>>  {
>>      lang(matches:/[a-z]{2}/)
>>   }
>> }
>>
>> will the code checks if I have a 2 letter string after the first
>> slash, in that case fill the $lang parameter, and in the other case
>> assume the $lang parameter is not present? I am not sure if such
>> advanced conditional mappings currently work with Grails.
>
> I'm afraid I don't know, but I don't think you can even have an option
> parameter appearing ahead of a required one.

I came to the same conclusion. However apparently if you put two
mappins, one with the lang parameter and the other without, it works
fine. So it's ok for me

Thanks Peter,

Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: When exactly is the locale changed when using the lang parameter?

by Jean-Noël Rivasseau-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> My name isn't Graeme, but I can confirm that SessionLocaleResolver is
> used. You can find out what Spring beans are used by looking inside
> I18nGrailsPlugin.

OK. Well, Peter, you're our second Graeme on the MLs :)

>
>> In particular, does the constraints allow the URL mapper resolver to
>> discriminate on a optional parameter?
>>
>> Eg, for the mapping:
>>
>>  "/$lang?/$controller/$action?"
>> {
>>   constraints
>>  {
>>      lang(matches:/[a-z]{2}/)
>>   }
>> }
>>
>> will the code checks if I have a 2 letter string after the first
>> slash, in that case fill the $lang parameter, and in the other case
>> assume the $lang parameter is not present? I am not sure if such
>> advanced conditional mappings currently work with Grails.
>
> I'm afraid I don't know, but I don't think you can even have an option
> parameter appearing ahead of a required one.

I came to the same conclusion. However apparently if you put two
mappins, one with the lang parameter and the other without, it works
fine. So it's ok for me

Thanks Peter,

Jean-Noel

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email