English 中文(简体)
django blocktrans and i18n in templates
原标题:

I have an i18n problem in django:

This works fine :

{% trans cat.name %}  

cat.name will be translated

But this doesn t work:

{% blocktrans with cat.name|slugify as cat_slug %}{{ cat_slug }}{% endblocktrans %}  

cat.name is not translated

If I change the filter :

{% blocktrans with cat.name|capfirst as cat_slug %}{{ cat_slug }}{% endblocktrans %}  

I can see that the filter is working, but there is no translation...

问题回答

I m only just getting started with Django internationalization, but I think you re misunderstanding how the {% blocktrans %} tag handles placeholders.

The point of blocktrans is to allow the text around the placeholders to be translated. It won t translate anything inside {{...}}.

If you look at the generated .po file, you ll see that the following template code:

{% blocktrans %}This is my variable: {{variable}}{% endblocktrans %}

Will get converted into something like the following:

msgid:"This is my variable: %s"

I don t think you can translate a variable within a blocktrans tag. You can probably do constant strings with {% blocktrans with _("string") as x %}{{x}}{% endblocktrans %} but I can t think why you d want to.

You ll have to do what you want in your view or model code I think.

This works:

{% filter slugify %}{% trans cat.name %}{% endfilter %}

As Tom pointed out blocktrans will preserve what you put inside the with statement instead of translating it. What you need to do is use the with before the translation. In your example, it would look like this:

{% with cat_slug=cat.name|slugify %}
    {% trans cat_slug %}
{% endwith %}

P.S. I know I m answering a 6yr old question, but I ve run across this exact situation a couple times now and haven t seen a SO question/answer that handles it.

{% blocktrans with cat.name as cat_slug %}{{ cat_slug|capfirst }}{% endblocktrans %}

?

EDIT: you were right the doc says the filter as to be placed in the blocktrans

{% blocktrans with cat_slug=cat.name|capfirst %}{{ cat_slug }}{% endblocktrans %}  




相关问题
How does gettext handle dynamic content?

In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content? I have 2 cases in mind. 1) Let s say I have <?=$user1?> poked John <?=$user2?>. ...

Explain the Need for Mutexes in Locales, Please

Reading the question Why doesn’t C++ STL support atoi(const string& ) like functions?, I encountered a comment which warned that GCC (at least) has a bug that can slow down multi-threaded ...

How does Vistalizer work

How does Vistalizer manage to override the language limit in Windows Vista Home edition. Which api s does it use to allow installation of Multiple language packages.

Localized exceptions (within a Struts2 app)

I am developing a Struts 2 application with support for multiple languages. If one of the domain objects needs to throw an exception, how can it do so in such a way that the error message is no ...

Rails Globalize plugin help

Has anyone gotten the Globalize plugin to work Rails 2.3.2 or later? If so, could you direct me to some useful info?

热门标签