| | 17 | class GetAvailableLanguageNamesNode(Node): |
|---|
| | 18 | def __init__(self, variable): |
|---|
| | 19 | self.variable = variable |
|---|
| | 20 | |
|---|
| | 21 | def render(self, context): |
|---|
| | 22 | from django.conf import settings |
|---|
| | 23 | original_language = translation.get_language() |
|---|
| | 24 | context[self.variable] = [] |
|---|
| | 25 | for code, name in settings.LANGUAGES: |
|---|
| | 26 | try: |
|---|
| | 27 | translation.activate(code) |
|---|
| | 28 | except IndexError: |
|---|
| | 29 | translation.activate(original_language) |
|---|
| | 30 | context[self.variable].append({ |
|---|
| | 31 | 'language_code': code, |
|---|
| | 32 | 'name': name, |
|---|
| | 33 | 'name_local': translation.ugettext(name)}) |
|---|
| | 34 | translation.activate(original_language) |
|---|
| | 35 | return '' |
|---|
| | 36 | |
|---|
| | 118 | def do_get_available_language_names(parser, token): |
|---|
| | 119 | """ |
|---|
| | 120 | This will store in the context a list of available |
|---|
| | 121 | languages and supply each item with the name of the |
|---|
| | 122 | language both in the current language and the |
|---|
| | 123 | language itself. |
|---|
| | 124 | |
|---|
| | 125 | Usage:: |
|---|
| | 126 | |
|---|
| | 127 | {% get_available_language_names as languages %} |
|---|
| | 128 | {% for language in languages %} |
|---|
| | 129 | ... |
|---|
| | 130 | {% endfor %} |
|---|
| | 131 | |
|---|
| | 132 | This will convert the LANGUAGES setting from your |
|---|
| | 133 | setting file (or the default settings) to a list of |
|---|
| | 134 | dicts and put it into the named variable. Each item |
|---|
| | 135 | will have the keys ``language_code``, ``name`` and |
|---|
| | 136 | ``name_local``. |
|---|
| | 137 | """ |
|---|
| | 138 | args = token.contents.split() |
|---|
| | 139 | if len(args) != 3 or args[1] != 'as': |
|---|
| | 140 | raise TemplateSyntaxError, "'get_available_language_names' requires 'as variable' (got %r)" % args |
|---|
| | 141 | return GetAvailableLanguageNamesNode(args[2]) |
|---|
| | 142 | |
|---|