locked
How to use "System.Resources.ResourceManager" in a website mode project ? RRS feed

  • Question

  • User-1730506059 posted

    I am working with a website project here that was created by a 3rd party and trying to implement some localization functionality.

    Since there is already a LocalizationModule existing but we do not want to use that kind, I though about using ResourceManager to Translate parts of the site.

    Basically the project tree is something like this : 

    /
    /App_Code
      |_ Locale.cs
    / App_GlobalResources
      |_ Locale.en.resx
      |_ Locale.fr.resx
    /default.aspx



    The file Locale.cs is quite simple : 

    /// <summary><br/>
    /// Summary description for Locale
    /// </summary>
    public static class Locale
    {
    	private static System.Resources.ResourceManager Manager;
    	
    	public static void Initialize()
    	{
    		Manager = new System.Resources.ResourceManager("Resources.Locale", Assembly.Load("App_GlobalResources"));
    	}
    	public static string T(string search)
    	{
    		var text = Manager.GetString(search);
    
    		if (text == null)
    			return "null";
    		else if (text == string.Empty)
    			return "empty";
    		else
    			return text;
    	}
    }
    


    In Global.asax Application_Start :

    Locale.Initialize();


    Now I used the class in a template like this : 

    <%= Locale.T("T_HOME") %>
    

    I got this error at runtime : 

    Server Error in '/' Application.
    
    Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.
    
    Source Error: 
    
    
    Line 14: 	public static void Initialize()
    Line 15: 	{
    Line 16: 		Manager = new System.Resources.ResourceManager("Resources.Locale", Assembly.Load("App_GlobalResources"));
    Line 17: 	}
    Line 18: 
    
    Source File: c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\App_Code\Locale.cs    Line: 16 
    
    Assembly Load Trace: The following information can be helpful to determine why the assembly 'App_GlobalResources' could not be loaded.
    
    
    WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
    
    Stack Trace: 
    
    
    [FileNotFoundException: Could not load file or assembly 'App_GlobalResources' or one of its dependencies. The system cannot find the file specified.]
       System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
       System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +43
       System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +127
       System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +142
       System.Reflection.Assembly.Load(String assemblyString) +28
       Locale.Initialize() in c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\App_Code\Locale.cs:16
       ASP.global_asax.Session_Start(Object sender, EventArgs e) in c:\inetpub\vhosts\galerieocarre.com\subdomains\dev\httpdocs\Global.asax:26
       System.EventHandler.Invoke(Object sender, EventArgs e) +0
       System.Web.SessionState.SessionStateModule.RaiseOnStart(EventArgs e) +8779824
       System.Web.SessionState.SessionStateModule.CompleteAcquireState() +237
       System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) +504
       System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +66
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
    
    Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074


    I tried to implements this tutorial in fact : http://forums.asp.net/p/1118923/1741721.aspx#1741721

    Wednesday, February 3, 2010 10:46 AM

All replies

  • User1611391320 posted

    why dont u use simply like ds

    Resources.Resource.ResourceManager.GetString("");


    Thursday, February 4, 2010 11:51 PM
  • User771898575 posted

    check the link
    http://stackoverflow.com/questions/2156265/how-to-use-resourcemanager-in-a-website-mode/2190578

    Friday, February 5, 2010 12:00 AM
  • User-574293449 posted

    i am also thinking the same as vijay told y not simply acces like that..

    or you use resourceset if u access like global resource

     

     

    ResourceSet st = Resource1.ResourceManager.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true);
    Label1.Text = st.GetString("keyname");




     


     

    Friday, February 5, 2010 12:01 AM
  • User-624088818 posted

    //Lets take example that you are using 'en-US'
    Thread.CurrentThread.CurrentCulture = 'en-US';
    Thread.CurrentThread.CurrentUICulture = 'en-US';
    
    string ResourceFile = Server.MapPath("App_GlobalResources" + "\\" + cInfo);
    ResourceManager rManager = ResourceManager.CreateFileBasedResourceManager("resource", ResourceFile, null);
    var text = rManager.GetString(search); 
      
            if (text == null)  
                return "null";  
            else if (text == string.Empty)  
                return "empty";  
            else  
                return text;



    Monday, February 8, 2010 7:58 AM
  • User-1730506059 posted

    It's definetly a very good start!

    The only problem I see here is that it's searching for a .resources file. The resources I added had the .resx extension. A .resources file is a windows application extention I believe. So the next step is to find a way to look for resx instead of the .resources file 

    Monday, February 8, 2010 10:53 AM