Ticket #4030: make-language-names.diff

File make-language-names.diff, 3.7 KB (added by Antti Kaihola, 17 years ago)

implements mtredinnick's suggestion: a script for generating a local language name module and a fixed template tag

  • 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.language_names import language_names
     23        context[self.variable] = language_names
     24        return ''
     25
    1726class GetCurrentLanguageNode(Node):
    1827    def __init__(self, variable):
    1928        self.variable = variable
     
    95104        raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args
    96105    return GetAvailableLanguagesNode(args[2])
    97106
     107def do_get_available_language_names(parser, token):
     108    """
     109    This will store in the context a list of available
     110    languages and supply each item with the name of the
     111    language both in the current language and the
     112    language itself.
     113
     114    Usage::
     115
     116        {% get_available_language_names as languages %}
     117        {% for language in languages %}
     118        ...
     119        {% endfor %}
     120
     121    This will convert the LANGUAGES setting from your
     122    setting file (or the default settings) to a list of
     123    dicts and put it into the named variable.  Each item
     124    will have the keys ``language_code``, ``name`` and
     125    ``name_local``.
     126    """
     127    args = token.contents.split()
     128    if len(args) != 3 or args[1] != 'as':
     129        raise TemplateSyntaxError, "'get_available_language_names' requires 'as variable' (got %r)" % args
     130    return GetAvailableLanguageNamesNode(args[2])
     131
    98132def do_get_current_language(parser, token):
    99133    """
    100134    This will store the current language in the context.
     
    240274    return BlockTranslateNode(extra_context, singular, plural, countervar, counter)
    241275
    242276register.tag('get_available_languages', do_get_available_languages)
     277register.tag('get_available_language_names', do_get_available_language_names)
    243278register.tag('get_current_language', do_get_current_language)
    244279register.tag('get_current_language_bidi', do_get_current_language_bidi)
    245280register.tag('trans', do_translate)
  • django/bin/make-language-names.py

     
     1#!/usr/bin/env python
     2
     3LANGUAGE_NAMES_PY_FILE = 'django/conf/language_names.py'
     4
     5# Need to ensure that the i18n framework is enabled
     6from django.conf import settings
     7settings.configure(USE_I18N = True)
     8
     9from django.utils import translation
     10
     11def make_language_names():
     12    language_names = []
     13    for code, name in settings.LANGUAGES:
     14        try:
     15            translation.activate(code)
     16        except IndexError:
     17            print "Warning: Can't activate language %r, using English name %r." % (code, name)
     18            translation.activate('en')
     19        language_names.append({
     20            'language_code': code,
     21            'name': name,
     22            'name_local': translation.ugettext(name)})
     23    from os.path import join, dirname, abspath
     24    target = abspath(join(dirname(__file__), '..', '..', LANGUAGE_NAMES_PY_FILE))
     25    print 'Writing language names list to\n%s' % target
     26    file(target, 'w').write('language_names = %r\n' % language_names)
     27    print 'Done.'
     28
     29
     30if __name__ == "__main__":
     31    make_language_names()
Back to Top