Last post Sep 12, 2006 10:39 PM by multiplex7777
Sep 09, 2006 02:28 AM|multiplex7777|LINK
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
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.
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.
Sep 10, 2006 08:49 PM|Gordon-Freeman|LINK
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
Sep 12, 2006 03:14 AM|multiplex7777|LINK
Thanks very much Gordon-Freeman.
I 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.
Sep 12, 2006 03:58 AM|Gordon-Freeman|LINK
public static void SetCfgValue(string AppKey, string AppValue)
System.Xml.XmlDocument xDoc = new XmlDocument();
xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
xNode = xDoc.SelectSingleNode("//appSettings");
xElemKey = (XmlElement)xNode.SelectSingleNode("//add[@key=\"" + AppKey + "\"]");
if (xElemKey != null) xElemKey.SetAttribute("value", AppValue);
xElemValue = xDoc.CreateElement("add");
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
Sep 12, 2006 10:39 PM|multiplex7777|LINK
Thanks Gordon-Freeman – your help is much appreciated!