locked
Editing GlobalResources files at run time RRS feed

  • Question

  • User966057038 posted

    I'm creating a multi-lingual website that has a lot of static text that needs to be toggled between languages. I've put Labels as the container for my text, and assigned the text to the values in the Resources.resx files (stored in App_GlobalResources) like this:

    <asp:Label ID="Label3" runat="server" Text="<%$ Resources:Resource, Msg1 %>"></asp:Label>

    The language toggling works fine. But I'm also trying to build a content management feature where non-technical website administrators can edit the text in the Value of the key Msg1. They would just need to type in the text in a TextBox and when a "Save" is hit, the TextBox.Text will be assigned to the Value of the Msg1 key.

    Questions:

    1. Is there a way I can access and set the Key/Values of Resources.resx at run time? I know there's a GetGlobalResourceObject method but is there any SetGlobalResourceObject or equivalent method?

    2. Is my approach to this problem correct, or is there a better way to build a CMS that is linked to multi-lingual resource files.

    Thanks in advance for the help.

    Saturday, September 9, 2006 2:28 AM

Answers

  • User-1936818034 posted

    As you use asp.net built-in resource management, I recommend you not to edit resx file runtime as it's one of the asp.net application items, by modifying it you will force a new compilation and the application will restart, that means you lose all your states such as session, cache, application ...

    Btw: Resource files are just XML files, thought the framework doesn't provide you with some Set method, you can do it the xml way. Open it as XmlDoc, use XPath to locate the string, and modify. In fact you can even do this to web.config, which will also cause a restart, though ...

    In my opinion, if you wanna your application state kept, some other approach is needed ... some mechanism like this, or the sql way, but bad news is it's hard for those walkarounds to provide design time support

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 10, 2006 8:49 PM
  • User-1936818034 posted
    Hi~ I can provide you with some code I formerly used to modify winform config file's appSettings section, it's just a sample for you. You can then right-click on .resx file and 'View Code' to analyze its schema yourself and do some change in the code: 
            public static void SetCfgValue(string AppKey, string AppValue)
            {
                System.Xml.XmlDocument xDoc = new XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
    
                XmlNode xNode;
                XmlElement xElemKey;
                XmlElement xElemValue;
    
                xNode = xDoc.SelectSingleNode("//appSettings");
    
                xElemKey = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + AppKey + "\"]");
                if (xElemKey != null) xElemKey.SetAttribute("value", AppValue);
                else
                {
                    xElemValue = xDoc.CreateElement("add");
                    xElemValue.SetAttribute("key", AppKey);
                    xElemValue.SetAttribute("value", AppValue);
                    xNode.AppendChild(xElemValue);
                }
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
            }
     Basicly, you will find .resx file is some what like: 
    <root>
    	<data name="resxName1">
    		<value>resxValue1</value>
    	</data>
    	<data name="resxName2">
    		<value>resxValue2</value>
    	</data>
    	......
    </root>
     And you may also want some Xpath Reference~
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 12, 2006 3:58 AM

All replies

  • User-1936818034 posted

    As you use asp.net built-in resource management, I recommend you not to edit resx file runtime as it's one of the asp.net application items, by modifying it you will force a new compilation and the application will restart, that means you lose all your states such as session, cache, application ...

    Btw: Resource files are just XML files, thought the framework doesn't provide you with some Set method, you can do it the xml way. Open it as XmlDoc, use XPath to locate the string, and modify. In fact you can even do this to web.config, which will also cause a restart, though ...

    In my opinion, if you wanna your application state kept, some other approach is needed ... some mechanism like this, or the sql way, but bad news is it's hard for those walkarounds to provide design time support

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 10, 2006 8:49 PM
  • User966057038 posted

    Thanks very much Gordon-Freeman.

    DO need to edit the resx file at runtime. It would be ideal if the application did not restart, but I guess I could live with it if it did.

    1. What is the "SQL way"? Is it storing all the key-values in a database table? Do I then just retrieve the value each time via a database query? Since this would be quite expensive, is there a more efficient way of doing it (say, storing all the values in an array stored in an application variable)?

    2. Assuming I'm ok with the application restarting, is there a way I can update the key-values in the resx file through a front end interface? Can they be set just like object properties?

    Thanks for the help.

    Tuesday, September 12, 2006 3:14 AM
  • User-1936818034 posted
    Hi~ I can provide you with some code I formerly used to modify winform config file's appSettings section, it's just a sample for you. You can then right-click on .resx file and 'View Code' to analyze its schema yourself and do some change in the code: 
            public static void SetCfgValue(string AppKey, string AppValue)
            {
                System.Xml.XmlDocument xDoc = new XmlDocument();
                xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
    
                XmlNode xNode;
                XmlElement xElemKey;
                XmlElement xElemValue;
    
                xNode = xDoc.SelectSingleNode("//appSettings");
    
                xElemKey = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + AppKey + "\"]");
                if (xElemKey != null) xElemKey.SetAttribute("value", AppValue);
                else
                {
                    xElemValue = xDoc.CreateElement("add");
                    xElemValue.SetAttribute("key", AppKey);
                    xElemValue.SetAttribute("value", AppValue);
                    xNode.AppendChild(xElemValue);
                }
                xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
            }
     Basicly, you will find .resx file is some what like: 
    <root>
    	<data name="resxName1">
    		<value>resxValue1</value>
    	</data>
    	<data name="resxName2">
    		<value>resxValue2</value>
    	</data>
    	......
    </root>
     And you may also want some Xpath Reference~
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 12, 2006 3:58 AM
  • User966057038 posted

    Thanks Gordon-Freeman – your help is much appreciated!

    Tuesday, September 12, 2006 10:39 PM