Django

Code

Changeset 1049

Show
Ignore:
Timestamp:
11/02/05 11:48:49 (3 years ago)
Author:
hugo
Message:

i18n: updated documentation and unittests for the new get_available_languages tag and the new switching view

Files:

Legend:

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

    r955 r1049  
    156156look like this:: 
    157157 
    158     <form method="POST"> 
    159     <select name="django_language"> 
     158    <form action="/i18n/setlang/" method="POST"> 
     159    <input name="next" type="hidden" value="http://server.doma.in/path/to/go/to/after/switch/"/> 
     160    <select name="language"> 
    160161    {% for lang in LANGUAGES %} 
    161162    <option value="{{ lang.0 }}">{{ lang.1 }}</option> 
     
    164165    </form> 
    165166 
    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. 
     167This would jump to a language switcher and then be redirected back to the 
     168page it came from. The switcher page will just store the language in either 
     169the users session or a special cookie for use in the language discovery. 
     170 
     171You can leave out the "next" field. In that case the /i18n/setlang/ view 
     172will redirect to the URL that is in the "Referer" header. Please keep in mind 
     173that people might suppress that header, so in those cases there won't be a 
     174URL to redirect to - in those cases the view function will redirect to "/" 
     175as a fallback. 
     176 
     177The /i18n/setlang/ url can be set up by including the following in your 
     178ROOT_URLCONF:: 
     179 
     180    urlpatterns = patterns('', 
     181        (r'^i18n/', include('django.conf.urls.i18n'), 
     182    ) 
     183 
     184This adds the setlang handler to your urlspace and hooks up a standard view 
     185function to it. 
    173186 
    174187How the Language is Discovered 
     
    207220the request - every user can have her own settings. 
    208221 
    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. 
     222The first thing the LocaleMiddleware does, it looks at the session data for the 
     223user. If that carries a key django_language, it's contents will be used as the 
     224language code. If the session doesn't contain a language setting, the 
     225middleware will look at the cookies for a django_language cookie.  If that is 
     226found, it gives the language code. 
    223227 
    224228The format for the explicit django_language parameters is allways the 
  • django/branches/i18n/tests/othertests/templates.py

    r874 r1049  
    259259    # translation of a variable with a non-translated filter 
    260260    'i18n14': ('{{ bool|yesno:"ja,nein" }}', {'bool': True}, 'ja'), 
     261 
     262    # usage of the get_available_languages tag 
     263    'i18n15': ('{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), 
    261264} 
    262265 
     
    278281    for name, vals in tests: 
    279282        if 'LANGUAGE_CODE' in vals[1]: 
    280             activate('*', vals[1]['LANGUAGE_CODE']) 
     283            activate(vals[1]['LANGUAGE_CODE']) 
    281284        try: 
    282285            output = loader.get_template(name).render(template.Context(vals[1]))