开发者

How to dynamically load and switch the resource file in the web app (ASP.NET) without recompiling?

开发者 https://www.devze.com 2022-12-10 08:04 出处:网络
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 wa

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 开发者_JAVA百科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..


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.


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) {
        // ...
    } 
}


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消