Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript converts french special characters

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é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.

like image 584
Silviu Preda Avatar asked Feb 04 '14 09:02

Silviu Preda


1 Answers

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)

like image 80
metadings Avatar answered Sep 18 '22 17:09

metadings