Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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..

like image 215
Tomas Vana Avatar asked Dec 22 '22 08:12

Tomas Vana


1 Answers

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.

like image 98
Dänu Avatar answered Apr 22 '23 10:04

Dänu