Translate-plugin: Event not firing

Created on 30 Jan 2020  路  3Comments  路  Source: rainlab/translate-plugin

Hi

OctoberCMS : version 464
PHP : 7.2.19
Plugins :

  • Rainlab.Builder
  • Excodus.TranslateExtended
  • Rainlab.Translate

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,

Question third party

All 3 comments

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

@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.

@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,

Was this page helpful?
0 / 5 - 0 ratings