c# Custom Attribute with support for string resources does not load localized resources

1k views Asked by At

I have a DLL containing this custom attibutes

[AttributeUsage(AttributeTargets.Class)]
public class MyAttribute: System.Attribute {

private ResourceManager resManager = null;

public MyAttribute() {
}

public Type ResourceType { get; set; }

private string caption = string.Empty;
public string Caption {
  get { return getResourceString(caption); }
  set { caption = value; }
} //Caption

private string getResourceString(string resourceKey) {

  if (string.IsNullOrWhiteSpace(resourceKey))
    return string.Empty;

  if (ResourceType == default(Type))
    return resourceKey;

  if (resManager == null)
    resManager = new ResourceManager(ResourceType);

  return resManager.GetString(resourceKey) ?? string.Format("[[{0}]]", resourceKey);

} //getResourceString()

}

And a program (exe) that use the MyAttribute and also has the resources needed (resKeyHello) for both english and french in Resources.resx and Resources.fr.resx respectively

[MyAttribute(Caption="resKeyHello", ResourceType=typeof(Resources)]
public Foo() {
}

The problem is that it never use the french satellite assembly. I also tried to create the ResourceManager like this ResourceManager(ResourceType.FullName, ResourceType.Assembly), but without any effect. And also tried loading the resource like this resManager.GetString(resourceKey, Thread.CurrentThread.CurrentUICulture)

I can see in debugger that CurrentUICulture is french.

1

There are 1 answers

0
Donald Leclerc On BEST ANSWER

I finally found a workaround.

The problem seem to be about the way french resources file were generated. I used Project -> New -> Resources file with same name and .fr.resx extension.

The satellite DLL were generated during compilation in the ./bin/fr sub folder, but the runtime did not see it.

Finally I used "ResX Resource Manager" from CodePlex to generate my resource files and now everything is ok