wagtail / wagtail-localize

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

Translation of "wagtail.contrib.settings" models (BaseGenericSetting and BaseSiteSetting) #681

Closed pyzenberg closed 3 months ago

pyzenberg commented 1 year ago

How can I separate the site settings for each language? For example, separate Twitter accounts for each language and...

zerolab commented 1 year ago

This is a Wagtail core issue. WIP PR - https://github.com/wagtail/wagtail/pull/9390

lb- commented 9 months ago

Flagging that an issue has been raised to track this request. Any input (clarity on use cases, desire to sponsor or contribute) should be added to the issue.

https://github.com/wagtail/wagtail/issues/11508

zerolab commented 3 months ago

Closing as this is a core matter

pyzenberg commented 3 months ago

Sorry for the late reply as I opened the issue first. I think I found the answer to my own question later.

Here is the shortened code snippet:

from django.conf import settings
from django.db import models
from django.utils.translation import gettext as _, get_language
from modelcluster.models import ClusterableModel
from wagtail.admin.panels import FieldPanel, MultiFieldPanel, TabbedInterface
from wagtail.contrib.settings.models import BaseGenericSetting
from wagtail.contrib.settings.registry import register_setting

@register_setting
class LocalizedConfig(BaseGenericSetting, ClusterableModel):
    language_code = models.CharField(
        _('language'),
        max_length=2,
        unique=True,
    )

    # social network tab
    twitter = models.URLField(_('Twitter'), blank=True)
    youtube = models.URLField(_('Youtube'), blank=True)
    instagram = models.URLField(_('Instagram'), blank=True)
    _tab_socials = [
        FieldPanel('twitter'),
        FieldPanel('youtube'),
        FieldPanel('instagram'),
    ]

    panels = [
        TabbedInterface([
            MultiFieldPanel(_tab_socials, heading=_('Social Networks')),
        ]),
    ]

    class Meta:
        verbose_name = _('Localized Configuration')

    def save(self, *args, **kwargs):
        if not self.language_code:
            self.language_code = get_language() or settings.LANGUAGE_CODE
        super().save(*args, **kwargs)

    @classmethod
    def base_queryset(cls):
        queryset = cls.objects.filter(language_code=get_language())
        if cls.select_related is not None:
            queryset = queryset.select_related(*cls.select_related)
        return queryset
#

And here is the full code. I was used Wagtail 4.2.1 then. Hope it help.

zerolab commented 2 months ago

Thank you @pyzenberg