turquoiseowl / i18n

Smart internationalization for ASP.NET
Other
556 stars 155 forks source link

Json serialization with escaped characters. #182

Open rickardliljeberg opened 9 years ago

rickardliljeberg commented 9 years ago

I have this MVC controller that returns an object to a AJAX call with JSON serialization as you can see here: https://db.tt/ppJJmS5x

However this is what reaches the client

{"Success":2,"Message":"Unable to delete the tag while it\u0027s in use. The tag is used in 1 places","PermissionAccessValid":true,"PermissionLevel":0,"PermissionResponseId":null,"PermissionResponseBool":null,"PermissionResponseText":null}

you can see that the "it's" has been encoded to "it\u0027s" and for that reason (I think at least) it does not find the nugget.

I don't quite know what is the best action here and would happily listen to advice. One possibility is to allow the i18n project to find escaped characters but i see definite drawbacks with that as well. Any suggestions?

turquoiseowl commented 9 years ago

Would it not be reasonable to write the nugget [[[it's]]] as [[[it\u0027s]]] in the first place?

rickardliljeberg commented 9 years ago

Well in my case it would be OK. but it feels annoying that I need to think of that when it will be jsoned I could also rewrite it's with it is.

It would just loose that freedom that this project gives about not caring about delivery just make nuggets and it will be served right.

But I don't have a great solution myself so yeah it deserves some thinking. On Feb 5, 2015 8:42 PM, "Martin Connell" notifications@github.com wrote:

Would it not be reasonable to write the nugget [[[it's]]] as [[[it\u0027s]]] in the first place?

— Reply to this email directly or view it on GitHub https://github.com/turquoiseowl/i18n/issues/182#issuecomment-73112374.

turquoiseowl commented 9 years ago

Perhaps a best-practices approach would be to say:

We recommend you always escape: nugget markup, html markup, and ' and " for json.

rickardliljeberg commented 9 years ago

Well you could but i see several problems with this.

  1. Human error
  2. Annoying to look up what ' and " are every time
  3. But perhaps biggest, it's not at all unlikely that i want to pipe the same data to a view and to a mobile application via json or xml.

Encoding the text as you write it seems both cumbersome and error prone.

I am more thinking along the lines of detecting json format, then looking for ' and " and anything else that breaks. Or perhaps just look for any encoded characters to see if encoding is going on for any reason.

So if the reply seems to be json and contains {varName:"[[[it's fine]]]"} then look for translation key both "it\u0007s fine" and "it's fine"

You could also configure when to trigger this behaviour (json for instance) and which characters to look out for and have interchangable.

We solved it with pure "it is" workaround, but could still be interesting.