0

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 Answers 2

3

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.

2
  • I've created a post about it with picture
    – ev.werkz
    Nov 13, 2019 at 9:13
  • :( jQuery on Controller like frontend, dont realy work
    – ev.werkz
    Nov 13, 2019 at 9:50
0

@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

3
  • can you share minimal working plugin using drop box etc. i can check and fix it and send you back may be. Nov 13, 2019 at 12:03
  • I create snippet by Bitbucket: bitbucket.org/snippets/ev-werkz/7nBkx6
    – ev.werkz
    Nov 14, 2019 at 13:43
  • 1
    I have updated answer hope it should solve your issue. Nov 19, 2019 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.