Package: djangocms-aldryn-translation-tools
Aldryn Translation ToolsContinuation of the deprecated projectDivio Aldryn search.A collection of shared helpers and mixins for translated django-CMS projects.To use, install it into your project using pip:pip install djangocms-aldryn-translation-toolsContributingWe’re grateful to all contributors who have helped create and maintain this package.Contributors are listed atcontributions page.admin.AllTranslationsMixinAutomatically adds a list of language “tags” in the changelist. Tag color
indicates the state of translation for that object. Grey meaning untranslated,
blue meaning translated. Darker versions of each are used to indicate the
current language.Each tag is linked to the specific language tag on the change form of the
object.A similar capability is in HVAD, and now there is this for Parler-based
projects.Preview:To use this, merely import and add the mixin to your Model Admin class:from parler.admin import TranslatableAdmin
from aldryn_translation_tools.admin import AllTranslationsMixin

class GroupAdmin(AllTranslationsMixin, TranslatableAdmin):
   # ....If you wish to put the tags into a different column, you can addall_translationsto the list_display list wherever you’d like, otherwise the
“Languages” column will automatically be placed on the far right.admin.LinkedRelatedInlineMixinThis admin inline mixin links the first field to the row object’s own admin
change form.If the first field is editable, results are undefined but probably won’t work
as expected. For best results, consider making all fields readonly (since they
can be edited with ease by following the link), and disabling the ability to
add new objects by overriding has_add_permission() on the inline to always
returnFalse.models.TranslatedAutoSlugMixinThis is a TranslatableModel mixin that automatically generates a suitable
slug for the object onsave().Ifslug_globally_uniqueisTrue, then slugs will be required to be
unique across all languages.Ifslug_globally_uniqueisFalse(default), then the strategy used here
is that it is OK for two objects to use the same slug if the slugs are for
different languages. So if this were used on a translated Article model, these
URLs would be valid:/en/pain- An article in EN about physical discomfort/fr/pain- An article in FR about breadOf course, this means that when resolving an object from its URL, care must
be taken to factor in the language segment of the URL too.When using this mixin, it is important to also set theslug_source_field_nameproperty on the implementing model to the name of
the translated field which the slug is to be derived from. If you require more
slugs to be derived from multiple fields (translated or otherwise), simply
override the methodget_slug_sourceto provide the source string for the
slug.Configuration propertiesslug_defaultProvide a lazy translated string to use for the default slug should an object
not have a source string to derive a slug from.slug_field_nameProvide the name of the translated field in which generated slug shall
be stored.slug_globally_uniqueA boolean flag controlling whether slugs are globally unique, or only unique
with each language. Default value is False.slug_max_lengthDeclares the max_length of slugs. This defaults to themax_lengthof the
slug_field and is determined via introspection.slug_separatorThis determines the separator used before any index added to the slug. It doesnotdetermine the separator used in the slug itself, which is always-.
This is only provided for compatibility with the slugify()`` method in
aldryn_common, but it is not recommended to be used. Defaults to-.slug_source_field_nameProvide the name of the translated field to be used for deriving the slug.
If more than one field, or other complex sources are required, override the
methodget_slug_source()instead. Note that ifget_slug_source()is
overriden, it is recommended to also overrideget_slug_default().Public methodsget_slug_defaultNaively constructs a translated default slug from the object. For better
results, just set theslug_defaultproperty on the class to a lazy
translated string. Alternatively, override this method if you need to more
programmtically determine the default slug.Example: If your model is “news article” and your source field is “title” this
will return “news-article-without-title”.get_slug_max_lengthAccepts an optional parameteridx_len.Introspects the slug field to determine the maximum length, taking into account
a possible separator and up to a [idx_len]-digit number.get_slug_sourceSimply returns the value of the slug source field. Override for more complex
situations such as using multiple fields (translated or not) as the source.models.TranslationHelperMixinPublic Methodsknown_translation_getter()Signature:(value, language) = obj.known_translation_getter(field, default=None, language_code=None, any_language=False)Acts like Parler’s safe_translation_getter(), but respects the fallback
preferences as defined insettings.CMS_LANGUAGESand provides both the
translated value and the language it represents as a tuple.This is especially helpful when resolving an object’s absolute url for a given
language. If a fallback is used (respecting preference), then the returned
language_code can then be used to set the correct context for a reverse() to get
a URL consistent to the resulting language.For example:from django.utils.translation import override

from aldryn_translation_tools.models import TranslationHelperMixin
from cms.utils.i18n import get_current_language
from parler.models import TranslatableModel, TranslatedFields

class Fruit(TranslationHelperMixin, TranslatableModel):
    translations = TranslatableFields(
        name=models.CharField(...),
        slug=models.CharField(...)
    )

    def get_absolute_url(self, language=None):
        language = language or get_current_language()
        (slug, language) = self.known_translation_getter('slug',
            default=None, language_code=language, any_language=False)
        with override(language):
            return reverse('fruit-detail', kwargs={'slug': slug})In contrast, if we had only done something like this:...

    def get_absolute_url(self, language=None)
        language = language or get_current_language()
        slug = self.safe_translation_getter('slug', default=None,
            language_code=language, any_language=False)
        with override(language):
            return reverse('fruit-detail', kwargs={'slug': slug})Then, if the fruitapplehas not yet been translated to FR it is possible that
you’ll end up with the slug in a fallback langauge, and the rest of the URL in
the requested language, so instead of getting a language-consistent fallback
url:/en/apple/You might get:/fr/apple/Which, at best, would be confusing for site visitors but more likely won’t exist
resulting in a NoReverseFound exception or 404 and which clearly is not
respecting the fallback preferences set by the developer.