wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
227 stars 87 forks source link

ParentalManyToManyField and translations #534

Open Redjam opened 2 years ago

Redjam commented 2 years ago

Hi there,

I have a translatable snippet (lets call it "items") that editors can associate to a page using checkboxes.

To achieve that I use a model with a ParentalManyToManyField. That way, editors can associate items to a page.

Wagtail Localize works perfectly to translate the items snippet.

Issue: when editing the page, the items listed are not scoped to the selected language. Instead, editors have the list of all items in all languages (items are duplicated).

For example if my website is in English and French, if I edit a page in english, I will have the list of items in English and in French as well.

The solution I found is to replace the ParentalManytoManyField with an inline panel (following this tutorial -> https://www.accordbox.com/blog/wagtail-tip-1-how-replace-parentalmanytomanyfield-inlinepanel/) and making the intermediate model a translatable model.

I don't know if it's the only solution, but for my case it would have been more convenient to stick with a ParentalManyToManyField.

crukundo commented 2 years ago

Having the same issue. Have a snippet for Country. And while these items have the same translation keys in the db, they still show up in admin as separate countries.

Screenshot 2022-03-15 at 10 40 16

crukundo commented 2 years ago

Was able to solve it by taking advantage of limit_choices_to on the ParentalManyToManyField. It's a hack but I had to map it just the objects with locale_id="1".

def limit_country_choices():  
    limit = models.Q(locale__id="1")  
    return limit

Then in my class:

countries = ParentalManyToManyField(  
        "core.Country",  
        blank=True,  
        verbose_name="Select host country",  
        limit_choices_to=limit_country_choices,  
   )
Redjam commented 2 years ago

I think it's a good hack if you don't need the translated version displayed on the back-office. Why don't you use an inline panel? Checkboxes are important in your case?

zemogle commented 2 years ago

I used this solution with a slight modification. Instead of hard coding the language ID, I use whatever the language of the user is. This isn't without its problems (i.e. if a user's browser is in a language not supported), but for us it works fine.

from django.utils.translation import get_language

def limit_country_choices():  
    limit = models.Q(locale__language_code=get_language())  
    return limit
zerolab commented 2 years ago

Related Wagtail issue - https://github.com/wagtail/wagtail/issues/8821