Problems with Translations and Multisite

We are facing an issue related to translating a specific model. We want to translate both the ‘Name’ and ‘Slug’ fields of this model. When the model is accessed via a component, we intend for the translated version of the model to be called, allowing for different URLs based on the language.

We have attempted to resolve this using both the MultiSite functionality and the Translate Plugin, but neither approach seems to work correctly.

Procedure with multisite:

Model:

use \October\Rain\Database\Traits\Multisite;
protected $propagatable = [];

Component:

public function onRun() : void
{
$this->page['test'] = Tran::where('slug', $this->param('slug'))->first();
}

HTML

{% if test %}
<h1>{{test.name}}</h1>
{% else %}
<h1>No data!!!!</h1>
{% endif %}


<p> Switch language to:</p>
<ul>

    {% for site in sitePicker.sites %}
    <li>
        <a href="{{ site.url }}">{{ site.url }}</a>
    </li>
    {% endfor %}
</ul>

Theme page:

url = "/seite/:slug"
layout = "default"
title = "Seite"

[viewBag]
localeUrl[en-gb] = "/page/:slug"
localeUrl[en] = "/us-page/:slug"
localeTitle[en-gb] = "Page"
localeTitle[en] = "/US PAGE"

[sitePicker]
[rms_trans]
==
{% component 'rms_trans' %}

URL: localhost:8011/de/seite/hund

Hund
Switch language to:

localhost:8011/de/seite/hund
localhost:8011/en/page/hund
localhost:8011/en-us/us-page/hund

URL: localhost:8011/en/page/dog

Dog
Switch language to:

localhost:8011/de/seite/dog
localhost:8011/en/page/dog
localhost:8011/en-us/us-page/dog

Why isn’t the model slug translated?


With Translate Plugin

Model:

public $implement = [
\RainLab\Translate\Behaviors\TranslatableModel::class
];
public $translatable = ['name', 'slug'];

Component:

public function onRun() : void
{
$this->page['test'] = Test::transWhere('slug', $this->param('slug'))->first();
}

HTML:

{% if test %}
<h1>{{test.name}}</h1>
{% else %}
<h1>NO Data!!!!</h1>
{% endif %}

<p> Switch language to:</p>
<ul>

    {% for site in sitePicker.sites %}
    <li>
        <a href="{{ site.url }}">{{ site.url }}</a>
    </li>
    {% endfor %}
</ul>

Theme page:

url = "/test-de/:slug"
layout = "default"
title = "DE"


[sitePicker]
[rms_test]

[viewBag]
localeUrl[en-gb] = "/test-en/:slug"
localeUrl[en] = "/test-us/:slug"
localeTitle[en-gb] = "EN"
localeTitle[en] = "US"
==
{% component 'rms_test' %}

Result:
URL : localhost:8011/de/test-de/katze

Katze
Switch language to:

localhost:8011/de/test-de/katze
localhost:8011/en/test-en/katze
localhost:8011/en-us/test-us/katze

URL: localhost:8011/en/test-en/katze

Cat
Switch language to:

localhost:8011/de/test-de/katze
localhost:8011/en/test-en/katze
localhost:8011/en-us/test-us/katze

URL: localhost:8011/en/test-en/cat

NO Data!!!!
Switch language to:

localhost:8011/de/test-de/cat
localhost:8011/en/test-en/cat
localhost:8011/en-us/test-us/cat

I don’t understand how this works and how it doesn’t translate the slug, but the pages in the theme do.

Hi @rms-team

By default, the sitePicker component isn’t aware of model parameters in the URL, such as page slugs and identifiers. The cms.sitePicker.overrideParams event is used to override the URL parameters to their translated versions. A good place to put this event is inside the init or the onRun method of a CMS Component class.

For example, if the model implements the Multisite trait, the newOtherSiteQuery method is used to locate the model for the proposed site and modify the URL parameters.

$myModel = MyModel::find(1);
$otherModels = $myModel->newOtherSiteQuery()->get();

Event::listen('cms.sitePicker.overrideParams', function($page, $params, $currentSite, $proposedSite) use ($otherModels) {
    $otherModel = $otherModels->where('site_id', $proposedSite->id)->first();
    if ($otherModel) {
        $params['id'] = $otherModel->id;
        $params['slug'] = $otherModel->slug;
        $params['fullslug'] = $otherModel->fullslug;
    }
    return $params;
});

More information on the site picker component can be found in the documentation: