validator message i18n

validator message i18n

we0038we0038 Posts: 39Questions: 13Answers: 1

So basically, the default message for notEmpty is "This field is required."
Is there a practical way to do internationalization other than overriding
ValidateOptions::inst()->message()
thanks

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421

    https://editor.datatables.net/manual/php/validation

    You can set a custom message or use a custom validator. That's it, I guess.

  • allanallan Posts: 63,564Questions: 1Answers: 10,479 Site admin
    Answer ✓

    Yup, that's basically it. How else could it be done? Possibly return a token and have that token looked up? Then you'd need to have a list of the tokens and you might as well do the lookup at the server-side?

    Allan

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421

    I agree, Allan. That wouldn't add much value.

    I mostly use custom validators and return English or German from the server depending on the respective user language.

  • we0038we0038 Posts: 39Questions: 13Answers: 1

    Yes token based solution is what I am looking for. I am using i18next jquery (I have more than 2 languages). When I first thought about it, I had to change the default message values in Editor/Validate.php to tokens.
    However, I then thought about updates. So I asked the question here.

  • we0038we0038 Posts: 39Questions: 13Answers: 1

    forget to mention that I am doing translation on front-end side.

  • we0038we0038 Posts: 39Questions: 13Answers: 1

    well.. thanks for sharing ideas. I ended up doing it on server-side like this:

    Field::inst( 'user.username' )
            ->validator( Validate::notEmpty( ValidateOptions::inst()
              ->message( $i18n['required'] )
            ))
    

    where $i18n is an array with the user language messages

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421

    Sounds like a good solution. Mine is a little more "primitive". Here is an example with a global validator. These are not global variables. I need to use "use" to be able to use the messages in the validator.

    if ($lang === 'de') {     
        $msg[0] = 'Feld darf nicht leer sein.';
        $msg[1] = 'Bitte wählen Sie mindestens eine Installation aus.';
        $msg[2] = 'Förderregister: Jede Abteilung kann nur eine Installation haben.';
        $msg[3] = 'Vertragsverwaltung: Wegen nutzerübergreifender Laufender Nummer '
                . 'kann jede Abteilung nur eine Installation haben.';
    } else {
        $msg[0] = 'Field may not be empty.';
        $msg[1] = 'Please select at least one installation.';
        $msg[2] = 'Subsidy Register: Each department can have only one installation.';
        $msg[3] = 'Contract Management: Because of cross-user sequential number, '
                . 'each department can have only one installation.';
    }
    .... 
    ->validator( function ( $editor, $editorAction, $data ) use ( $msg ) {
        if ( $editorAction === Editor::ACTION_CREATE || 
             $editorAction === Editor::ACTION_EDIT       ) {
            foreach ( $data['data'] as $pkey => $values ) {
                if ( isset($values['ctr_installation-many-count']) ) {
                    if ( $values['ctr_installation-many-count'] <= 0 ) {
                        return $msg[1];
                    }
                    if ( (bool)$_POST['is_subsidy_register'] ) {
                        if ( $values['ctr_installation-many-count'] > 1 ) {
                            return $msg[2];
                        }
                    } else {
                        if ( $values['ctr_installation-many-count'] > 1 ) {
                            if ( (bool)$values["numeric_serial"] ) {
                                return $msg[3];
                            }
                        }
                    }
                }
            }
            return null;
        }
    } )
    
  • allanallan Posts: 63,564Questions: 1Answers: 10,479 Site admin

    For client-side token resolution, you could use postSubmit on the client-side. Spin over the fieldErrors array and replace the status value with the token lookup. If I were to implement token lookup in Editor, then this is exactly what I would be doing internally.

    Allan

  • rf1234rf1234 Posts: 3,004Questions: 87Answers: 421

    Interesting solution! I am using that, too. But only to capture SQL errors in order to present a meaningful error message to my users:

    table
        .on( 'postSubmit', function ( e, json, data, action ) {
            if (json.error) {
                if ( json.error.indexOf('1062 Duplicate entry') >= 0 ) {
                   json.error = lang === 'de' ? 
                                "Tut uns Leid, aber Sie haben schon eine \n\
                                 zugewiesene Rolle für diese Abteilung!" : 
                                "Sorry, you already have an assigned role for \n\
                                 this department!"; 
                }
                if ( json.error.indexOf('1452 Cannot add or update a child row') >= 0 ) {
                    json.error = lang === 'de' ? 
                        "Keine Dateneingabe erlaubt, nur Auswahl!" : 
                        "Data entry not permitted, only selection!";
                }
            } 
        });
    
  • we0038we0038 Posts: 39Questions: 13Answers: 1

    Hi Everybody,
    Thank you all for your outputs. That was very useful.
    However, let me take this a step further. When I said a practical way in my first question, what I meant was re-usability. For example, the Internationalisation plug-ins in datatables does support internatiolisation by design. It is easy to use and maintain.
    I hope Editor's back-end messages and database error messages will be redesigned in similar manner in future updates.

  • allanallan Posts: 63,564Questions: 1Answers: 10,479 Site admin

    It is certainly something I can keep under review. It could be that I check all returned error messages to see if they start editor.error. and if so, run them through i18n().

    Allan

Sign In or Register to comment.