Code

Ticket #4030: 4030_language_info_v2_r8347_updated_languages.diff

File 4030_language_info_v2_r8347_updated_languages.diff, 20.2 KB (added by akaihola, 6 years ago)

Updated the pre-generated language information dictionary according to current Django languages

Line 
1Index: django/conf/language_info.py
2===================================================================
3--- django/conf/language_info.py        (revision 0)
4+++ django/conf/language_info.py        (revision 0)
5@@ -0,0 +1,203 @@
6+# This file was generated with django/bin/make-language-info.py and it is not
7+# recommended to edit it by hand.  For new translations, compile the new .po
8+# files and edit the LANGUAGES setting, and finally re-run
9+# django/bin/make-language-info.py to generate this file.
10+
11+language_info = {
12+ 'ar': {'bidi': True,
13+        'language_code': 'ar',
14+        'name': 'Arabic',
15+        'name_local': u'Arabic'},
16+ 'bg': {'bidi': False,
17+        'language_code': 'bg',
18+        'name': 'Bulgarian',
19+        'name_local': u'\u0431\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438'},
20+ 'bn': {'bidi': False,
21+        'language_code': 'bn',
22+        'name': 'Bengali',
23+        'name_local': u'\u09ac\u09be\u0982\u09b2\u09be'},
24+ 'ca': {'bidi': False,
25+        'language_code': 'ca',
26+        'name': 'Catalan',
27+        'name_local': u'catal\xe0'},
28+ 'cs': {'bidi': False,
29+        'language_code': 'cs',
30+        'name': 'Czech',
31+        'name_local': u'\u010desky'},
32+ 'cy': {'bidi': False,
33+        'language_code': 'cy',
34+        'name': 'Welsh',
35+        'name_local': u'Cymraeg'},
36+ 'da': {'bidi': False,
37+        'language_code': 'da',
38+        'name': 'Danish',
39+        'name_local': u'Dansk'},
40+ 'de': {'bidi': False,
41+        'language_code': 'de',
42+        'name': 'German',
43+        'name_local': u'Deutsch'},
44+ 'el': {'bidi': False,
45+        'language_code': 'el',
46+        'name': 'Greek',
47+        'name_local': u'\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac'},
48+ 'en': {'bidi': False,
49+        'language_code': 'en',
50+        'name': 'English',
51+        'name_local': u'English'},
52+ 'es': {'bidi': False,
53+        'language_code': 'es',
54+        'name': 'Spanish',
55+        'name_local': u'espa\xf1ol'},
56+ 'es-ar': {'bidi': False,
57+           'language_code': 'es-ar',
58+           'name': 'Argentinean Spanish',
59+           'name_local': u'espa\xf1ol de Argentina'},
60+ 'et': {'bidi': False,
61+        'language_code': 'et',
62+        'name': 'Estonian',
63+        'name_local': u'Estonian'},
64+ 'eu': {'bidi': False,
65+        'language_code': 'eu',
66+        'name': 'Basque',
67+        'name_local': u'Basque'},
68+ 'fa': {'bidi': True,
69+        'language_code': 'fa',
70+        'name': 'Persian',
71+        'name_local': u'Persian'},
72+ 'fi': {'bidi': False,
73+        'language_code': 'fi',
74+        'name': 'Finnish',
75+        'name_local': u'suomi'},
76+ 'fr': {'bidi': False,
77+        'language_code': 'fr',
78+        'name': 'French',
79+        'name_local': u'Fran\xe7ais'},
80+ 'ga': {'bidi': False,
81+        'language_code': 'ga',
82+        'name': 'Irish',
83+        'name_local': u'Gaeilge'},
84+ 'gl': {'bidi': False,
85+        'language_code': 'gl',
86+        'name': 'Galician',
87+        'name_local': u'galego'},
88+ 'he': {'bidi': True,
89+        'language_code': 'he',
90+        'name': 'Hebrew',
91+        'name_local': u'\u05e2\u05d1\u05e8\u05d9\u05ea'},
92+ 'hr': {'bidi': False,
93+        'language_code': 'hr',
94+        'name': 'Croatian',
95+        'name_local': u'Hrvatski'},
96+ 'hu': {'bidi': False,
97+        'language_code': 'hu',
98+        'name': 'Hungarian',
99+        'name_local': u'Magyar'},
100+ 'is': {'bidi': False,
101+        'language_code': 'is',
102+        'name': 'Icelandic',
103+        'name_local': u'\xcdslenska'},
104+ 'it': {'bidi': False,
105+        'language_code': 'it',
106+        'name': 'Italian',
107+        'name_local': u'Italiano'},
108+ 'ja': {'bidi': False,
109+        'language_code': 'ja',
110+        'name': 'Japanese',
111+        'name_local': u'\u65e5\u672c\u8a9e'},
112+ 'ka': {'bidi': False,
113+        'language_code': 'ka',
114+        'name': 'Georgian',
115+        'name_local': u'\u10e5\u10d0\u10e0\u10d7\u10e3\u10da\u10d8'},
116+ 'km': {'bidi': False,
117+        'language_code': 'km',
118+        'name': 'Khmer',
119+        'name_local': u'Khmer'},
120+ 'kn': {'bidi': False,
121+        'language_code': 'kn',
122+        'name': 'Kannada',
123+        'name_local': u'Kannada'},
124+ 'ko': {'bidi': False,
125+        'language_code': 'ko',
126+        'name': 'Korean',
127+        'name_local': u'\ud55c\uad6d\uc5b4'},
128+ 'lt': {'bidi': False,
129+        'language_code': 'lt',
130+        'name': 'Lithuanian',
131+        'name_local': u'Lithuanian'},
132+ 'lv': {'bidi': False,
133+        'language_code': 'lv',
134+        'name': 'Latvian',
135+        'name_local': u'Latvie\u0161u'},
136+ 'mk': {'bidi': False,
137+        'language_code': 'mk',
138+        'name': 'Macedonian',
139+        'name_local': u'\u041c\u0430\u043a\u0435\u0434\u043e\u043d\u0441\u043a\u0438'},
140+ 'nl': {'bidi': False,
141+        'language_code': 'nl',
142+        'name': 'Dutch',
143+        'name_local': u'Nederlands'},
144+ 'no': {'bidi': False,
145+        'language_code': 'no',
146+        'name': 'Norwegian',
147+        'name_local': u'Norsk'},
148+ 'pl': {'bidi': False,
149+        'language_code': 'pl',
150+        'name': 'Polish',
151+        'name_local': u'Polski'},
152+ 'pt': {'bidi': False,
153+        'language_code': 'pt',
154+        'name': 'Portugese',
155+        'name_local': u'Portugese'},
156+ 'pt-br': {'bidi': False,
157+           'language_code': 'pt-br',
158+           'name': 'Brazilian Portuguese',
159+           'name_local': u'Portugu\xeas Brasileiro'},
160+ 'ro': {'bidi': False,
161+        'language_code': 'ro',
162+        'name': 'Romanian',
163+        'name_local': u'Romana'},
164+ 'ru': {'bidi': False,
165+        'language_code': 'ru',
166+        'name': 'Russian',
167+        'name_local': u'\u0420\u0443\u0441\u0441\u043a\u0438\u0439'},
168+ 'sk': {'bidi': False,
169+        'language_code': 'sk',
170+        'name': 'Slovak',
171+        'name_local': u'Slovensky'},
172+ 'sl': {'bidi': False,
173+        'language_code': 'sl',
174+        'name': 'Slovenian',
175+        'name_local': u'Slovenski'},
176+ 'sr': {'bidi': False,
177+        'language_code': 'sr',
178+        'name': 'Serbian',
179+        'name_local': u'Srpski'},
180+ 'sv': {'bidi': False,
181+        'language_code': 'sv',
182+        'name': 'Swedish',
183+        'name_local': u'Svenska'},
184+ 'ta': {'bidi': False,
185+        'language_code': 'ta',
186+        'name': 'Tamil',
187+        'name_local': u'\u0ba4\u0bae\u0bbf\u0bb4\u0bcd'},
188+ 'te': {'bidi': False,
189+        'language_code': 'te',
190+        'name': 'Telugu',
191+        'name_local': u'Telugu'},
192+ 'tr': {'bidi': False,
193+        'language_code': 'tr',
194+        'name': 'Turkish',
195+        'name_local': u'T\xfcrk\xe7e'},
196+ 'uk': {'bidi': False,
197+        'language_code': 'uk',
198+        'name': 'Ukrainian',
199+        'name_local': u'Ukrainian'},
200+ 'zh-cn': {'bidi': False,
201+           'language_code': 'zh-cn',
202+           'name': 'Simplified Chinese',
203+           'name_local': u'\u7b80\u4f53\u4e2d\u6587'},
204+ 'zh-tw': {'bidi': False,
205+           'language_code': 'zh-tw',
206+           'name': 'Traditional Chinese',
207+           'name_local': u'\u7e41\u9ad4\u4e2d\u6587'}
208+}
209Index: django/templatetags/i18n.py
210===================================================================
211--- django/templatetags/i18n.py (revision 8347)
212+++ django/templatetags/i18n.py (working copy)
213@@ -17,6 +17,34 @@
214         context[self.variable] = [(k, translation.ugettext(v)) for k, v in settings.LANGUAGES]
215         return ''
216 
217+class GetLanguageInfoNode(Node):
218+    def __init__(self, lang_code, variable):
219+        self.lang_code = Variable(lang_code)
220+        self.variable = variable
221+
222+    def render(self, context):
223+        lang_code = self.lang_code.resolve(context)
224+        context[self.variable] = translation.get_language_info(lang_code)
225+        return ''
226+
227+class GetLanguageInfoListNode(Node):
228+    def __init__(self, languages, variable):
229+        self.languages = Variable(languages)
230+        self.variable = variable
231+
232+    def get_language_info(self, language):
233+        # ``language`` is either a language code string or a sequence with the
234+        # language code as its first item
235+        if len(language[0]) > 1:
236+            return translation.get_language_info(language[0])
237+        else:
238+            return translation.get_language_info(str(language))
239+
240+    def render(self, context):
241+        languages = self.languages.resolve(context)
242+        context[self.variable] = [self.get_language_info(l) for l in languages]
243+        return ''
244+
245 class GetCurrentLanguageNode(Node):
246     def __init__(self, variable):
247         self.variable = variable
248@@ -107,6 +135,55 @@
249         raise TemplateSyntaxError, "'get_available_languages' requires 'as variable' (got %r)" % args
250     return GetAvailableLanguagesNode(args[2])
251 
252+def do_get_language_info(parser, token):
253+    """
254+    This will store the language information dictionary for the given language
255+    code in a context variable.
256+
257+    Usage::
258+
259+        {% get_language_info for language_code as l %}
260+        {{ l.language_code }}
261+        {{ l.name }}
262+        {{ l.name_local }}
263+        {{ l.bidi|yesno:"bi-directional,uni-directional" }}
264+    """
265+    args = token.contents.split()
266+    if len(args) != 5 or args[1] != 'for' or args[3] != 'as':
267+        raise TemplateSyntaxError, "'%s' requires 'for string as variable' (got %r)" % (args[0], args[1:])
268+    return GetLanguageInfoNode(args[2], args[4])
269+
270+def do_get_language_info_list(parser, token):
271+    """
272+    This will store a list of language information dictionaries for the given
273+    language codes in a context variable.  The language codes can be specified
274+    ither as a list of strings or a settings.LANGUAGES style tuple (or any
275+    sequence of sequences whose first items are language codes).
276+
277+    Usage::
278+
279+        {% get_language_info_list for LANGUAGES as langs %}
280+        {% for l in langs %}
281+          {{ l.language_code }}
282+          {{ l.name }}
283+          {{ l.name_local }}
284+          {{ l.bidi|yesno:"bi-directional,uni-directional" }}
285+        {% endfor %}
286+    """
287+    args = token.contents.split()
288+    if len(args) != 5 or args[1] != 'for' or args[3] != 'as':
289+        raise TemplateSyntaxError, "'%s' requires 'for sequence as variable' (got %r)" % (args[0], args[1:])
290+    return GetLanguageInfoListNode(args[2], args[4])
291+
292+def language_name(lang_code):
293+    return translation.get_language_info(lang_code)['name']
294+
295+def language_name_local(lang_code):
296+    return translation.get_language_info(lang_code)['name_local']
297+
298+def language_bidi(lang_code):
299+    return translation.get_language_info(lang_code)['bidi']
300+
301 def do_get_current_language(parser, token):
302     """
303     This will store the current language in the context.
304@@ -253,7 +330,13 @@
305             counter)
306 
307 register.tag('get_available_languages', do_get_available_languages)
308+register.tag('get_language_info', do_get_language_info)
309+register.tag('get_language_info_list', do_get_language_info_list)
310 register.tag('get_current_language', do_get_current_language)
311 register.tag('get_current_language_bidi', do_get_current_language_bidi)
312 register.tag('trans', do_translate)
313 register.tag('blocktrans', do_block_translate)
314+
315+register.filter(language_name)
316+register.filter(language_name_local)
317+register.filter(language_bidi)
318Index: django/utils/translation/__init__.py
319===================================================================
320--- django/utils/translation/__init__.py        (revision 8347)
321+++ django/utils/translation/__init__.py        (working copy)
322@@ -3,6 +3,10 @@
323 """
324 from django.utils.functional import lazy
325 from django.utils.encoding import force_unicode
326+try:
327+    from django.conf.language_info import language_info
328+except ImportError:
329+    raise ImportError("Module django.conf.language_info not found. Run django/bin/make-language-info.py to generate it.")
330 
331 __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
332         'ngettext_lazy', 'string_concat', 'activate', 'deactivate',
333@@ -109,3 +113,9 @@
334     """
335     return u''.join([force_unicode(s) for s in strings])
336 string_concat = lazy(string_concat, unicode)
337+
338+def get_language_info(lang_code):
339+    try:
340+        return language_info[lang_code]
341+    except KeyError:
342+        raise KeyError("Unknown language code %r.  If you have added a new translation, remember to add the language to the LANGUAGES setting and update the django/conf/language_info.py file by running django/bin/make-language-info.py." % lang_code)
343Index: django/bin/make-language-info.py
344===================================================================
345--- django/bin/make-language-info.py    (revision 0)
346+++ django/bin/make-language-info.py    (revision 0)
347@@ -0,0 +1,45 @@
348+#!/usr/bin/env python
349+
350+LANGUAGE_INFO_PY_FILE = 'django/conf/language_info.py'
351+
352+# Need to ensure that the i18n framework is enabled
353+from django.conf import settings
354+settings.configure(USE_I18N = True)
355+
356+TEMPLATE = """\
357+# This file was generated with django/bin/make-language-info.py and it is not
358+# recommended to edit it by hand.  For new translations, compile the new .po
359+# files and edit the LANGUAGES setting, and finally re-run
360+# django/bin/make-language-info.py to generate this file.
361+
362+language_info = {
363+ %s
364+}
365+"""
366+
367+def make_language_info(target_path):
368+    print 'Writing language information to\n%s' % target_path
369+    # Create a dummy language_info.py file to make sure we can import the
370+    # translation module.
371+    file(target_path, 'w').write(TEMPLATE % '')
372+    from django.utils import translation
373+    from pprint import pformat
374+    language_info = {}
375+    for code, name in settings.LANGUAGES:
376+        translation.activate(code)
377+        language_info[code] = {"language_code": code,
378+                               "name": name,
379+                               "name_local": translation.ugettext(name),
380+                               "bidi": code in settings.LANGUAGES_BIDI}
381+    file(target_path, 'w').write(TEMPLATE % pformat(language_info)[1:-1])
382+    # Because the timestamps of the dummy .pyc and real .py may fall on the
383+    # same second, we must enforce compilation.
384+    import py_compile
385+    py_compile.compile(target_path)
386+    print 'Done.'
387+
388+
389+if __name__ == "__main__":
390+    from os.path import join, dirname, abspath
391+    path = abspath(join(dirname(__file__), '..', '..', LANGUAGE_INFO_PY_FILE))
392+    make_language_info(path)
393Index: tests/regressiontests/i18n/tests.py
394===================================================================
395--- tests/regressiontests/i18n/tests.py (revision 8347)
396+++ tests/regressiontests/i18n/tests.py (working copy)
397@@ -1,5 +1,5 @@
398 # coding: utf-8
399-import misc
400+import misc, language_info
401 
402 regressions = ur"""
403 Format string interpolation should work with *_lazy objects.
404@@ -68,5 +68,6 @@
405 
406 __test__ = {
407     'regressions': regressions,
408+    'language_info': language_info.tests,
409     'misc': misc.tests,
410 }
411Index: tests/regressiontests/templates/tests.py
412===================================================================
413--- tests/regressiontests/templates/tests.py    (revision 8347)
414+++ tests/regressiontests/templates/tests.py    (working copy)
415@@ -776,6 +776,14 @@
416             'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'),
417             'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'),
418 
419+            # retrieving language information
420+            'i18n19': ('{% load i18n %}{% get_language_info for "de" as l %}{{ l.language_code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}', {}, 'de: German/Deutsch bidi=False'),
421+            'i18n20': ('{% load i18n %}{% get_language_info for LANGUAGE_CODE as l %}{{ l.language_code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}', {'LANGUAGE_CODE': 'fi'}, 'fi: Finnish/suomi bidi=False'),
422+            'i18n21': ('{% load i18n %}{% get_language_info_list for langcodes as langs %}{% for l in langs %}{{ l.language_code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}', {'langcodes': ['it', 'no']}, 'it: Italian/Italiano bidi=False; no: Norwegian/Norsk bidi=False; '),
423+            'i18n22': ('{% load i18n %}{% get_language_info_list for langcodes as langs %}{% for l in langs %}{{ l.language_code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}', {'langcodes': (('sl', 'Slovenian'), ('fa', 'Persian'))}, 'sl: Slovenian/Slovenski bidi=False; fa: Persian/Persian bidi=True; '),
424+            'i18n23': ('{% load i18n %}{{ "hu"|language_name }} {{ "hu"|language_name_local }} {{ "hu"|language_bidi }}', {}, 'Hungarian Magyar False'),
425+            'i18n24': ('{% load i18n %}{{ langcode|language_name }} {{ langcode|language_name_local }} {{ langcode|language_bidi }}', {'langcode': 'nl'}, 'Dutch Nederlands False'),
426+
427             ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
428 
429             'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),
430Index: docs/i18n.txt
431===================================================================
432--- docs/i18n.txt       (revision 8347)
433+++ docs/i18n.txt       (working copy)
434@@ -679,6 +679,64 @@
435 .. _session: ../sessions/
436 .. _request object: ../request_response/#httprequest-objects
437 
438+Local names of languages
439+========================
440+
441+The ``get_language_info()`` function provides detailed information about
442+languages::
443+
444+    >>> form django.utils.translation import get_language_info
445+    >>> l = get_language_info('de')
446+    >>> print l['name'], l['name_local'], l['bidi']
447+    German Deutsch False
448+
449+The ``name`` and ``name_local`` attributes of the dictionary contain the name
450+of the language in English and the language itself, respectively.  The ``bidi``
451+attribute is True only for bi-directional languages.
452+
453+In templates, you can use special template tags or filters to retrieve the same
454+information. To get information about a single language, use the
455+``{% get_language_info %}`` tag::
456+
457+    {% get_language_info for LANGUAGE_CODE as lang %}
458+    {% get_language_info for "pl" as lang %}
459+
460+You can then access the information::
461+
462+    Language code: {{ lang.language_code }}<br />
463+    Name of language: {{ lang.name_local }}<br />
464+    Name in English: {{ lang.name }}<br />
465+    Bi-directional: {{ lang.bidi }}
466+
467+You can also use the ``{% get_language_info_list %}`` template tag to retrieve
468+information for a list of languages (e.g. active languages as specified in
469+``settings.LANGUAGES``).  See `The set_language redirect view`_ for an example
470+of how to display a language selector using ``{% get_language_info_list %}``.
471+
472+In addition to ``settings.LANGUAGES`` style nested tuples,
473+``{% get_language_info_list %}`` supports simple lists of language codes.  If
474+you do this in your view::
475+
476+  return render_to_response('mytemplate.html',
477+                            {'available_languages': ['en', 'es', 'fr']},
478+                            RequestContext(request))
479+
480+you can iterate those languages in the template::
481+
482+  {% get_language_info_list available_languages %}
483+  {% for l in available_languages %} ... {% endfor %}
484+
485+There are also simple filters available for convenience:
486+
487+    * ``{{ LANGUAGE_CODE|language_name }}``  ("German")
488+    * ``{{ LANGUAGE_CODE|language_name_local }}`` ("Deutsch")
489+    * ``{{ LANGUAGE_CODE|bidi }}`` (False)     
490+
491+The source of the language information is the ``django.conf.language_info``
492+module.  If you add a new translation, you should run the
493+``django/bin/make-language-info.py`` script to insert the information for the
494+added language.
495+
496 Using translations in your own projects
497 =======================================
498 
499@@ -772,16 +830,20 @@
500     * If that's empty -- say, if a user's browser suppresses that header --
501       then the user will be redirected to ``/`` (the site root) as a fallback.
502 
503-Here's example HTML template code::
504+Here's example HTML template code (see `Local names of languages`_ for more
505+information)::
506 
507+    {% get_language_info_list for LANGUAGES as available_languages %}
508     <form action="/i18n/setlang/" method="post">
509-    <input name="next" type="hidden" value="/next/page/" />
510-    <select name="language">
511-    {% for lang in LANGUAGES %}
512-    <option value="{{ lang.0 }}">{{ lang.1 }}</option>
513-    {% endfor %}
514-    </select>
515-    <input type="submit" value="Go" />
516+      <input name="next" type="hidden" value="/next/page/" />
517+      <select name="language">
518+        {% for lang in available_languages %}
519+        <option value="{{ lang.language_code }}" {% ifequal lang.language_code LANGUAGE_CODE %}selected{% endifequal %}>
520+          {{ lang.name_local }}
521+        </option>
522+        {% endfor %}
523+      </select>
524+      <input type="submit" value="Go" />
525     </form>
526 
527 Translations and JavaScript