twill icon indicating copy to clipboard operation
twill copied to clipboard

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

Open plokko opened this issue 1 year ago • 0 comments

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;
    }
}

plokko avatar Jul 31 '24 22:07 plokko