minou
minou

Reputation: 16563

webapp2 routes for locale in URL

The webapp2 i18n documentation has an example of specifying the locale in a parameter, e.g.:

www.example.com?locale=en_US

but Google search console says that this is not recommended.

I'd like to instead put the locale in the URL like this:

www.example.com
www.example.com/about
www.example.com/contact
www.example.com/fr
www.example.com/fr/about
www.example.com/fr/contact

What is a good way of setting up your webapp2 routes and passing the locale as a parameter to your handlers?

Upvotes: 3

Views: 94

Answers (2)

minou
minou

Reputation: 16563

I ended up using a variation of Dj Dac's answer. I replace each route with two routes like this:

Route(r'/page', views.PageHandler),
Route(r'/<locale:\w\w>/page', views.PageHandler),

The route without the locale gives the default and is also used for the x-default hreflang tag.

Upvotes: 1

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26637

If you want to try the way I do it: Set a regex (app = webapp2.WSGIApplication([('/([^/]+)/?([^/]*)', RegionSearch)], config=settings.w2config, debug=settings.DEBUG) and then "pass on" the parameter to the template where you can set the locate arbitrarily for many different locales, timezones and currencies e.g.

   {% if request.... == "..." %} # depend on values in the request
        {% set currency = "SEK" %}
        {% set format = "sv_SE" %}
        {% set timezoneinfo = 'Europe/Stockholm' %}
        {% set locale = "se" %}    
    {% endif %}

Upvotes: 1

Related Questions