Access to my message.properties file from javascript

1

I need to access variables that are in my "message.properties" file from a .js file Suppose we have the following:

message.properties

ERROR = La longitud máxima son 20 digitos.

validacion.js In this file, what I currently have is the following:

  $.validator.addMethod('msis', function (value, element) {     
        var result = false, m, regex1 = /^\d{1,20}$/;

        if(regex1.test(value))
            result = true;

        return result;
    }, 'Formato invalido. ');

Does anyone know how I could replace the text string that I currently show "Format invalidated" by the ERROR of my message.properties?

    
asked by Mr.Z 19.07.2018 в 09:52
source

1 answer

0

The internationalization files (i18n) can not be accessed from the user, since they are inside the JAR / WAR and are not served.

There are several options, but I mention one: Create accessible i18n files for front-end, for example a JSON file:

en.json

{
  "errors": {
    "mailFormat": "El formato del email es inválido"
    ...
  }
}

en.json

{
  "errors": {
    "mailFormat": "The email format is not valid"
    ...
  }
}

And get the correct file by AJAX according to the language, to then read the messages:

alert(i18n.errors.mailFormat);

This is more or less what libraries do like ngx-translate for Angular or i18next (compatible with many frameworks and libraries)

    
answered by 19.07.2018 в 10:40