How i can on October CMS use translate plugin after extend new fields to user plugin?

1.9k views Asked by At

For my project i use User Plugin and Translate Plugin. I've added custom new fields to the user and now I want to translate them.

I think I know why it does not work. But find no solution. Somebody to have idea?

if i add to $model->translatable default fields like 'email', works fine.

i added boot function to my custom plugin with this code

\RainLab\User\Models\User::extend(function ($model) {
 $model->implement[] = 'RainLab.Translate.Behaviors.TranslatableModel';
 $model->translatable = ['about', 'preview_text'];
});
2

There are 2 answers

2
Hardik Satasiya On BEST ANSWER

Hmm,

There is one problem. when you try to add it directly $model->translatable, It seems its treating it like attribute of model.

Try this $model->addDynamicProperty(variable_name, value);

\RainLab\User\Models\User::extend(function ($model) {
   $model->implement[] = 'RainLab.Translate.Behaviors.TranslatableModel';
   $model->addDynamicProperty('translatable', ['about', 'preview_text']);
   // like this ^
});

It should treat it as local variable and it should work.

If any doubts please comment.

Revision [ Final solution ] - solution for : it works for existing fields when we are adding new fields this is not working.

Problem: With translation mechanism is that it listens the backend.form.extendFieldsBefore event for form and then register fields for translation. When we try to register new fields in form using extendFormFields extension, It happens afterward so new added fields are not visible to translation listener. so they are kind of skipped as translation field registration process already been done.

Solution: So for solution we can just add our field before translation registartion happens. luckily translate plugin has lowest -1 priority for listening this event backend.form.extendFieldsBefore so we can register our fields before It so we are good now and our fields can be added before it can process fields for translation.

Code

\Event::listen('backend.form.extendFieldsBefore', function($widget) {        
    // You should always check to see if you're extending correct model
    if (!$widget->model instanceof \RainLab\User\Models\User) {
        return;
    }

    // we will merge current fields with fields we want to add
    // we used shorthand + plus operator for this
    $widget->tabs['fields'] = $widget->tabs['fields'] + Config::get('rms.secis::user_fields');

    // here Config::get('rms.secis::user_fields') is just returning field array
    // Fo ref. Ex: 
    // return [
    //     'gender' => [
    //         'label'   => 'Gender',
    //         'tab'     => 'Security Island',
    //         'type'    => 'radio',
    //         'options' => [
    //             'male'   => 'Male',
    //             'female' => 'Female'
    //         ],
    //         'span'    => 'auto'
    //     ],
    // ];
});

Note: we are adding fields to tab so we are using $widget->tabs['fields'] to add fields to the tabs. If you want to add normal fields or secondary tab fields you can use $widget->fields and $widget->secondaryTabs['fields] respectively.

Yes now translator can see our fields and its processed, It should able to show translation UI in frontend-ui as well.

if any doubts please comment.

3
ev.werkz On

@hardik-satasiya

yes, no more errors on frontend, but new problem is, that no translate functions on fields. Maybe to add jQuery Script to Controller?

Integration without JQuery and October Framework files: https://octobercms.com/plugin/rainlab-translate

end of documentation

enter image description here