Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sub menu doesn't show localized items #388

Closed
AlexDeltax opened this issue Mar 10, 2021 · 4 comments
Closed

sub menu doesn't show localized items #388

AlexDeltax opened this issue Mar 10, 2021 · 4 comments

Comments

@AlexDeltax
Copy link

I've created custom flat menu using Replacing both the FlatMenu and FlatMenuItem models

The problem is, that if menu has submenus, items there from link_page, and not from link_page_fr or so. Same is for link_text.

As a result, I have:

home_en
- page_en_one
- page_en_two

in another language I have:

home_fr
- page_en_one
- page_en_two

but I need:

home_fr
- page_fr_one
- page_fr_two
class TranslatedPageField(object):
    def __init__(self, en_field, fr_field):
        self.en_field = en_field
        self.fr_field = fr_field

    def __get__(self, instance, owner):
        active_language = translation.get_language()
        if active_language == 'fr':
            return getattr(instance, self.fr_field)
        return getattr(instance, self.en_field)

class TranslatedFlatMenuItem(AbstractFlatMenuItem):
    menu = ParentalKey(
        TranslatedFlatMenu, # we can use the model from above
        on_delete=models.CASCADE,
        related_name=settings.FLAT_MENU_ITEMS_RELATED_NAME,
    )
    link_page_fr = models.ForeignKey(
        Page,
        verbose_name=_('Link to an internal page (Frenc)'),
        blank=True,
        null=True,
        on_delete=models.CASCADE,
        related_name='link_page_ru',
    )
    link_url_fr = models.CharField(
        verbose_name=_('Link to a custom URL (French)'),
        max_length=255,
        blank=True,
        null=True,
    )
    link_text_ru = models.CharField(
        verbose_name=_("Link text (Russian)"),
        max_length=255,
        blank=True,
    )
    translated_link_text = TranslatedField('link_text', 'link_text_fr')
    translated_link_url = TranslatedField('link_url', 'link_url_fr')
    translated_link_page = TranslatedPageField('link_page', 'link_page_fr')

    def translated_page(self):
        return self.translated_link_page

    @property
    def menu_text(self):
        """Use `translated_link_text` instead of just `link_text`"""
        return self.translated_link_text or getattr(
            self.translated_link_page,
            settings.PAGE_FIELD_FOR_MENU_ITEM_TEXT,
            self.translated_link_page.title
        )

    def relative_url(self, site=None, request=None):
        if self.translated_link_page:
            try:
                page_url = self.translated_link_page.get_url(
                    request=request, current_site=site)
                return page_url + self.url_append
            except TypeError:
                return ''
        elif self.translated_link_url:
            return self.translated_link_url + self.url_append
        return self.link_url + self.url_append

Any suggestions of what kind of information I need to add? Have I missed something or did wrong?

@noparamos
Copy link

Good day. Maybe this will help you: #242 .

@AlexDeltax
Copy link
Author

Good day. Maybe this will help you: #242 .

@noparamos, yes, I've read that issue and some solutions, but no success with AbstractFlatMenuItem. Adding localized do nothing. It shows submenu for page in link_page but when I active another language and want to get submenu for link_page_fr I got nothing.

@ababic
Copy link
Collaborator

ababic commented Apr 8, 2021

@AlexDeltax you will want to look at (and override) the methods on the menu model that gather the pages needed for generation, like the developer in the linked PR is doing.

It looks like you're using a completely different approach to translation than they are, so the code will not work like-for-like - you will need to figure out how to use your menu model's language-specific foreign key values.

@ababic
Copy link
Collaborator

ababic commented Apr 8, 2021

Am closing this issue, as it isn't a bug with this software - more a support request

@ababic ababic closed this as completed Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants