Ticket #4030: make-language-names.diff
File make-language-names.diff, 3.7 KB (added by , 17 years ago) |
---|
-
django/templatetags/i18n.py
14 14 context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES] 15 15 return '' 16 16 17 class 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 17 26 class GetCurrentLanguageNode(Node): 18 27 def __init__(self, variable): 19 28 self.variable = variable … … 95 104 raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args 96 105 return GetAvailableLanguagesNode(args[2]) 97 106 107 def 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 98 132 def do_get_current_language(parser, token): 99 133 """ 100 134 This will store the current language in the context. … … 240 274 return BlockTranslateNode(extra_context, singular, plural, countervar, counter) 241 275 242 276 register.tag('get_available_languages', do_get_available_languages) 277 register.tag('get_available_language_names', do_get_available_language_names) 243 278 register.tag('get_current_language', do_get_current_language) 244 279 register.tag('get_current_language_bidi', do_get_current_language_bidi) 245 280 register.tag('trans', do_translate) -
django/bin/make-language-names.py
1 #!/usr/bin/env python 2 3 LANGUAGE_NAMES_PY_FILE = 'django/conf/language_names.py' 4 5 # Need to ensure that the i18n framework is enabled 6 from django.conf import settings 7 settings.configure(USE_I18N = True) 8 9 from django.utils import translation 10 11 def 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 30 if __name__ == "__main__": 31 make_language_names()