Code

Ticket #4030: language-local-name.diff

File language-local-name.diff, 2.7 KB (added by akaihola, 7 years ago)

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

Line 
1Index: django/templatetags/i18n.py
2===================================================================
3--- django/templatetags/i18n.py (revision 6182)
4+++ django/templatetags/i18n.py (working copy)
5@@ -14,6 +14,26 @@
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 import settings
15+        original_language = translation.get_language()
16+        context[self.variable] = []
17+        for code, name in settings.LANGUAGES:
18+            try:
19+                translation.activate(code)
20+            except IndexError:
21+                translation.activate(original_language)
22+            context[self.variable].append({
23+                'language_code': code,
24+                'name': name,
25+                'name_local': translation.ugettext(name)})
26+        translation.activate(original_language)
27+        return ''
28+
29 class GetCurrentLanguageNode(Node):
30     def __init__(self, variable):
31         self.variable = variable
32@@ -95,6 +115,31 @@
33         raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args
34     return GetAvailableLanguagesNode(args[2])
35 
36+def do_get_available_language_names(parser, token):
37+    """
38+    This will store in the context a list of available
39+    languages and supply each item with the name of the
40+    language both in the current language and the
41+    language itself.
42+
43+    Usage::
44+
45+        {% get_available_language_names as languages %}
46+        {% for language in languages %}
47+        ...
48+        {% endfor %}
49+
50+    This will convert the LANGUAGES setting from your
51+    setting file (or the default settings) to a list of
52+    dicts and put it into the named variable.  Each item
53+    will have the keys ``language_code``, ``name`` and
54+    ``name_local``.
55+    """
56+    args = token.contents.split()
57+    if len(args) != 3 or args[1] != 'as':
58+        raise TemplateSyntaxError, "'get_available_language_names' requires 'as variable' (got %r)" % args
59+    return GetAvailableLanguageNamesNode(args[2])
60+
61 def do_get_current_language(parser, token):
62     """
63     This will store the current language in the context.
64@@ -240,6 +285,7 @@
65     return BlockTranslateNode(extra_context, singular, plural, countervar, counter)
66 
67 register.tag('get_available_languages', do_get_available_languages)
68+register.tag('get_available_language_names', do_get_available_language_names)
69 register.tag('get_current_language', do_get_current_language)
70 register.tag('get_current_language_bidi', do_get_current_language_bidi)
71 register.tag('trans', do_translate)