Skip to content

Bad Error Message in ResourceManager #43906

Closed
@mandalorianbob

Description

@mandalorianbob

I just ran into an issue with the ResourceManager and while the issue was my fault, the error I got out of it caused me to spin my wheels for a while.

I am creating a .NET Standard library for some internal development. And to avoid having to pass around and maintain resource files, I am including localized resources in the nuget package. However, I cannot depend on using thread culture for retrieving resources, because there's no guarantee that the thread culture is what is to be used. (Both UWP and Unity might be using this library.)

Thus, I needed to take in a CultureInfo object, and then get Resources based on that, using a ResourceManager.

In order to get Nuget to package the resources correctly I had to name them all [Name].[locale].resx. (Another undocumented pitfall.) Unfortunately, I also did this to the en-us resources. This was incorrect, but I didn't know that.

So when I tried to make a ResourceManager looking for the appropriate resources by passing an "en-us" CultureInfo object, I got back an error that said it couldn't find them.

System.Resources.MissingManifestResourceException: Could not find the resource "[Name].resources" among the resources "[Unrelated].resources" embedded in the assembly "[Assembly]", nor among the resources in any satellite assemblies for the specified culture. Perhaps the resources were embedded with an incorrect name.

But of course, in the Resource Manager code we see

if (lookForCulture.HasInvariantCultureName && _mediator.FallbackLoc == UltimateResourceFallbackLocation.MainAssembly)
{
      // don't bother looking in satellites in this case
      satellite = _mediator.MainAssembly;
}

So this error message really messed me up. Because what was actually happening was not what the error message said was happening. The Satellite assembly was after all, getting created. (It creates the satellite assembly regardless of the culture's name.) And the resx had the correct name, but it accidentally had the locale when it shouldn't have.

I tried finding documentation, but that just lead me around in circles. Of course, part of the problem was that I wasn't misusing the API, it was the data that was wrong.

Thankfully when I realized I could just step through the code (thanks SourceLink!) I found this and realized what was going on.

But this whole process was very poorly documented and this particular pitfall was really exacerbated by this particular error message not mentioning anything about the invariant culture or the fact that the satellite assemblies would get skipped in the event of culture name matching the invariant culture.

Localization has proven to have more pitfalls than expected, and at almost every step when something failed, it feels like it was very hard to figure out what failed. I'm hoping at least this small part could be corrected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.ResourcesenhancementProduct code improvement that does NOT require public API changes/additions

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions