Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
839edceb
Kaydet (Commit)
839edceb
authored
Haz 12, 2015
tarafından
Matthew Somerville
Kaydeden (comit)
Tim Graham
Tem 01, 2015
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #21695 -- Added asvar option to blocktrans.
Thanks Bojan Mihelac for the initial patch.
üst
b35b43df
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
5 deletions
+80
-5
i18n.py
django/templatetags/i18n.py
+24
-3
1.9.txt
docs/releases/1.9.txt
+3
-0
translation.txt
docs/topics/i18n/translation.txt
+18
-2
test_i18n.py
tests/template_tests/syntax_tests/test_i18n.py
+35
-0
No files found.
django/templatetags/i18n.py
Dosyayı görüntüle @
839edceb
...
...
@@ -96,7 +96,7 @@ class TranslateNode(Node):
class
BlockTranslateNode
(
Node
):
def
__init__
(
self
,
extra_context
,
singular
,
plural
=
None
,
countervar
=
None
,
counter
=
None
,
message_context
=
None
,
trimmed
=
False
):
counter
=
None
,
message_context
=
None
,
trimmed
=
False
,
asvar
=
None
):
self
.
extra_context
=
extra_context
self
.
singular
=
singular
self
.
plural
=
plural
...
...
@@ -104,6 +104,7 @@ class BlockTranslateNode(Node):
self
.
counter
=
counter
self
.
message_context
=
message_context
self
.
trimmed
=
trimmed
self
.
asvar
=
asvar
def
render_token_list
(
self
,
tokens
):
result
=
[]
...
...
@@ -166,7 +167,11 @@ class BlockTranslateNode(Node):
"string returned by gettext:
%
r using
%
r"
%
(
result
,
data
))
with
translation
.
override
(
None
):
result
=
self
.
render
(
context
,
nested
=
True
)
return
result
if
self
.
asvar
:
context
[
self
.
asvar
]
=
result
return
''
else
:
return
result
class
LanguageNode
(
Node
):
...
...
@@ -429,6 +434,13 @@ def do_block_translate(parser, token):
{
%
blocktrans with foo|filter as bar and baz|filter as boo
%
}
{
%
blocktrans count var|length as count
%
}
The translated string can be stored in a variable using `asvar`::
{
%
blocktrans with bar=foo|filter boo=baz|filter asvar var
%
}
This is {{ bar }} and {{ boo }}.
{
%
endblocktrans
%
}
{{ var }}
Contextual translations are supported::
{
%
blocktrans with bar=foo|filter context "greeting"
%
}
...
...
@@ -442,6 +454,7 @@ def do_block_translate(parser, token):
options
=
{}
remaining_bits
=
bits
[
1
:]
asvar
=
None
while
remaining_bits
:
option
=
remaining_bits
.
pop
(
0
)
if
option
in
options
:
...
...
@@ -468,6 +481,13 @@ def do_block_translate(parser, token):
six
.
reraise
(
TemplateSyntaxError
,
TemplateSyntaxError
(
msg
),
sys
.
exc_info
()[
2
])
elif
option
==
"trimmed"
:
value
=
True
elif
option
==
"asvar"
:
try
:
value
=
remaining_bits
.
pop
(
0
)
except
IndexError
:
msg
=
"No argument provided to the '
%
s' tag for the asvar option."
%
bits
[
0
]
six
.
reraise
(
TemplateSyntaxError
,
TemplateSyntaxError
(
msg
),
sys
.
exc_info
()[
2
])
asvar
=
value
else
:
raise
TemplateSyntaxError
(
'Unknown argument for
%
r tag:
%
r.'
%
(
bits
[
0
],
option
))
...
...
@@ -506,7 +526,8 @@ def do_block_translate(parser, token):
raise
TemplateSyntaxError
(
"'blocktrans' doesn't allow other block tags (seen
%
r) inside it"
%
token
.
contents
)
return
BlockTranslateNode
(
extra_context
,
singular
,
plural
,
countervar
,
counter
,
message_context
,
trimmed
=
trimmed
)
counter
,
message_context
,
trimmed
=
trimmed
,
asvar
=
asvar
)
@register.tag
...
...
docs/releases/1.9.txt
Dosyayı görüntüle @
839edceb
...
...
@@ -332,6 +332,9 @@ Internationalization
project and it will find all the app message files that were created by
:djadmin:`makemessages`.
* :ttag:`blocktrans` supports assigning its output to a variable using
``asvar``.
Management Commands
^^^^^^^^^^^^^^^^^^^
...
...
docs/topics/i18n/translation.txt
Dosyayı görüntüle @
839edceb
...
...
@@ -580,8 +580,9 @@ use the following syntax::
<title>{{ the_title }}</title>
<meta name="description" content="{{ the_title }}">
In practice you'll use this to get strings that are used in multiple places
or should be used as arguments for other template tags or filters::
In practice you'll use this to get a string you can use in multiple places in a
template or so you can use the output as an argument for other template tags or
filters::
{% trans "starting point" as start %}
{% trans "end point" as end %}
...
...
@@ -682,6 +683,21 @@ be retrieved (and stored) beforehand::
This is a URL: {{ the_url }}
{% endblocktrans %}
If you'd like to retrieve a translated string without displaying it, you can
use the following syntax::
{% blocktrans asvar the_title %}The title is {{ title }}.{% endblocktrans %}
<title>{{ the_title }}</title>
<meta name="description" content="{{ the_title }}">
In practice you'll use this to get a string you can use in multiple places in a
template or so you can use the output as an argument for other template tags or
filters.
.. versionchanged:: 1.9
The ``asvar`` syntax was added.
``{% blocktrans %}`` also supports :ref:`contextual
markers<contextual-markers>` using the ``context`` keyword:
...
...
tests/template_tests/syntax_tests/test_i18n.py
Dosyayı görüntüle @
839edceb
...
...
@@ -435,6 +435,35 @@ class I18nTagTests(SimpleTestCase):
'fr: French/français/francouzsky bidi=False; '
)
# blocktrans tag with asvar
@setup
({
'i18n39'
:
'{
%
load i18n
%
}'
'{
%
blocktrans asvar page_not_found
%
}Page not found{
%
endblocktrans
%
}'
'>{{ page_not_found }}<'
})
def
test_i18n39
(
self
):
with
translation
.
override
(
'de'
):
output
=
self
.
engine
.
render_to_string
(
'i18n39'
)
self
.
assertEqual
(
output
,
'>Seite nicht gefunden<'
)
@setup
({
'i18n40'
:
'{
%
load i18n
%
}'
'{
%
trans "Page not found" as pg_404
%
}'
'{
%
blocktrans with page_not_found=pg_404 asvar output
%
}'
'Error: {{ page_not_found }}'
'{
%
endblocktrans
%
}'
})
def
test_i18n40
(
self
):
output
=
self
.
engine
.
render_to_string
(
'i18n40'
)
self
.
assertEqual
(
output
,
''
)
@setup
({
'i18n41'
:
'{
%
load i18n
%
}'
'{
%
trans "Page not found" as pg_404
%
}'
'{
%
blocktrans with page_not_found=pg_404 asvar output
%
}'
'Error: {{ page_not_found }}'
'{
%
endblocktrans
%
}'
'>{{ output }}<'
})
def
test_i18n41
(
self
):
with
translation
.
override
(
'de'
):
output
=
self
.
engine
.
render_to_string
(
'i18n41'
)
self
.
assertEqual
(
output
,
'>Error: Seite nicht gefunden<'
)
@setup
({
'template'
:
'{
%
load i18n
%
}{
%
trans
%
}A}'
})
def
test_syntax_error_no_arguments
(
self
):
msg
=
"'trans' takes at least one argument"
...
...
@@ -453,6 +482,12 @@ class I18nTagTests(SimpleTestCase):
with
self
.
assertRaisesMessage
(
TemplateSyntaxError
,
msg
):
self
.
engine
.
render_to_string
(
'template'
)
@setup
({
'template'
:
'{
%
load i18n
%
}{
%
blocktrans asvar
%
}Yes{
%
endblocktrans
%
}'
})
def
test_blocktrans_syntax_error_missing_assignment
(
self
):
msg
=
"No argument provided to the 'blocktrans' tag for the asvar option."
with
self
.
assertRaisesMessage
(
TemplateSyntaxError
,
msg
):
self
.
engine
.
render_to_string
(
'template'
)
@setup
({
'template'
:
'{
%
load i18n
%
}{
%
trans "Yes" as var context
%
}'
})
def
test_syntax_error_missing_context
(
self
):
msg
=
"No argument provided to the 'trans' tag for the context option."
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment