From 002b3d87b5f1e35edcb6e14ce2e2827fc6c032a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ehrlich?= Date: Sun, 31 May 2015 15:20:19 +0100 Subject: [PATCH] Fixed #24230 -- Added translated language name for i18n template tag/filter. --- django/templatetags/i18n.py | 8 ++++ django/utils/translation/__init__.py | 11 ++++-- docs/releases/1.9.txt | 4 ++ docs/spelling_wordlist | 1 + docs/topics/i18n/translation.txt | 11 ++++++ .../template_tests/syntax_tests/test_i18n.py | 37 ++++++++++++++----- 6 files changed, 59 insertions(+), 13 deletions(-) diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py index c72e85d9792..ace6504ffe5 100644 --- a/django/templatetags/i18n.py +++ b/django/templatetags/i18n.py @@ -215,6 +215,7 @@ def do_get_language_info(parser, token): {% get_language_info for LANGUAGE_CODE as l %} {{ l.code }} {{ l.name }} + {{ l.name_translated }} {{ l.name_local }} {{ l.bidi|yesno:"bi-directional,uni-directional" }} """ @@ -238,6 +239,7 @@ def do_get_language_info_list(parser, token): {% for l in langs %} {{ l.code }} {{ l.name }} + {{ l.name_translated }} {{ l.name_local }} {{ l.bidi|yesno:"bi-directional,uni-directional" }} {% endfor %} @@ -253,6 +255,12 @@ def language_name(lang_code): return translation.get_language_info(lang_code)['name'] +@register.filter +def language_name_translated(lang_code): + english_name = translation.get_language_info(lang_code)['name'] + return translation.ugettext(english_name) + + @register.filter def language_name_local(lang_code): return translation.get_language_info(lang_code)['name_local'] diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py index 505d753268f..506fcce7ddb 100644 --- a/django/utils/translation/__init__.py +++ b/django/utils/translation/__init__.py @@ -215,17 +215,22 @@ def get_language_info(lang_code): try: lang_info = LANG_INFO[lang_code] if 'fallback' in lang_info and 'name' not in lang_info: - return get_language_info(lang_info['fallback'][0]) - return lang_info + info = get_language_info(lang_info['fallback'][0]) + else: + info = lang_info except KeyError: if '-' not in lang_code: raise KeyError("Unknown language code %s." % lang_code) generic_lang_code = lang_code.split('-')[0] try: - return LANG_INFO[generic_lang_code] + info = LANG_INFO[generic_lang_code] except KeyError: raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code)) + if info: + info['name_translated'] = ugettext_lazy(info['name']) + return info + trim_whitespace_re = re.compile('\s*\n\s*') diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index 72e7fce2dc2..bca086b2072 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -187,6 +187,10 @@ Internationalization for languages which can be written in different scripts, for example Latin and Cyrillic (e.g. ``be@latin``). +* Added the ``name_translated`` attribute to the object returned by the + :ttag:`get_language_info` template tag. Also added a corresponding template + filter: :tfilter:`language_name_translated`. + Management Commands ^^^^^^^^^^^^^^^^^^^ diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist index a641bbeec34..0fe8fb78864 100644 --- a/docs/spelling_wordlist +++ b/docs/spelling_wordlist @@ -515,6 +515,7 @@ naturalday naturaltime nd needsinfo +německy nestable neuroscientist newforms diff --git a/docs/topics/i18n/translation.txt b/docs/topics/i18n/translation.txt index bfaec965e43..d3f68d2ca56 100644 --- a/docs/topics/i18n/translation.txt +++ b/docs/topics/i18n/translation.txt @@ -870,6 +870,11 @@ You can then access the information:: Name of language: {{ lang.name_local }}
Name in English: {{ lang.name }}
Bi-directional: {{ lang.bidi }} + Name in the active language: {{ lang.name_translated }} + +.. versionadded:: 1.9 + + The ``name_translated`` attribute was added. .. templatetag:: get_language_info_list @@ -899,6 +904,7 @@ you can iterate over those languages in the template:: .. templatefilter:: language_name .. templatefilter:: language_name_local .. templatefilter:: language_bidi +.. templatefilter:: language_name_translated Template filters ~~~~~~~~~~~~~~~~ @@ -908,6 +914,11 @@ There are also simple filters available for convenience: * ``{{ LANGUAGE_CODE|language_name }}`` ("German") * ``{{ LANGUAGE_CODE|language_name_local }}`` ("Deutsch") * ``{{ LANGUAGE_CODE|language_bidi }}`` (False) +* ``{{ LANGUAGE_CODE|language_name_translated }}`` ("německy", when active language is Czech) + +.. versionadded:: 1.9 + + The ``language_name_translated`` filter was added. .. _Django templates: ../templates_python/ diff --git a/tests/template_tests/syntax_tests/test_i18n.py b/tests/template_tests/syntax_tests/test_i18n.py index 1d0f83478a9..e40144e3470 100644 --- a/tests/template_tests/syntax_tests/test_i18n.py +++ b/tests/template_tests/syntax_tests/test_i18n.py @@ -342,17 +342,26 @@ class I18nTagTests(SimpleTestCase): ) @setup({'i18n32': '{% load i18n %}{{ "hu"|language_name }} ' - '{{ "hu"|language_name_local }} {{ "hu"|language_bidi }}'}) + '{{ "hu"|language_name_local }} {{ "hu"|language_bidi }} ' + '{{ "hu"|language_name_translated }}'}) def test_i18n32(self): output = self.engine.render_to_string('i18n32') - self.assertEqual(output, 'Hungarian Magyar False') + self.assertEqual(output, 'Hungarian Magyar False Hungarian') + + with translation.override('cs'): + output = self.engine.render_to_string('i18n32') + self.assertEqual(output, 'Hungarian Magyar False maďarsky') @setup({'i18n33': '{% load i18n %}' '{{ langcode|language_name }} {{ langcode|language_name_local }} ' - '{{ langcode|language_bidi }}'}) + '{{ langcode|language_bidi }} {{ langcode|language_name_translated }}'}) def test_i18n33(self): output = self.engine.render_to_string('i18n33', {'langcode': 'nl'}) - self.assertEqual(output, 'Dutch Nederlands False') + self.assertEqual(output, 'Dutch Nederlands False Dutch') + + with translation.override('cs'): + output = self.engine.render_to_string('i18n33', {'langcode': 'nl'}) + self.assertEqual(output, 'Dutch Nederlands False nizozemsky') # blocktrans handling of variables which are not in the context. # this should work as if blocktrans was not there (#19915) @@ -405,18 +414,26 @@ class I18nTagTests(SimpleTestCase): # Test whitespace in filter arguments @setup({'i18n38': '{% load i18n custom %}' '{% get_language_info for "de"|noop:"x y" as l %}' - '{{ l.code }}: {{ l.name }}/{{ l.name_local }} bidi={{ l.bidi }}'}) + '{{ l.code }}: {{ l.name }}/{{ l.name_local }}/' + '{{ l.name_translated }} bidi={{ l.bidi }}'}) def test_i18n38(self): - output = self.engine.render_to_string('i18n38') - self.assertEqual(output, 'de: German/Deutsch bidi=False') + with translation.override('cs'): + output = self.engine.render_to_string('i18n38') + self.assertEqual(output, 'de: German/Deutsch/německy bidi=False') @setup({'i18n38_2': '{% load i18n custom %}' '{% get_language_info_list for langcodes|noop:"x y" as langs %}' '{% for l in langs %}{{ l.code }}: {{ l.name }}/' - '{{ l.name_local }} bidi={{ l.bidi }}; {% endfor %}'}) + '{{ l.name_local }}/{{ l.name_translated }} ' + 'bidi={{ l.bidi }}; {% endfor %}'}) def test_i18n38_2(self): - output = self.engine.render_to_string('i18n38_2', {'langcodes': ['it', 'no']}) - self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ') + with translation.override('cs'): + output = self.engine.render_to_string('i18n38_2', {'langcodes': ['it', 'fr']}) + self.assertEqual( + output, + 'it: Italian/italiano/italsky bidi=False; ' + 'fr: French/français/francouzsky bidi=False; ' + ) @setup({'template': '{% load i18n %}{% trans %}A}'}) def test_syntax_error_no_arguments(self):