django-modeltranslation icon indicating copy to clipboard operation
django-modeltranslation copied to clipboard

BooleanField fallback bug

Open fabiocaccamo opened this issue 9 years ago • 13 comments

Steps:

  • we have 2 languages, English and French, English is the default language.
  • we have an i18n BooleanField called 'published'.
  • the value of 'published_en' is True and the value of 'published_fr' is False.
  • if we call 'published' (when the active language is French), it fallbacks to 'published_en' and returns True because 'published_fr' results empty.

I think that BooleanField(s) should be excluded from the default fallback behavior.

fabiocaccamo avatar Jan 25 '16 19:01 fabiocaccamo

I solved it by disabling fallback_languages in the translation options: fallback_languages = {'default':()}

In any case I think that this should be the default behavior for BooleanFields.

fabiocaccamo avatar Jan 26 '16 10:01 fabiocaccamo

I'll check this. You're right - False value of BooleanField shouldn't cause fallback.

zlorf avatar Jan 26 '16 10:01 zlorf

But, why have you a BooleanField translated? Use django gettext function for it, it's easy.

SalahAdDin avatar Feb 05 '16 06:02 SalahAdDin

I think the idea is to have a checkbox for every language, as in the example: published.

zlorf avatar Feb 05 '16 09:02 zlorf

@zlorf exactly :)

fabiocaccamo avatar Feb 05 '16 09:02 fabiocaccamo

I don't understand you yet.

SalahAdDin avatar Feb 06 '16 09:02 SalahAdDin

The goal is not to display translated True/False (which can be made with gettext), but to have a set of boolean fields per language. Consider Article class: it has title, content and published flag (BooleanField). And developer want to have title and content translated, as well as separate checkbox for controlling publication of article for every language. So that he can publish article e.g. in English and German, but don't publish in Spanish yet, since the Spanish translation is missing.

zlorf avatar Feb 06 '16 10:02 zlorf

Ah, now i understand! Thanks.

SalahAdDin avatar Feb 06 '16 17:02 SalahAdDin

Just experienced the exact same thing; see for my original question: http://stackoverflow.com/questions/41998415/how-to-model-a-many-to-many-relation-when-the-target-model-is-a-tuple-in-django/ it explains why this is a nice feature...

Paul424 avatar Feb 03 '17 12:02 Paul424

from modeltranslation.utils import fallbacks

with fallbacks(False):
    is_published = object.published

liminspace avatar Oct 19 '18 11:10 liminspace

I just hit the same issue :( Going with fallbacks disabled for now... MODELTRANSLATION_ENABLE_FALLBACKS = False

viktoradavid avatar Dec 09 '18 11:12 viktoradavid

MODELTRANSLATION_ENABLE_FALLBACKS is a global option. so it can be not a good idea as only BooleanField has problem with fallback

liminspace avatar Dec 09 '18 15:12 liminspace

It can be solved by overriding fallback_undefined:

class Product(models.Model):
    published = models.BooleanField(...)
@register(Product)
class ProductTranslationOptions(TranslationOptions):
    fields = ('published', )
    fallback_undefined = {
        "published": None,
    }

EnriqueSoria avatar Jun 15 '22 14:06 EnriqueSoria