| 166 | | This would jump to the same page you came from and pass the django_language |
|---|
| 167 | | variable. This is used in discovery of languages, as described in the next |
|---|
| 168 | | chapter. Of course this can only work if your rendering context contains |
|---|
| 169 | | the LANGUAGE_CODE and LANGUAGES variables. If you use DjangoContext as |
|---|
| 170 | | your rendering context, they are automatically defined. If you use the |
|---|
| 171 | | simpler Context class, you need to pass them along from request.LANGUAGE_CODE |
|---|
| 172 | | and settings.LANGUAGES themselve. |
|---|
| | 167 | This would jump to a language switcher and then be redirected back to the |
|---|
| | 168 | page it came from. The switcher page will just store the language in either |
|---|
| | 169 | the users session or a special cookie for use in the language discovery. |
|---|
| | 170 | |
|---|
| | 171 | You can leave out the "next" field. In that case the /i18n/setlang/ view |
|---|
| | 172 | will redirect to the URL that is in the "Referer" header. Please keep in mind |
|---|
| | 173 | that people might suppress that header, so in those cases there won't be a |
|---|
| | 174 | URL to redirect to - in those cases the view function will redirect to "/" |
|---|
| | 175 | as a fallback. |
|---|
| | 176 | |
|---|
| | 177 | The /i18n/setlang/ url can be set up by including the following in your |
|---|
| | 178 | ROOT_URLCONF:: |
|---|
| | 179 | |
|---|
| | 180 | urlpatterns = patterns('', |
|---|
| | 181 | (r'^i18n/', include('django.conf.urls.i18n'), |
|---|
| | 182 | ) |
|---|
| | 183 | |
|---|
| | 184 | This adds the setlang handler to your urlspace and hooks up a standard view |
|---|
| | 185 | function to it. |
|---|
| 209 | | The first thing the LocaleMiddleware does, is looking at the GET and POST |
|---|
| 210 | | data. If it finds a django_language variable there (if both have one, the |
|---|
| 211 | | one from GET will succeed), this language will be stored in the session |
|---|
| 212 | | (if sessions are used in your site) or in a cookie (if you don't use |
|---|
| 213 | | sessions). And it will be the selected langauge, of course. That way |
|---|
| 214 | | you can provide simple language switches by creating either a link with |
|---|
| 215 | | language (linked from country flags for example) or by giving the user some |
|---|
| 216 | | language selector like in the previous chapter. |
|---|
| 217 | | |
|---|
| 218 | | If neither GET nor POST have django_language, the middleware looks at the |
|---|
| 219 | | session data for the user. If that carries a key django_language, it's contents |
|---|
| 220 | | will be used as the language code. If the session doesn't contain a language |
|---|
| 221 | | setting, the middleware will look at the cookies for a django_language cookie. |
|---|
| 222 | | If that is found, it gives the language code. |
|---|
| | 222 | The first thing the LocaleMiddleware does, it looks at the session data for the |
|---|
| | 223 | user. If that carries a key django_language, it's contents will be used as the |
|---|
| | 224 | language code. If the session doesn't contain a language setting, the |
|---|
| | 225 | middleware will look at the cookies for a django_language cookie. If that is |
|---|
| | 226 | found, it gives the language code. |
|---|