Showing posts with label File as. Show all posts
Showing posts with label File as. Show all posts

Sunday, 10 May 2015

CSharp Read Resource File as Json

protected void Page_Load(object sender, EventArgs e)
        {
          mapResFileName = "MapResources.en-US.resx";
            if (m_UserLanguage == "ar")
            {
                mapResFileName = "MapResources.resx";
            }
            ClientScript.RegisterStartupScript(this.GetType(), "localizedString", getReourseFileAsJSON(mapResFileName), true);
        }





        public string getReourseFileAsJSON(String resourceFileName)
        {           
            try
            {
                string mapResString = "localizedString={'userLang':'" + m_UserLanguage + "',";
                ResXResourceReader rsxr = new ResXResourceReader(
                String.Format("{0}{1}\\{2}", System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "App_GlobalResources", resourceFileName));
                foreach (DictionaryEntry d in rsxr)
                {
                    mapResString += "'" + d.Key.ToString() + "':'" + d.Value.ToString() + "',";
                }
                mapResString += "};";
                return mapResString;
            }
            catch (Exception exc)
            {
                return "File Not Found";
            }
            

        }