Javascript converts french special characters

asp.net-mvc-3, javascript, unicode-string

Solution

According to your comment

<script type="text/javascript" charset="utf-8">
/*...*/
var localizedResources = { 
   FailedUploadErrorMessage: '@Resources.GeneralLocalization.FailedUploadErrorMessage'
/*...*/ 

you tried to render javaScript using ASP.NET MVC Razor. Usually Razor escapes special characters to entities, like `&` are becoming `&amp;` non-named entities like `é` are becoming `&#233;`. In javaScript, you need these characters either native in UTF-8/16 (or if really not possible encoded like `\u00E9`).

In Razor, the `@` makes the rendering engine to output an encoded, escaped, entity-fied representation in HTML.

When rendering javaScript you don't want Razors implicit escaping, so what you need is

<script type="text/javascript" charset="utf-8">
/*...*/
var localizedResources = { 
   FailedUploadErrorMessage: '@Html.Raw(Resources.GeneralLocalization.FailedUploadErrorMessage)'
/*...*/

And oops: also javaScript is UTF-16 (with some caveats)

Problem

In my MVC 3 app I have several resx files with message strings such as: `"Editer groupe détails"`. All these are used for either window titles or confirm/alert messages. So, in order to be able to use them in my various .js files I have created in my master page a Javascript object called localizedRessources. The problem is that the strings get altered in this object, for example, the above mentioned string becomes `"Editer groupe d&#233;tails"`. What are my options here? Is there a special function that tells JS not to encode these? Is there a way to directly use the strings in my .js files? (this works, by the way, in some cases, but i would rather I didn't have too much js code in my cshtml files). I have the `<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">` tag added to my `<head>` element for all pages, so I am fine there. Any suggestion is welcome.

Original source

Related problems