Search code examples
javascriptjqueryasp.net-mvcresx

How to fix spanish grave accent issue from resouce file?


The multilingual feature are working fine in normal html page from resource file , but when it comes to javascript its fails For Example : Success is 'éxito' in spanish in html page its working fine but when it comes to javascript it is displayed as & #201;xito
if we hardcode 'éxito' as string in javascript it will also works , but when loading from resouce file to javascript it fails

                    

function Login_Create_user(id) {

        $.ajax({
            url: '@Url.Action("CreateLogin", "User")',
            type: "POST",
            async: false,
            data: { studentId: Id },
            success: function(result1) {
                if (result1 == true) {
                    swal("@Resource.Success", "StudentLoginCreatedSuccessfully.", "success");
                    window.location.reload();
                } else if (result1 == false) {
                    swal("@Resource.warning", "@Resource.FailedtoCreatelogin ! @Resource.Pleasetryagainlater", "warning");
                } else {

                    swal("@Resource.warning", result1, "warning");
                }
            },
            error: function(ex) {

            }
        });
    }

I expect 'éxito' should load as 'éxito' when it loaded from resouce file to javascript


Solution

  • you can decode ajax response using any of following functions either using pure JavaScript or Jquery

    //Decode HTML-entities (JS)
    function decodeHTMLEntities(text) {
      var textArea = document.createElement('textarea');
      textArea.innerHTML = text;
      return textArea.value;
    }
    
    //Decode HTML-entities (JQuery)
    function decodeHTMLEntities(text) {
      return $("<textarea/>")
        .html(text)
        .text();
    }
    
    
    decodeHTMLEntities('&#201;xito')
    

    output: "Éxito"