append language-code tot variable name

18 views
Skip to first unread message

Jaap van Wingerde

unread,
May 23, 2017, 10:08:55 AM5/23/17
to Django users
In my templates I have variables as {{ artobject.title|safe|capfirst }}. I want the site in more languages.So in changed the model: title changed in title_nl, and I appended title_de, title_en, title_fr. This is more easy for me than po-files.

A template must work for all languages,so the variable above in the template must become {{ artobject.title_nl|safe|capfirst }} etc.There is a variable {{ LANGUAGE_CODE }}, but nesting the variables: {{ artobject.title_{{ LANGUAGE_CODE }}|safe|capfirst }} does not work. How can I solve this?

Matthew Pava

unread,
May 23, 2017, 10:39:39 AM5/23/17
to django...@googlegroups.com

Have you considered internationalization and localization as an alternative to what you are doing?

https://docs.djangoproject.com/en/1.11/topics/i18n/

 

To do what you are doing, you would need to do it in the view function, something like this:

context[‘title’] = getattr(artobject, “%s_%s” % (title, LANGUAGE_CODE))

 

And in your template:

{{ title|safe|capfirst }}

 

 

You might also be able to create your own filter that would take two arguments: the prefix and the language code.  You would do that if you really want that logic in your template and not inside the view function.  Check this out for information on creating your own template filters:

https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5ed6725c-a77d-48c5-bdc2-4e5642b03919%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Melvyn Sopacua

unread,
May 28, 2017, 12:01:47 PM5/28/17
to django...@googlegroups.com

On Tuesday 23 May 2017 07:08:55 Jaap van Wingerde wrote:

> In my templates I have variables as {{ artobject.title|safe|capfirst

> }}. I want the site in more languages.So in changed the model: title

> changed in title_nl, and I appended title_de, title_en, title_fr.

 

You're trying to solve what Django Modeltranslation already did. Allthough, it can be hard working with it in the beginning stage of a project as models change structure a lot, it should be a breeze when data model is stable.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages