Search code examples
javascriptc#asp.net-mvcmodel-view-controllermultilingual

How to get resource (.resx) string in JS file


I am developing an application MVC 5 in multilingual and I have some functions in my xyz.js file, but I don't know how to get resource string in .js file. I have explored a lot on web but not able to find suitable answer. Please help me out for this.

Thanks in advance.


Solution

  • I have resolved this problem. I have made a method in my controller which will get the resource string from resource file and I used that string further in my js file.

      public JsonResult GetResourceString(string labelName)
        {
            string result ="";
            result = ResourceMessage.ResourceManager.GetString(labelName);
            if (Request.Cookies["culture"].Value == "th")
            {
                cul = CultureInfo.CreateSpecificCulture("th"); 
                 result = ResourceMessage.ResourceManager.GetString(labelName,cul);
            }
            return Json(new
            {
                message = result
            }, JsonRequestBehavior.AllowGet);
        }
    

    Above method will get resource string and then you can use it. Here ResourceMessage is my resource file name and cul is CultuerInfo.

    Thanks :)