Search code examples
c#asp.net.netasp.net-web-apiglobalization

Issue while accessing resource file strings in Web API


I have added a resource file which needs to support globalization.

enter image description here

In Global.asax I received the culture info.

protected void Application_BeginRequest()
    {
        var cul = Context.Request.Headers["culture"];
        if (cul != null && !string.IsNullOrEmpty(cul))
        {
            var culture = new CultureInfo(cul);
            //Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
        }
    }

Now I'm trying to access it as:

ResourceManager rm = new ResourceManager("Resource", System.Reflection.Assembly.GetExecutingAssembly());
string err = rm.GetString("ERROR_1");

The error I get is:

{"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \"Resource.resources\" was correctly embedded or linked into assembly \"...\" at compile time, or that all the satellite assemblies required are loadable and fully signed."} System.SystemException {System.Resources.MissingManifestResourceException}

The code and resource has same namespace.


Solution

  • I don't have much experience with the ResourceManager, but from the official documentation I don't think that using reflection to get the assembly is intended. It specifically mentions that the second parameter, the assembly, should be 'the assembly in which the default .resources file resides'. It later talks about setting Thread.CurrentThread.CurrentUICulture. You can find all that information on msdn: https://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(v=vs.110).aspx

    However, you might want to take a look at the msdn documentation on localizing asp.net web pages: https://msdn.microsoft.com/en-us/library/ms227427.aspx. It seems to have some specialized mechanisms that might be very useful to you.