5

I would like to store the resource files (containing texts for labels etc.) for my web application in the database to be able to edit and create them dynamically later (probably in the UI). My idea was to store the whole resx file in an xml column and simply load it on demand - depending on the language and some other attributes of the current user when he is logging into the application or switching the context. The important thing is that the resources do not only depend on the culture info of the user but also on some context information that can be switched by user without logging off and on again (in our case called "group", not having anything to do with a group of users).

Is it possible somehow to load the contents of the resources from an external source and simply switch them without web application being recompiled by the server ? I know that there are some interfaces on which I could hook up and implement a custom resources provider which reads from the database directly but if it could work somehow with the resx files it would probably make things a lot easier..

2
  • Did you get any solution for this? I am also facing the same situation. Thanks! Commented Jun 14, 2010 at 14:06
  • I'm also looking for the answer for that question. Commented Dec 17, 2010 at 9:00

3 Answers 3

6

Pretty late but since there is no answer as of yet.

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader("PathToResourceFile");

That's pretty much it. Now you can create resource files like en.resx or de.resx and load them depending on the users language. Something like

System.Resources.ResourceReader resourceReader 
    = new System.Resources.ResourceReader(HttpContext.Current.Request.UserLanguages[0] 
    + ".resource");

Keep in mind to provide a default language (resource file) for a user with a language you don't support.

Edit:

Take a look at this link.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to clarify this only handles compiled resource files e.g .resouce, as opposed to XML files e.g .resx right?
4

The question is 6 years old, but I'm still gonna answer it :)

To read .resx files, you need to use System.Resources.ResXResourceReader class from System.Windows.Forms.dll

This is nicely explained here. Just a quick sample for completeness:

using (ResXResourceReader resxReader = new ResXResourceReader(@".\CarResources.resx"))
{
    foreach (DictionaryEntry entry in resxReader) {
        // ...
    } 
}

Comments

1

Sure you can do this easily, and it works for straight XML files. You don't need to use a resx file.

/// <summary>
/// Sets or replaces the ResourceDictionary by dynamically loading
/// a Localization ResourceDictionary from the file path passed in.
/// </summary>
/// <param name="resourceDictionaryFile">The resource dictionary to use to set/replace
/// the ResourceDictionary.</param>
private void SetCultureResourceDictionary(String resourceDictionaryFile)
{
    // Scan all resource dictionaries and remove, if it is string resource distionary
    for ( int index= 0; index < Resources.MergedDictionaries.Count; ++index)
    {
        // Look for an indicator in the resource file that indicates the resource is
        // swappable. For instance in our files the header contains this: 
        // <sys:String x:Key="ResourceDictionaryName">Resources-en-CA</sys:String> 
        if (Resources.MergedDictionaries[index].Contains("ResourceDictionaryName"))
        {
            if ( File.Exists(resourceDictionaryFile) )
            {
                Resources.MergedDictionaries.Remove(Resources.MergedDictionaries[index]);
            }
        }
    }

    // read required resource file to resource dictionary and add to MergedDictionaries collection
    ResourceDictionary newResourceDictionary =  new ResourceDictionary();
    newResourceDictionary.Source = new Uri(resourceDictionaryFile);
    Resources.MergedDictionaries.Add(newResourceDictionary);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.