| | 119 | Sometimes you might want to give the user a selection of languages. This |
|---|
| | 120 | can be done by accessing the LANGUAGES variable of a DjangoContext. This |
|---|
| | 121 | is a list of tuples where the first element is the language code and the |
|---|
| | 122 | second element is the language name (in that language). The code might |
|---|
| | 123 | look like this:: |
|---|
| | 124 | |
|---|
| | 125 | <form method="POST"> |
|---|
| | 126 | <select name="django_language"> |
|---|
| | 127 | {% for lang in LANGUAGES %} |
|---|
| | 128 | <option value="{{ lang.0 }}">{{ lang.1 }}</option> |
|---|
| | 129 | {% endfor %} |
|---|
| | 130 | </select> |
|---|
| | 131 | </form> |
|---|
| | 132 | |
|---|
| | 133 | This would jump to the same page you came from and pass the django_language |
|---|
| | 134 | variable. This is used in discovery of languages, as described in the next |
|---|
| | 135 | chapter. |
|---|
| | 136 | |
|---|
| 151 | | The LocaleMiddleware first looks at the session data for the user. If that |
|---|
| 152 | | carries a key django_language, it's contents will be used as the language |
|---|
| 153 | | code. If the session doesn't contain a language setting, the middleware will |
|---|
| 154 | | look at the cookies for a django_language cookie. If that is found, it gives |
|---|
| 155 | | the language code. If neither the session nor the cookie carry a language code, |
|---|
| 156 | | the middleware will look at the HTTP header Accept-Language. This header is |
|---|
| 157 | | sent by your browser and tells the server what languages you prefer. Languages |
|---|
| 158 | | are ordered by some choice value - the higher, the more you prefer the language. |
|---|
| | 169 | The first thing the LocaleMiddleware does, is looking at the GET and POST |
|---|
| | 170 | data. If it finds a django_language variable there (if both have one, the |
|---|
| | 171 | one from GET will succeed), this language will be stored in the session |
|---|
| | 172 | (if sessions are used in your site) or in a cookie (if you don't use |
|---|
| | 173 | sessions). And it will be the selected langauge, of course. That way |
|---|
| | 174 | you can provide simple language switches by creating either a link with |
|---|
| | 175 | language (linked from country flags for example) or by giving the user some |
|---|
| | 176 | language selector like in the previous chapter. |
|---|
| | 177 | |
|---|
| | 178 | If neither GET nor POST have django_language, the middleware looks at the |
|---|
| | 179 | session data for the user. If that carries a key django_language, it's contents |
|---|
| | 180 | will be used as the language code. If the session doesn't contain a language |
|---|
| | 181 | setting, the middleware will look at the cookies for a django_language cookie. |
|---|
| | 182 | If that is found, it gives the language code. If neither the session nor the |
|---|
| | 183 | cookie carry a language code, the middleware will look at the HTTP header |
|---|
| | 184 | Accept-Language. This header is sent by your browser and tells the server what |
|---|
| | 185 | languages you prefer. Languages are ordered by some choice value - the higher, |
|---|
| | 186 | the more you prefer the language. |
|---|