Search code examples
c#asp.netasp.net-mvcglobalization

Globalization unable to find appropriate resources



I'm trying to globalize an mvc application (french and english), here's what I did:

1) Created an App_LocalResources folder and added 2 files:

  • Resources.resx (will contain french) and
  • Resources.en.resx (will contain english).

Then I added one resource in each file with the same name (just to test) and modified the access modifier to Public (for each file)

2) Specified the french culture in global.asax

protected void Session_Start()
{
    string userLanguage = Request.UserLanguages[0];
    string cultureName = CultureHelper.GetImplementedCulture(userLanguage);
    Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
    Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
    HRP.App_LocalResources.Resources.Culture = Thread.CurrentThread.CurrentCulture;
}

CultureHelper is a helper I found online, it determines the correct name of the culture (credits to the author): http://pastebin.com/JkhjJg4N

3) Added a test string to display in my view:

<p><span class="Title">@HRP.App_LocalResources.Resources.EmployeesTitle</span></p>

And the exception is:

[MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "HRP.App_LocalResources.Resources.resources" was correctly embedded or linked into assembly "HRP" at compile time, or that all the satellite assemblies required are loadable and fully signed.]

Please help.


Solution

  • I have done a small example showing how your code should work out.

    1. Set the build action of the resources to embedded resource
    2. Enter custom tool PublicResXFileCodeGenerator

    Afterwards your resources will be available within your code

    razor file

    <p>@NAMESPACE.App_LocalResources.YOUR_RESOURCE_FILE.RESOURCE_NAME</p>
    

    I have used another custom tool as K. Scott Allen stated

    Resx Files Outside Of Special Resource Directories resx properties in MVC

    If you add a resx file to any other folder in an MVC project or class library, the resx is automatically set to be embedded into the project’s output assembly - this is good. The IDE also assigns the resx a custom tool of ResxCodeFileGenerator to generate a strongly typed wrapper - this is good. The generated class is internal by default – this is bad. The assembly created for a view (by ASP.NET) won’t be able to use the internal class because it is in a different assembly – the project assembly compiled in Visual Studio.

    So if you add your resource files to the special folders - anything goes right. If you choose to place it on another path it would fail (unless you use the custom tool).