I’m tinkering with an error system where I use enums to identify errors. The enums are contained serverside and converted into JS objects, but I don’t know where to keep my error messages.
Errors enum
public enum MyError
{
InvalidThingFormat = 1001,
AllowedThingsExceeded = 1002
}
Generated JavaScript “enum”
var MyError= {InvalidThingFormat :1001,AllowedThingsExceeded:1002};
And do checking like
switch(data.error) {
case MyError.InvalidThingFormat:
alert("Format validation failed");
break;
// etc.
}
So far I like how this feels but I don’t like the hardcoded error messages; so where would I put my localization/internationalization of the human readable error message?
Should there also be a backend translation set that I push out in JS or should the translations be only on the JS side, handled by some other method? What would the code look like; Error.GetMessage(MyError.AllowedThingsExceeded)?
You would have several javascript files, each named for their localization. IE:
en-us.js:
es-mx.js: (Note, I don’t actually know spanish)
So on and so forth.
So you would include en-us.js as a default, then after that, include whatever the appropriate localization is.
You could then use it like so:
or