Multilingual support¶
New in version 1.0.
django-fluent-contents supports creating page content in multiple languages. As all content items are added to a “parent” object, some support on the parent side is needed to use this feature. The benefit however, is that you can use django-fluent-contents with any multilingual package.
Installation¶
All created content items have a language_code
attribute,
to identify the language an item was created in. This attribute is filled using the language of the parent
object those content items are created for.
The current language can be detected by providing:
- A
get_current_language()
method on the parent object. This covers all models that use django-parler for translation support. - A
language_code
attribute or property on the parent object.
When such properly is found on the parent, all rendering functions use that information too. For example, only content items that match the parent’s language will be rendered.
Rendering items¶
Django projects can use a different language per request or URL, using any of these methods:
- Running a second instance with a different
LANGUAGE_CODE
setting. - Using
LocaleMiddleware
in combination withi18n_patterns()
. - Writing code that calls
translation.activate()
directly.
By default, all content items are rendered in the language they are created in.
When you switch the language, the item will still appear in the original language.
It uses translation.override
during the rendering.
Without such strict policy, you risk combining database content of the original language,
and {% trans ".." %}
content in the current language.
The output would become a mix of languages, and even be stored in the cache this way.
When you deal with this explicitly, this behavior can be disabled. There are some plugin settings available:
cache_output_per_language
- Cache each rendering in the currently active language. This is perfect for using{% trans ".." %}
tags in the template and items rendered in fallback languages too.render_ignore_item_language
- Don’t change the language at all. This is typically desired whencache_output
is completely disabled. Theget_language()
points to the active site language.
Fallback languages¶
When a page is not translated, the system can be instructed to render the fallback language. The fallback language has to be defined in FLUENT_CONTENTS_DEFAULT_LANGUAGE_CODE.
In the templates, use:
{% render_placeholder ... fallback=True %}
The render_placeholder()
function also has a fallback_language
parameter.