rainlab / translate-plugin

Enables multi-lingual sites
Other
125 stars 88 forks source link

Event not firing #547

Closed Ladylain closed 4 years ago

Ladylain commented 4 years ago

Hi

OctoberCMS : version 464 PHP : 7.2.19 Plugins :

Issue

I have a model named Article where I have a translatable Slug I use CMSPage to display an Article with the Slug as URL parameter.

I'm using the Extended Locale Picker from Excodus.TranslateExtended plugin, this one just extends Rainlab.Translate plugin.

What i'm trying to have is to display the translated slug in the URL when i'm switching language from the picker.

I've read the documentation of Rainlab.Translate plugin and add this snippet to my Plugin.php

Event::listen('translate.LocalePicker.translateParams', function($page, $params, $oldLocale, $newLocale) {
    if ($page->baseFileName == 'single-article') {
         return Article::translateParams($params, $oldLocale, $newLocale);
    }
});

and add this one to my model :

    public static function translateParams($params, $oldLocale, $newLocale) {
        $newParams = $params;
        foreach ($params as $paramName => $paramValue) {
            $records = self::transWhere($paramName, $paramValue, $oldLocale)->first();
            if ($records) {
                $records->translateContext($newLocale);
                $newParams[$paramName] = $records->$paramName;
            }
        }
        return $newParams;
    }

but when i switch language the URL parameter is not translated and moreover, if i try to access the page with the translated URL parameter directly, it returns me "Record not found"

Can someone have a solution to fix this ?

Best,

LukeTowers commented 4 years ago

@Ladylain I feel like this is a problem with the Excodus.TranslateExtended plugin

Ladylain commented 4 years ago

@LukeTowers i've just finished to test this bug on another project and it seems that all works fine. The second project have also Excodus.TranslateExtended plugin

I don't know why this is not working for now, i'm investigating.

Ladylain commented 4 years ago

@LukeTowers you can close this issue ! i've found the solution. It was my mistake, i have a lot of code in my plugin and i was doing this :

        // Check if we are currently in backend module.
        if (!App::runningInBackend()) {
            return;
        }
        Event::listen('translate.localePicker.translateParams', function($page, $params, $oldLocale, $newLocale) {
            if ($page->baseFileName == 'single-actualite') {
                return Article::translateParams($params, $oldLocale, $newLocale);
            }
        });

so, unless i was on the backend the listener could not work. :(

Resolution of function boot() in plugin.php :

        Event::listen('translate.localePicker.translateParams', function($page, $params, $oldLocale, $newLocale) {
            if ($page->baseFileName == 'single-actualite') {
                return Article::translateParams($params, $oldLocale, $newLocale);
            }
        });

        // Check if we are currently in backend module.
        if (!App::runningInBackend()) {
            return;
        }

sorry for wasting your time, and thanks for your help

best,