editor field validation with multiple languages

editor field validation with multiple languages

rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

How can I use multiple language error messages without having to provide English wording for the default validation again?
As I see it right now I must specify a "message" entry in the array passed to validator if I want another language. I have no option to leave it empty and have validator provide the English default messages. I can only drop the "message" entry completely to get the English default error messages, but then I cannot have a translation at all. This makes a mess with the code I guess because I need different "Field::inst ... " statements per language. Do you have an idea how to solve this more elegantly?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,831Questions: 1Answers: 10,518 Site admin
    Answer ✓

    This makes a mess with the code I guess because I need different "Field::inst ... " statements per language.

    That would be a nightmare for certain!

    The why I approach this myself is to have the language name stored in a session variable (although it could be stored anywhere) and have a lookup function that will provide the translation - e.g.:

    Field::inst( 'range' )->validator( 'Validate::minMaxNum', array(
        'min' => 10,
        'max' => 100,
        'message' => i18n( 'Please enter a number between 10 and 100' )
    ) );
    

    Then the i18n() method would either look up a translation for that string, or if the language is English, just use that default:

    function i18n ( $str ) {
      if ( $_SESSION['lang'] === 'en_US' ) {
        return $str;
      }
      else {
        return ...; // token lookup
      }
    }
    

    I tend to actually use a JSON token rather than the English phrasing, but I've seen both ways being used in various applications. I guess if you wanted the translations to all be inline, you could simply define them in an array that is passed to the i18n() function that would then pick the correct one.

    Allan

  • rf1234rf1234 Posts: 3,028Questions: 88Answers: 422

    Thanks, Allan!

This discussion has been closed.