Code

Ticket #4030: make-language-names.diff

File make-language-names.diff, 3.7 KB (added by akaihola, 7 years ago)

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

Line 
1Index: django/templatetags/i18n.py
2===================================================================
3--- django/templatetags/i18n.py (revision 6341)
4+++ django/templatetags/i18n.py (working copy)
5@@ -14,6 +14,15 @@
6         context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
7         return ''
8 
9+class GetAvailableLanguageNamesNode(Node):
10+    def __init__(self, variable):
11+        self.variable = variable
12+
13+    def render(self, context):
14+        from django.conf.language_names import language_names
15+        context[self.variable] = language_names
16+        return ''
17+
18 class GetCurrentLanguageNode(Node):
19     def __init__(self, variable):
20         self.variable = variable
21@@ -95,6 +104,31 @@
22         raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args
23     return GetAvailableLanguagesNode(args[2])
24 
25+def do_get_available_language_names(parser, token):
26+    """
27+    This will store in the context a list of available
28+    languages and supply each item with the name of the
29+    language both in the current language and the
30+    language itself.
31+
32+    Usage::
33+
34+        {% get_available_language_names as languages %}
35+        {% for language in languages %}
36+        ...
37+        {% endfor %}
38+
39+    This will convert the LANGUAGES setting from your
40+    setting file (or the default settings) to a list of
41+    dicts and put it into the named variable.  Each item
42+    will have the keys ``language_code``, ``name`` and
43+    ``name_local``.
44+    """
45+    args = token.contents.split()
46+    if len(args) != 3 or args[1] != 'as':
47+        raise TemplateSyntaxError, "'get_available_language_names' requires 'as variable' (got %r)" % args
48+    return GetAvailableLanguageNamesNode(args[2])
49+
50 def do_get_current_language(parser, token):
51     """
52     This will store the current language in the context.
53@@ -240,6 +274,7 @@
54     return BlockTranslateNode(extra_context, singular, plural, countervar, counter)
55 
56 register.tag('get_available_languages', do_get_available_languages)
57+register.tag('get_available_language_names', do_get_available_language_names)
58 register.tag('get_current_language', do_get_current_language)
59 register.tag('get_current_language_bidi', do_get_current_language_bidi)
60 register.tag('trans', do_translate)
61Index: django/bin/make-language-names.py
62===================================================================
63--- django/bin/make-language-names.py   (revision 0)
64+++ django/bin/make-language-names.py   (revision 0)
65@@ -0,0 +1,31 @@
66+#!/usr/bin/env python
67+
68+LANGUAGE_NAMES_PY_FILE = 'django/conf/language_names.py'
69+
70+# Need to ensure that the i18n framework is enabled
71+from django.conf import settings
72+settings.configure(USE_I18N = True)
73+
74+from django.utils import translation
75+
76+def make_language_names():
77+    language_names = []
78+    for code, name in settings.LANGUAGES:
79+        try:
80+            translation.activate(code)
81+        except IndexError:
82+            print "Warning: Can't activate language %r, using English name %r." % (code, name)
83+            translation.activate('en')
84+        language_names.append({
85+            'language_code': code,
86+            'name': name,
87+            'name_local': translation.ugettext(name)})
88+    from os.path import join, dirname, abspath
89+    target = abspath(join(dirname(__file__), '..', '..', LANGUAGE_NAMES_PY_FILE))
90+    print 'Writing language names list to\n%s' % target
91+    file(target, 'w').write('language_names = %r\n' % language_names)
92+    print 'Done.'
93+
94+
95+if __name__ == "__main__":
96+    make_language_names()
97
98Property changes on: django/bin/make-language-names.py
99___________________________________________________________________
100Name: svn:executable
101   + *
102Name: svn:eol-style
103   + native
104