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

Translations with nested modules does not pick correct slug translation for parent. #2640

Open
plokko opened this issue Jul 31, 2024 · 0 comments

Comments

@plokko
Copy link

plokko commented Jul 31, 2024

Description

Translations with nested modules does not pick correct slug translation for parent.
For example if i create a nested category in italian and english where parent slug translates to:

  • parent-it for it
  • parent-en for en
    and create a child component with slug
  • child-it for it
  • child-en for en
    on the back-end it will displayed as
  • parent-en/child-en for en
  • parent-en/child-it for it (wrong).

I also have the same problem with parend-child nested modules (ex. nested translable categories with articles)

Steps to reproduce

Create a nested module with translation, create a child node and try to switch different language.
The

Expected result

Nested modules should show slug on correct translation.

Actual result

Parent slug is not translated on language change.

Versions

Twill 3.3.1
Laravel 10/11 (tested on both)
Php 8.2/8.3
Mysql 8

Temporary solution

After a lot of fiddling i found an improvised solution

Original code:

class PageController extends NestedModuleController
{
    //...
 
    protected function form(?int $id, TwillModelContract $item = null): array
    {
        $item = $this->repository->getById($id, $this->formWith, $this->formWithCount);
 
        $this->permalinkBase = $item->ancestorsSlug;
 
        return parent::form($id, $item);
    }
}

We will save all the translation for the permalinks and return them from getLocalizedPermalinkBase

class PageController extends NestedModuleController
{
    //...
    /// we will store localized slugs here:
    protected array $localizedPermalinkBase = [];
    protected function form(?int $id, ?TwillModelContract $item = null): array
    {
        $item = $this->repository->getById($id, $this->formWith, $this->formWithCount);

        ///default locale ancestor slug
        $this->permalinkBase = $item->ancestorsSlug;
        ///multiple locale ancestor slugs
        $locales = config('translatable.locales');
        $this->localizedPermalinkBase = [];
        foreach (config('translatable.locales') as $locale) {
            $this->localizedPermalinkBase[$locale] = $item->getAncestorsSlug($locale);
        }

        return parent::form($id, $item);
    }
    protected function getLocalizedPermalinkBase(): array
    {
        /// this function is called on parent::form so we should be safe
        return $this->localizedPermalinkBase;
    }
}
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

1 participant