Fixed #24230 -- Added translated language name for i18n template tag/filter.

This commit is contained in:
Tomáš Ehrlich 2015-05-31 15:20:19 +01:00 committed by Tim Graham
parent 9ca0961b8a
commit 002b3d87b5
6 changed files with 59 additions and 13 deletions

View File

@ -215,6 +215,7 @@ def do_get_language_info(parser, token):
{% get_language_info for LANGUAGE_CODE as l %} {% get_language_info for LANGUAGE_CODE as l %}
{{ l.code }} {{ l.code }}
{{ l.name }} {{ l.name }}
{{ l.name_translated }}
{{ l.name_local }} {{ l.name_local }}
{{ l.bidi|yesno:"bi-directional,uni-directional" }} {{ l.bidi|yesno:"bi-directional,uni-directional" }}
""" """
@ -238,6 +239,7 @@ def do_get_language_info_list(parser, token):
{% for l in langs %} {% for l in langs %}
{{ l.code }} {{ l.code }}
{{ l.name }} {{ l.name }}
{{ l.name_translated }}
{{ l.name_local }} {{ l.name_local }}
{{ l.bidi|yesno:"bi-directional,uni-directional" }} {{ l.bidi|yesno:"bi-directional,uni-directional" }}
{% endfor %} {% endfor %}
@ -253,6 +255,12 @@ def language_name(lang_code):
return translation.get_language_info(lang_code)['name'] 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 @register.filter
def language_name_local(lang_code): def language_name_local(lang_code):
return translation.get_language_info(lang_code)['name_local'] return translation.get_language_info(lang_code)['name_local']

View File

@ -215,17 +215,22 @@ def get_language_info(lang_code):
try: try:
lang_info = LANG_INFO[lang_code] lang_info = LANG_INFO[lang_code]
if 'fallback' in lang_info and 'name' not in lang_info: if 'fallback' in lang_info and 'name' not in lang_info:
return get_language_info(lang_info['fallback'][0]) info = get_language_info(lang_info['fallback'][0])
return lang_info else:
info = lang_info
except KeyError: except KeyError:
if '-' not in lang_code: if '-' not in lang_code:
raise KeyError("Unknown language code %s." % lang_code) raise KeyError("Unknown language code %s." % lang_code)
generic_lang_code = lang_code.split('-')[0] generic_lang_code = lang_code.split('-')[0]
try: try:
return LANG_INFO[generic_lang_code] info = LANG_INFO[generic_lang_code]
except KeyError: except KeyError:
raise KeyError("Unknown language code %s and %s." % (lang_code, generic_lang_code)) 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*') trim_whitespace_re = re.compile('\s*\n\s*')

View File

@ -187,6 +187,10 @@ Internationalization
for languages which can be written in different scripts, for example Latin for languages which can be written in different scripts, for example Latin
and Cyrillic (e.g. ``be@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 Management Commands
^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^

View File

@ -515,6 +515,7 @@ naturalday
naturaltime naturaltime
nd nd
needsinfo needsinfo
německy
nestable nestable
neuroscientist neuroscientist
newforms newforms

View File

@ -870,6 +870,11 @@ You can then access the information::
Name of language: {{ lang.name_local }}<br /> Name of language: {{ lang.name_local }}<br />
Name in English: {{ lang.name }}<br /> Name in English: {{ lang.name }}<br />
Bi-directional: {{ lang.bidi }} 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 .. templatetag:: get_language_info_list
@ -899,6 +904,7 @@ you can iterate over those languages in the template::
.. templatefilter:: language_name .. templatefilter:: language_name
.. templatefilter:: language_name_local .. templatefilter:: language_name_local
.. templatefilter:: language_bidi .. templatefilter:: language_bidi
.. templatefilter:: language_name_translated
Template filters Template filters
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
@ -908,6 +914,11 @@ There are also simple filters available for convenience:
* ``{{ LANGUAGE_CODE|language_name }}`` ("German") * ``{{ LANGUAGE_CODE|language_name }}`` ("German")
* ``{{ LANGUAGE_CODE|language_name_local }}`` ("Deutsch") * ``{{ LANGUAGE_CODE|language_name_local }}`` ("Deutsch")
* ``{{ LANGUAGE_CODE|language_bidi }}`` (False) * ``{{ 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/ .. _Django templates: ../templates_python/

View File

@ -342,17 +342,26 @@ class I18nTagTests(SimpleTestCase):
) )
@setup({'i18n32': '{% load i18n %}{{ "hu"|language_name }} ' @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): def test_i18n32(self):
output = self.engine.render_to_string('i18n32') 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 %}' @setup({'i18n33': '{% load i18n %}'
'{{ langcode|language_name }} {{ langcode|language_name_local }} ' '{{ langcode|language_name }} {{ langcode|language_name_local }} '
'{{ langcode|language_bidi }}'}) '{{ langcode|language_bidi }} {{ langcode|language_name_translated }}'})
def test_i18n33(self): def test_i18n33(self):
output = self.engine.render_to_string('i18n33', {'langcode': 'nl'}) 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. # blocktrans handling of variables which are not in the context.
# this should work as if blocktrans was not there (#19915) # this should work as if blocktrans was not there (#19915)
@ -405,18 +414,26 @@ class I18nTagTests(SimpleTestCase):
# Test whitespace in filter arguments # Test whitespace in filter arguments
@setup({'i18n38': '{% load i18n custom %}' @setup({'i18n38': '{% load i18n custom %}'
'{% get_language_info for "de"|noop:"x y" as l %}' '{% 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): def test_i18n38(self):
with translation.override('cs'):
output = self.engine.render_to_string('i18n38') output = self.engine.render_to_string('i18n38')
self.assertEqual(output, 'de: German/Deutsch bidi=False') self.assertEqual(output, 'de: German/Deutsch/německy bidi=False')
@setup({'i18n38_2': '{% load i18n custom %}' @setup({'i18n38_2': '{% load i18n custom %}'
'{% get_language_info_list for langcodes|noop:"x y" as langs %}' '{% get_language_info_list for langcodes|noop:"x y" as langs %}'
'{% for l in langs %}{{ l.code }}: {{ l.name }}/' '{% 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): def test_i18n38_2(self):
output = self.engine.render_to_string('i18n38_2', {'langcodes': ['it', 'no']}) with translation.override('cs'):
self.assertEqual(output, 'it: Italian/italiano bidi=False; no: Norwegian/norsk bidi=False; ') 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}'}) @setup({'template': '{% load i18n %}{% trans %}A}'})
def test_syntax_error_no_arguments(self): def test_syntax_error_no_arguments(self):