Django

Code

Changeset 770

Show
Ignore:
Timestamp:
10/03/05 07:33:50 (3 years ago)
Author:
hugo
Message:

i18n: updated the documentation for the new features

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/docs/translation.txt

    r760 r770  
    117117This is much shorter, but won't allow you to use gettext_noop or ngettext. 
    118118 
     119Sometimes you might want to give the user a selection of languages. This 
     120can be done by accessing the LANGUAGES variable of a DjangoContext. This 
     121is a list of tuples where the first element is the language code and the 
     122second element is the language name (in that language). The code might 
     123look 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 
     133This would jump to the same page you came from and pass the django_language 
     134variable. This is used in discovery of languages, as described in the next 
     135chapter. 
     136 
    119137How the Language is Discovered 
    120138============================== 
     
    149167the request - every user can have her own settings. 
    150168 
    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. 
     169The first thing the LocaleMiddleware does, is looking at the GET and POST 
     170data. If it finds a django_language variable there (if both have one, the 
     171one 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 
     173sessions). And it will be the selected langauge, of course. That way 
     174you can provide simple language switches by creating either a link with 
     175language (linked from country flags for example) or by giving the user some 
     176language selector like in the previous chapter. 
     177 
     178If neither GET nor POST have django_language, the middleware looks at the 
     179session data for the user. If that carries a key django_language, it's contents 
     180will be used as the language code. If the session doesn't contain a language 
     181setting, the middleware will look at the cookies for a django_language cookie. 
     182If that is found, it gives the language code. If neither the session nor the 
     183cookie carry a language code, the middleware will look at the HTTP header 
     184Accept-Language. This header is sent by your browser and tells the server what 
     185languages you prefer. Languages are ordered by some choice value - the higher, 
     186the more you prefer the language. 
    159187 
    160188So the middleware will iterate over that header, ordered by the preference 
     
    168196you don't use the middlware) the language is in settings.LANGUAGE_CODE, while 
    169197with dynamic translations (when you do use the middleware) it's in 
    170 request.LANGUAGE_CODE. 
     198request.LANGUAGE_CODE. And if your application builds on DjangoContext 
     199instances for template rendering, it will be automatically be available 
     200as LANGUAGE_CODE in your template (with automatic determination where to 
     201pull it from). 
    171202 
    172203Creating Language Files 
     
    220251That's it. You made your first translation. If you now configure your browser 
    221252to request your language, it show up in the admin for example. 
     253 
     254Another thing: please give us the name of your newly created language in that 
     255native language - so we can add it to the global list of available languages 
     256that is mirrored in settings.LANGUAGES (and the DjangoContext variable 
     257LANGUAGES in templates). 
    222258 
    223259Using Translations in Your Own Projects