Ticket #4030: language-local-name.diff

File language-local-name.diff, 2.7 KB (added by Antti Kaihola, 17 years ago)

patch for new template tag which provides language names in both the current language and the language itself

  • django/templatetags/i18n.py

     
    1414        context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
    1515        return ''
    1616
     17class 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
    1737class GetCurrentLanguageNode(Node):
    1838    def __init__(self, variable):
    1939        self.variable = variable
     
    95115        raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args
    96116    return GetAvailableLanguagesNode(args[2])
    97117
     118def 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
    98143def do_get_current_language(parser, token):
    99144    """
    100145    This will store the current language in the context.
     
    240285    return BlockTranslateNode(extra_context, singular, plural, countervar, counter)
    241286
    242287register.tag('get_available_languages', do_get_available_languages)
     288register.tag('get_available_language_names', do_get_available_language_names)
    243289register.tag('get_current_language', do_get_current_language)
    244290register.tag('get_current_language_bidi', do_get_current_language_bidi)
    245291register.tag('trans', do_translate)
Back to Top