mirror of https://github.com/django/django.git
Fixed #24230 -- Added translated language name for i18n template tag/filter.
This commit is contained in:
parent
9ca0961b8a
commit
002b3d87b5
|
@ -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']
|
||||
|
|
|
@ -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*')
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
|
|
@ -515,6 +515,7 @@ naturalday
|
|||
naturaltime
|
||||
nd
|
||||
needsinfo
|
||||
německy
|
||||
nestable
|
||||
neuroscientist
|
||||
newforms
|
||||
|
|
|
@ -870,6 +870,11 @@ You can then access the information::
|
|||
Name of language: {{ lang.name_local }}<br />
|
||||
Name in English: {{ lang.name }}<br />
|
||||
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/
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue